Skip to content

Commit

Permalink
Generate CommonJS files (#327)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
arv authored Mar 24, 2021
1 parent 5469151 commit ba88bd8
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 44 deletions.
6 changes: 2 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
coverage
node_modules
out
build
.DS_Store
repc
src/wasm
docs
docs
out
28 changes: 12 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@
"check-format": "prettier --check '{src,sample,perf}/**/*.{js,jsx,json,ts,tsx,html,css,md}' '*.{js,jsx,json,ts,tsx,html,css,md}'",
"lint": "eslint --ext .ts,.tsx,.js,.jsx src/ perf/",
"doc": "typedoc src/mod.ts",
"build": "rollup --config rollup.config.js",
"build-dev": "DEV=1 rollup --config rollup.config.js",
"build": "rollup --config rollup.config.js && CJS=1 rollup --config rollup.config.js ",
"build:watch": "rollup --config rollup.config.js --watch",
"postinstall": "rm -f node_modules/fetch-mock/esm/client.d.ts && tool/get-deps.sh",
"prepublishOnly": "npm run lint && npm run get-repc && npm run test && rm -rf out && npm run build && npm run build-dev",
"postinstall": "rm -f node_modules/fetch-mock/esm/client.d.ts",
"prepublishOnly": "npm run lint && tool/get-deps.sh && npm run test && rm -rf out && npm run build",
"prepack": "npm run prepublishOnly",
"perf": "node perf/runner.js",
"get-repc": "tool/get-deps.sh"
"prepare": "tool/get-deps.sh",
"perf": "node perf/runner.js"
},
"devDependencies": {
"@esm-bundle/chai": "^4.3.0",
Expand Down Expand Up @@ -46,15 +45,12 @@
"typedoc-plugin-sourcefile-url": "^1.0.6",
"typescript": "^4.2.3"
},
"type": "module",
"module": "out/replicache.js",
"types": "out/replicache.js",
"main": "out/replicache.js",
"types": "out/replicache.d.js",
"files": [
"out",
"tool/get-deps.sh"
],
"exports": {
".": "./out/replicache.js",
"./dev": "./out/replicache.dev.js"
}
"out/replicache.js",
"out/replicache.mjs",
"out/replicache.wasm",
"out/replicache.dev.wasm"
]
}
3 changes: 3 additions & 0 deletions perf/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
2 changes: 1 addition & 1 deletion perf/perf.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/* eslint-env browser, es2020 */

import {Replicache} from '../out/replicache.js';
import {Replicache} from '../out/replicache.mjs';

console.assert = console.debug = console.error = console.info = console.log = console.warn = () =>
void 0;
Expand Down
39 changes: 17 additions & 22 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,42 @@
import typescript from '@wessberg/rollup-plugin-ts';
import replace from '@rollup/plugin-replace';
import alias from '@rollup/plugin-alias';
import copy from 'rollup-plugin-copy';

/* eslint-env node */

const dev = !!process.env.DEV;
const part = dev ? '.dev' : '';
const variant = dev ? 'debug' : 'release';
const dir = 'out';
const cjs = process.env.CJS !== undefined;
const format = cjs ? 'cjs' : 'es';
const ext = cjs ? 'js' : 'mjs';

export default {
input: 'src/mod.ts',
output: {
file: `out/replicache${part}.js`,
format: 'esm',
file: `./${dir}/replicache.${ext}`,
format,
},
plugins: [
// When building dev version use wasm/debug/ instead of wasm/relese/
alias({
entries: [
{
find: /wasm\/release\/replicache_client\.js$/,
replacement: `wasm/${variant}/replicache_client.js`,
},
],
}),

// Use replicache.wasm in same directory as out/replicache.js
// Use replicache.wasm in same directory as replicache.js
replace({
['./wasm/release/replicache_client_bg.wasm']: `'./replicache${part}.wasm'`,
['./wasm/release/replicache_client_bg.wasm']: `'./replicache.wasm'`,
delimiters: [`'`, `'`],
include: 'src/repm-invoker.ts',
}),

typescript(),

// Copy wasm file to out directory
// Copy wasm files to out directory
copy({
targets: [
{
src: `src/wasm/${variant}/replicache_client_bg.wasm`,
dest: `out`,
rename: `replicache${part}.wasm`,
src: `src/wasm/release/replicache_client_bg.wasm`,
dest: `./${dir}/`,
rename: `replicache.wasm`,
},
{
src: `src/wasm/debug/replicache_client_bg.wasm`,
dest: `./${dir}/`,
rename: `replicache.dev.wasm`,
},
],
}),
Expand Down
2 changes: 1 addition & 1 deletion src/replicache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1483,7 +1483,7 @@ testWithBothStores('onSync', async () => {
patch: [],
});
rep.pull();
await tickAFewTimes(10);
await tickAFewTimes(15);

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

0 comments on commit ba88bd8

Please sign in to comment.