Skip to content

Commit ba88bd8

Browse files
authored
Generate CommonJS files (#327)
Our npm package is now a CJS package :'( We still include the ESM file in the npm package but we do not tell npm about it. You can use it by importing "replicache/out/replicache.mjs" for now.
1 parent 5469151 commit ba88bd8

File tree

6 files changed

+36
-44
lines changed

6 files changed

+36
-44
lines changed

.gitignore

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
coverage
22
node_modules
3-
out
4-
build
53
.DS_Store
6-
repc
74
src/wasm
8-
docs
5+
docs
6+
out

package.json

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@
1111
"check-format": "prettier --check '{src,sample,perf}/**/*.{js,jsx,json,ts,tsx,html,css,md}' '*.{js,jsx,json,ts,tsx,html,css,md}'",
1212
"lint": "eslint --ext .ts,.tsx,.js,.jsx src/ perf/",
1313
"doc": "typedoc src/mod.ts",
14-
"build": "rollup --config rollup.config.js",
15-
"build-dev": "DEV=1 rollup --config rollup.config.js",
14+
"build": "rollup --config rollup.config.js && CJS=1 rollup --config rollup.config.js ",
1615
"build:watch": "rollup --config rollup.config.js --watch",
17-
"postinstall": "rm -f node_modules/fetch-mock/esm/client.d.ts && tool/get-deps.sh",
18-
"prepublishOnly": "npm run lint && npm run get-repc && npm run test && rm -rf out && npm run build && npm run build-dev",
16+
"postinstall": "rm -f node_modules/fetch-mock/esm/client.d.ts",
17+
"prepublishOnly": "npm run lint && tool/get-deps.sh && npm run test && rm -rf out && npm run build",
1918
"prepack": "npm run prepublishOnly",
20-
"perf": "node perf/runner.js",
21-
"get-repc": "tool/get-deps.sh"
19+
"prepare": "tool/get-deps.sh",
20+
"perf": "node perf/runner.js"
2221
},
2322
"devDependencies": {
2423
"@esm-bundle/chai": "^4.3.0",
@@ -46,15 +45,12 @@
4645
"typedoc-plugin-sourcefile-url": "^1.0.6",
4746
"typescript": "^4.2.3"
4847
},
49-
"type": "module",
50-
"module": "out/replicache.js",
51-
"types": "out/replicache.js",
48+
"main": "out/replicache.js",
49+
"types": "out/replicache.d.js",
5250
"files": [
53-
"out",
54-
"tool/get-deps.sh"
55-
],
56-
"exports": {
57-
".": "./out/replicache.js",
58-
"./dev": "./out/replicache.dev.js"
59-
}
51+
"out/replicache.js",
52+
"out/replicache.mjs",
53+
"out/replicache.wasm",
54+
"out/replicache.dev.wasm"
55+
]
6056
}

perf/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "module"
3+
}

perf/perf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
/* eslint-env browser, es2020 */
55

6-
import {Replicache} from '../out/replicache.js';
6+
import {Replicache} from '../out/replicache.mjs';
77

88
console.assert = console.debug = console.error = console.info = console.log = console.warn = () =>
99
void 0;

rollup.config.js

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,42 @@
11
import typescript from '@wessberg/rollup-plugin-ts';
22
import replace from '@rollup/plugin-replace';
3-
import alias from '@rollup/plugin-alias';
43
import copy from 'rollup-plugin-copy';
54

65
/* eslint-env node */
76

8-
const dev = !!process.env.DEV;
9-
const part = dev ? '.dev' : '';
10-
const variant = dev ? 'debug' : 'release';
7+
const dir = 'out';
8+
const cjs = process.env.CJS !== undefined;
9+
const format = cjs ? 'cjs' : 'es';
10+
const ext = cjs ? 'js' : 'mjs';
1111

1212
export default {
1313
input: 'src/mod.ts',
1414
output: {
15-
file: `out/replicache${part}.js`,
16-
format: 'esm',
15+
file: `./${dir}/replicache.${ext}`,
16+
format,
1717
},
1818
plugins: [
19-
// When building dev version use wasm/debug/ instead of wasm/relese/
20-
alias({
21-
entries: [
22-
{
23-
find: /wasm\/release\/replicache_client\.js$/,
24-
replacement: `wasm/${variant}/replicache_client.js`,
25-
},
26-
],
27-
}),
28-
29-
// Use replicache.wasm in same directory as out/replicache.js
19+
// Use replicache.wasm in same directory as replicache.js
3020
replace({
31-
['./wasm/release/replicache_client_bg.wasm']: `'./replicache${part}.wasm'`,
21+
['./wasm/release/replicache_client_bg.wasm']: `'./replicache.wasm'`,
3222
delimiters: [`'`, `'`],
3323
include: 'src/repm-invoker.ts',
3424
}),
3525

3626
typescript(),
3727

38-
// Copy wasm file to out directory
28+
// Copy wasm files to out directory
3929
copy({
4030
targets: [
4131
{
42-
src: `src/wasm/${variant}/replicache_client_bg.wasm`,
43-
dest: `out`,
44-
rename: `replicache${part}.wasm`,
32+
src: `src/wasm/release/replicache_client_bg.wasm`,
33+
dest: `./${dir}/`,
34+
rename: `replicache.wasm`,
35+
},
36+
{
37+
src: `src/wasm/debug/replicache_client_bg.wasm`,
38+
dest: `./${dir}/`,
39+
rename: `replicache.dev.wasm`,
4540
},
4641
],
4742
}),

src/replicache.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1483,7 +1483,7 @@ testWithBothStores('onSync', async () => {
14831483
patch: [],
14841484
});
14851485
rep.pull();
1486-
await tickAFewTimes(10);
1486+
await tickAFewTimes(15);
14871487

14881488
expect(onSync.callCount).to.eq(2);
14891489
expect(onSync.getCall(0).args[0]).to.be.true;

0 commit comments

Comments
 (0)