Skip to content

Commit

Permalink
feat: publish CJS modules alongside ESM (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN committed Jun 1, 2022
1 parent 50f595d commit c40df05
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 4 deletions.
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",
}
}

0 comments on commit c40df05

Please sign in to comment.