Skip to content

Commit fc80333

Browse files
committed
feat: CommonJS compatibility
1 parent 1f60bad commit fc80333

File tree

5 files changed

+46
-15
lines changed

5 files changed

+46
-15
lines changed

apps/docs/src/guide/api.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,24 @@ yarn add webcrack
1616
pnpm add webcrack
1717
```
1818

19+
## Basic Usage
20+
1921
:::
2022

2123
:::info
22-
This is a pure ESM package, so you need to use `import` instead of `require`.
23-
For more info, check out [this gist](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).
24-
Or use the following wrapper in commonjs projects:
24+
All examples are shown with ESM syntax.
25+
For CommonJS, use the following instead:
2526

2627
```js
27-
async function webcrack(...args) {
28-
const { webcrack } = await import('webcrack');
29-
return webcrack(...args);
30-
}
28+
const { webcrack } = require('webcrack');
29+
30+
webcrack('const a = 1+1;').then((result) => {
31+
console.log(result.code); // 'const a = 2;'
32+
});
3133
```
3234

3335
:::
3436

35-
## Basic Usage
36-
3737
```js
3838
import { webcrack } from 'webcrack';
3939

packages/webcrack/esbuild.config.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ const watch = args.length > 0 && /^(?:--watch|-w)$/i.test(args[0]);
99
*/
1010
const babelImportPlugin = {
1111
name: 'babel-import',
12-
setup: build => {
13-
build.onResolve({ filter: /^@babel\/(traverse|generator)$/ }, args => {
12+
setup: (build) => {
13+
build.onResolve({ filter: /^@babel\/(traverse|generator)$/ }, (args) => {
1414
return {
1515
path: args.path,
1616
namespace: 'babel-import',
1717
};
1818
});
1919

20-
build.onLoad({ filter: /.*/, namespace: 'babel-import' }, args => {
20+
build.onLoad({ filter: /.*/, namespace: 'babel-import' }, (args) => {
2121
return {
2222
resolveDir: 'node_modules',
2323
contents: `import module from '${args.path}/lib/index.js';
@@ -35,6 +35,17 @@ const configs = [
3535
{
3636
entryPoints: ['src/index.ts'],
3737
},
38+
{
39+
entryPoints: [
40+
{
41+
in: 'src/cjs-wrapper.ts',
42+
out: 'index',
43+
},
44+
],
45+
outExtension: { '.js': '.cjs' },
46+
format: 'cjs',
47+
bundle: false,
48+
},
3849
{
3950
entryPoints: ['src/cli.ts'],
4051
bundle: false,

packages/webcrack/package.json

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,21 @@
55
"author": "j4k0xb",
66
"license": "MIT",
77
"type": "module",
8-
"main": "dist/index.js",
9-
"types": "dist/index.d.ts",
108
"bin": "src/cli-wrapper.js",
9+
"main": "dist/index.cjs",
10+
"types": "dist/index.d.ts",
11+
"exports": {
12+
".": {
13+
"import": {
14+
"types": "./dist/index.d.ts",
15+
"default": "./dist/index.js"
16+
},
17+
"require": {
18+
"types": "./dist/index.d.ts",
19+
"default": "./dist/index.cjs"
20+
}
21+
}
22+
},
1123
"files": [
1224
"dist",
1325
"src/cli-wrapper.js"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import type { webcrack as wc } from './index.js';
2+
3+
export const webcrack: typeof wc = async (...args) => {
4+
const { webcrack } = await import('./index.js');
5+
return webcrack(...args);
6+
};

packages/webcrack/tsconfig.build.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
"src"
1010
],
1111
"exclude": [
12-
"**/test"
12+
"**/test",
13+
"src/cli.ts",
14+
"src/cjs-wrapper.ts"
1315
]
1416
}

0 commit comments

Comments
 (0)