Skip to content

Commit

Permalink
fix: inject package.json through custom rollup plugin. remove erroneo…
Browse files Browse the repository at this point in the history
…us yarn doc command.
  • Loading branch information
yusinto committed Sep 21, 2023
1 parent 4200bc8 commit 1410bf6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
6 changes: 4 additions & 2 deletions packages/sdk/cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"prettier": "prettier --write '**/*.@(js|ts|tsx|json|css)' --ignore-path ../../../.prettierignore",
"test": "NODE_OPTIONS=\"--experimental-vm-modules --no-warnings\" jest --ci --runInBand",
"coverage": "yarn test --coverage",
"check": "yarn prettier && yarn lint && yarn build && yarn test && yarn doc"
"check": "yarn prettier && yarn lint && yarn build && yarn test"
},
"dependencies": {
"@cloudflare/workers-types": "^4.20230321.0",
Expand All @@ -63,6 +63,7 @@
"eslint-plugin-prettier": "^5.0.0",
"jest": "^29.5.0",
"launchdarkly-js-test-helpers": "^2.2.0",
"load-json-file": "^7.0.1",
"miniflare": "^2.13.0",
"prettier": "^3.0.0",
"rimraf": "^5.0.1",
Expand All @@ -73,6 +74,7 @@
"rollup-plugin-generate-package-json": "^3.2.0",
"ts-jest": "^29.1.0",
"typedoc": "0.25.0",
"typescript": "5.1.6"
"typescript": "5.1.6",
"write-json-file": "^5.0.0"
}
}
56 changes: 40 additions & 16 deletions packages/sdk/cloudflare/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,74 @@ import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import resolve from '@rollup/plugin-node-resolve';
import terser from '@rollup/plugin-terser';
import { loadJsonFileSync } from 'load-json-file';
import { OutputOptions } from 'rollup';
import dts from 'rollup-plugin-dts';
import esbuild from 'rollup-plugin-esbuild';
import filesize from 'rollup-plugin-filesize';
import generatePackageJson from 'rollup-plugin-generate-package-json';
import { writeJsonFileSync } from 'write-json-file';

type PackageType = 'commonjs' | 'module';
const basePlugins = [resolve(), commonjs(), esbuild(), json(), terser(), filesize()];
const generatePlugins = (type: PackageType) =>
basePlugins.concat([
generatePackageJson({
baseContents: ({ name, version }: any) => ({
const cjsIndex = 'dist/cjs/src/index.js';
const cjsPackageJson = 'dist/cjs/package.json';
const esmIndex = 'dist/esm/src/index.js';
const esmPackageJson = 'dist/esm/package.json';

function injectPackageJson() {
return {
name: 'inject-package-json',
generateBundle({ format }: OutputOptions) {
const { name, version } = loadJsonFileSync('package.json') as any;
const minimalPackageJson = {
name,
version,
type,
}),
outputFolder: type === 'commonjs' ? 'dist/cjs' : 'dist/esm',
}),
]);
type: format === 'cjs' ? 'commonjs' : 'module',
};

const packageJsonPath = format === 'cjs' ? cjsPackageJson : esmPackageJson;
writeJsonFileSync(packageJsonPath, minimalPackageJson, {
indent: 2,
});
},
};
}

const plugins = [
resolve(),
commonjs(),
esbuild(),
json(),
terser(),
filesize(),
injectPackageJson(),
];

// the second array item is a function to include all js-core packages in the bundle so they
// are not imported or required as separate npm packages
const external = [/node_modules/, (id: string) => !id.includes('js-core')];

export default [
{
input: 'src/index.ts',
output: [
{
file: 'dist/cjs/src/index.js',
file: cjsIndex,
format: 'cjs',
sourcemap: true,
},
],
plugins: generatePlugins('commonjs'),
plugins,
external,
},
{
input: 'src/index.ts',
output: [
{
file: 'dist/esm/src/index.js',
file: esmIndex,
format: 'esm',
sourcemap: true,
},
],
plugins: generatePlugins('module'),
plugins,
external,
},
{
Expand Down

0 comments on commit 1410bf6

Please sign in to comment.