Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: publish CJS modules alongside ESM #165

Merged
merged 1 commit into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
npm install @puppeteer/replay --save
```

> Currently, @puppeteer/replay ships only ESM modules. Please vote/comment on [this issue](https://github.com/puppeteer/replay/issues/26), if you need CJS module support.

If you want to replay recordings using Puppeteer, install Puppeteer as well:

```
Expand Down Expand Up @@ -212,3 +210,12 @@ console.log(
)
);
```


## CommonJS modules

If you want to use CommonJS modules, `require` the code from `@puppeteer/replay/cjs`. For example,

```js
const { stringify } = require('@puppeteer/replay/cjs');
```
54 changes: 54 additions & 0 deletions examples/cjs/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const {
createRunner,
PuppeteerRunnerExtension,
} = require('../../lib/cjs/main.js');
const puppeteer = require('puppeteer');

async function main() {
const browser = await puppeteer.launch({
headless: true,
});

const page = await browser.newPage();

class Extension extends PuppeteerRunnerExtension {
async beforeAllSteps(flow) {
await super.beforeAllSteps(flow);
console.log('starting');
}

async beforeEachStep(step, flow) {
await super.beforeEachStep(step, flow);
console.log('before', step);
}

async afterEachStep(step, flow) {
await super.afterEachStep(step, flow);
console.log('after', step);
}

async afterAllSteps(flow) {
await super.afterAllSteps(flow);
console.log('done');
}
}

const runner = await createRunner(
{
title: 'Test recording',
steps: [
{
type: 'navigate',
url: 'https://wikipedia.org',
},
],
},
new Extension(browser, page, 7000)
);

await runner.run();

await browser.close();
}

main();
3 changes: 3 additions & 0 deletions examples/cjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"eslint-fix": "eslint --ext js --ext ts --fix .",
"clean-lib": "rimraf lib",
"build": "npm run tsc",
"tsc": "npm run clean-lib && tsc --version && tsc -b tsconfig.json",
"tsc": "npm run clean-lib && tsc --version && tsc -b tsconfig.json && tsc -b tsconfig.cjs.json && echo '{\"type\":\"commonjs\"}' > lib/cjs/package.json",
"lint": "npm run eslint",
"docs": "typedoc --readme none --gitRevision main --externalPattern --excludeExternals --excludeProtected --excludePrivate --plugin typedoc-plugin-markdown --out docs/api src/main.ts",
"release": "standard-version"
Expand Down
2 changes: 1 addition & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ interface Arguments {
headless?: string;
}

await yargs(hideBin(process.argv))
yargs(hideBin(process.argv))
.command(
'$0 <files..>',
'run files',
Expand Down
7 changes: 7 additions & 0 deletions tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "lib/cjs",
"module": "CommonJS",
}
}