From: Dan Onoshko Date: Thu, 15 Dec 2022 23:11:11 +0000 (+0400) Subject: feat: restore commonjs bundle (#10984) X-Git-Tag: v4.1.0~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=35fd6204b98e9f5f55c83440ac70ec3d4757b880;p=thirdparty%2FChart.js.git feat: restore commonjs bundle (#10984) --- diff --git a/auto/auto.cjs b/auto/auto.cjs new file mode 100644 index 000000000..4a4590a4b --- /dev/null +++ b/auto/auto.cjs @@ -0,0 +1,6 @@ +const exports = require('../dist/chart.cjs'); +const {Chart, registerables} = exports; + +Chart.register(...registerables); + +module.exports = Object.assign(Chart, exports); diff --git a/helpers/helpers.cjs b/helpers/helpers.cjs new file mode 100644 index 000000000..d47684818 --- /dev/null +++ b/helpers/helpers.cjs @@ -0,0 +1 @@ +module.exports = require('../dist/helpers.cjs'); diff --git a/package.json b/package.json index 4c5ccae6f..cac1fb077 100644 --- a/package.json +++ b/package.json @@ -15,15 +15,18 @@ "exports": { ".": { "types": "./dist/types.d.ts", - "import": "./dist/chart.js" + "import": "./dist/chart.js", + "require": "./dist/chart.cjs" }, "./auto": { "types": "./auto/auto.d.ts", - "import": "./auto/auto.js" + "import": "./auto/auto.js", + "require": "./auto/auto.cjs" }, "./helpers": { "types": "./helpers/helpers.d.ts", - "import": "./helpers/helpers.js" + "import": "./helpers/helpers.js", + "require": "./helpers/helpers.cjs" } }, "types": "./dist/types.d.ts", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1f461b883..65dae2628 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -152,6 +152,12 @@ importers: dependencies: chart.js: link:../../.. + test/integration/node-commonjs: + specifiers: + chart.js: workspace:* + dependencies: + chart.js: link:../../.. + test/integration/react-browser: specifiers: '@babel/core': ^7.0.0 diff --git a/rollup.config.js b/rollup.config.js index d3e6fc54c..c6a775310 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -77,5 +77,26 @@ export default [ indent: false, sourcemap: true, }, + }, + + // CommonJS builds + // dist/chart.js + // helpers/*.js + { + input: { + 'dist/chart': 'src/index.ts', + 'dist/helpers': 'src/helpers/index.ts' + }, + plugins: plugins(), + external: _ => (/node_modules/).test(_), + output: { + dir: './', + chunkFileNames: 'dist/chunks/[name].cjs', + entryFileNames: '[name].cjs', + banner, + format: 'commonjs', + indent: false, + sourcemap: true, + }, } ]; diff --git a/test/integration/node-commonjs/package.json b/test/integration/node-commonjs/package.json new file mode 100644 index 000000000..19c9b2c0c --- /dev/null +++ b/test/integration/node-commonjs/package.json @@ -0,0 +1,10 @@ +{ + "private": true, + "description": "chart.js should work in Node", + "scripts": { + "test": "node test.js" + }, + "dependencies": { + "chart.js": "workspace:*" + } +} diff --git a/test/integration/node-commonjs/test.js b/test/integration/node-commonjs/test.js new file mode 100644 index 000000000..1d43d2d23 --- /dev/null +++ b/test/integration/node-commonjs/test.js @@ -0,0 +1,7 @@ +const {Chart} = require('chart.js'); +const {valueOrDefault} = require('chart.js/helpers'); + +Chart.register({ + id: 'TEST_PLUGIN', + dummyValue: valueOrDefault(0, 1) +}); diff --git a/test/integration/node/test.cjs b/test/integration/node/test.cjs index 94f86f221..1d43d2d23 100644 --- a/test/integration/node/test.cjs +++ b/test/integration/node/test.cjs @@ -1,10 +1,7 @@ -/* eslint-disable es/no-dynamic-import */ -Promise.all([ - import('chart.js'), - import('chart.js/helpers') -]).then(([{Chart}, {valueOrDefault}]) => { - Chart.register({ - id: 'TEST_PLUGIN', - dummyValue: valueOrDefault(0, 1) - }); +const {Chart} = require('chart.js'); +const {valueOrDefault} = require('chart.js/helpers'); + +Chart.register({ + id: 'TEST_PLUGIN', + dummyValue: valueOrDefault(0, 1) });