Skip to content

Commit

Permalink
Add cjs bundle output for @powersync/common.
Browse files Browse the repository at this point in the history
  • Loading branch information
rkistner committed Sep 12, 2024
1 parent 7e900cb commit e007acc
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 14 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
node_modules
lib
dist
tsconfig.tsbuildinfo
cjs
*.tsbuildinfo
.vscode
.DS_STORE
.idea
Expand Down
17 changes: 13 additions & 4 deletions packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@
"access": "public"
},
"description": "API definitions for JourneyApps PowerSync",
"main": "dist/index.js",
"types": "lib/index.d.ts",
"type": "module",
"main": "dist/bundle.cjs",
"module": "dist/bundle.mjs",
"types": "lib/index.d.ts",
"exports": {
".": {
"import": "./dist/bundle.mjs",
"require": "./dist/bundle.cjs",
"default": "./dist/bundle.mjs",
"types": "./lib/index.d.ts"
}
},
"author": "JOURNEYAPPS",
"license": "Apache-2.0",
"files": [
Expand All @@ -24,8 +33,8 @@
},
"homepage": "https://docs.powersync.com",
"scripts": {
"build": "tsc -b && rollup -c rollup.config.mjs",
"clean": "rm -rf lib tsconfig.tsbuildinfo dist",
"build": "tsc -b && rollup -c rollup.config.mjs && rollup -c rollup-cjs.config.mjs",
"clean": "rm -rf lib dist tsconfig.tsbuildinfo",
"test": "vitest"
},
"dependencies": {
Expand Down
41 changes: 41 additions & 0 deletions packages/common/rollup-cjs.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import commonjs from '@rollup/plugin-commonjs';
import inject from '@rollup/plugin-inject';
import json from '@rollup/plugin-json';
import nodeResolve from '@rollup/plugin-node-resolve';
import terser from '@rollup/plugin-terser';

export default (commandLineArgs) => {
const sourcemap = (commandLineArgs.sourceMap || 'true') == 'true';

// Clears rollup CLI warning https://github.com/rollup/rollup/issues/2694
delete commandLineArgs.sourceMap;

return {
// esm input is better, even for cjs output
input: 'lib/index.js',
output: {
file: 'dist/bundle.cjs',
format: 'cjs',
sourcemap: sourcemap
},
plugins: [
json(),
nodeResolve({ preferBuiltins: false, browser: true }),
commonjs({}),
inject({
Buffer: ['buffer', 'Buffer'],
ReadableStream: ['web-streams-polyfill/ponyfill', 'ReadableStream'],
// Used by can-ndjson-stream
TextDecoder: ['text-encoding', 'TextDecoder']
}),
terser()
],
// This makes life easier
external: [
// This has dynamic logic - makes bundling hard
'cross-fetch',
// TODO: make the useDefaults logic better. Currently need access to this package directly
'js-logger'
]
};
};
2 changes: 1 addition & 1 deletion packages/common/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default (commandLineArgs) => {
return {
input: 'lib/index.js',
output: {
file: 'dist/index.js',
file: 'dist/bundle.mjs',
format: 'esm',
sourcemap: sourcemap
},
Expand Down
7 changes: 0 additions & 7 deletions packages/common/tsconfig-cjs.json

This file was deleted.

2 changes: 1 addition & 1 deletion packages/common/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"jsx": "react",
"types": ["node"],
"rootDir": "src",
"outDir": "./lib/esm",
"outDir": "./lib",
"lib": ["esnext"],
"declaration": true,
"module": "node16",
Expand Down

0 comments on commit e007acc

Please sign in to comment.