Skip to content

Commit

Permalink
Allow ESM files to be used in Node.js (#10479)
Browse files Browse the repository at this point in the history
Allow ESM files to be used in Node.js
  • Loading branch information
benmccann committed Jul 30, 2022
1 parent 844270e commit 6feb48b
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 31 deletions.
5 changes: 0 additions & 5 deletions auto/auto.esm.js

This file was deleted.

5 changes: 5 additions & 0 deletions auto/auto.mjs
@@ -0,0 +1,5 @@
import {Chart, registerables} from '../dist/chart.mjs';

Chart.register(...registerables);

export default Chart;
File renamed without changes.
4 changes: 2 additions & 2 deletions auto/package.json
Expand Up @@ -3,6 +3,6 @@
"private": true,
"description": "auto registering package",
"main": "auto.js",
"module": "auto.esm.js",
"types": "auto.esm.d.ts"
"module": "auto.mjs",
"types": "auto.mts"
}
2 changes: 1 addition & 1 deletion docs/.vuepress/config.js
Expand Up @@ -94,7 +94,7 @@ module.exports = {
config.merge({
resolve: {
alias: {
'chart.js': path.resolve(__dirname, '../../dist/chart.esm.js'),
'chart.js': path.resolve(__dirname, '../../dist/chart.mjs'),
}
}
})
Expand Down
2 changes: 1 addition & 1 deletion docs/scripts/components.js
@@ -1,3 +1,3 @@
// Add Chart components needed in samples here.
// Usable through `components[name]`.
export {Tooltip} from '../../dist/chart.esm';
export {Tooltip} from '../../dist/chart.mjs';
3 changes: 1 addition & 2 deletions docs/scripts/helpers.js
@@ -1,4 +1,3 @@
// Add helpers needed in samples here.
// Usable through `helpers[name]`.
export {color, getHoverColor, easingEffects} from '../../dist/helpers.esm';

export {color, getHoverColor, easingEffects} from '../../dist/helpers.mjs';
2 changes: 1 addition & 1 deletion docs/scripts/register.js
@@ -1,4 +1,4 @@
import {Chart, registerables} from '../../dist/chart.esm';
import {Chart, registerables} from '../../dist/chart.mjs';
import Log2Axis from './log2';
import './derived-bubble';
import analyzer from './analyzer';
Expand Down
2 changes: 1 addition & 1 deletion docs/scripts/utils.js
@@ -1,7 +1,7 @@
import colorLib from '@kurkle/color';
import {DateTime} from 'luxon';
import 'chartjs-adapter-luxon';
import {valueOrDefault} from '../../dist/helpers.esm';
import {valueOrDefault} from '../../dist/helpers.mjs';

// Adapted from http://indiegamr.com/generate-repeatable-random-numbers-in-js/
var _seed = Date.now();
Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions helpers/package.json
Expand Up @@ -3,6 +3,6 @@
"private": true,
"description": "helper package",
"main": "helpers.js",
"module": "helpers.esm.js",
"types": "helpers.esm.d.ts"
"module": "helpers.mjs",
"types": "helpers.mts"
}
16 changes: 5 additions & 11 deletions package.json
Expand Up @@ -7,7 +7,7 @@
"jsdelivr": "dist/chart.min.js",
"unpkg": "dist/chart.min.js",
"main": "dist/chart.js",
"module": "dist/chart.esm.js",
"module": "dist/chart.mjs",
"types": "types/index.esm.d.ts",
"keywords": [
"canvas",
Expand All @@ -25,16 +25,10 @@
"url": "https://github.com/chartjs/Chart.js/issues"
},
"files": [
"auto/package.json",
"auto/**/*.js",
"auto/**/*.d.ts",
"dist/*.js",
"dist/chunks/*.js",
"types/*.d.ts",
"types/helpers/*.d.ts",
"helpers/package.json",
"helpers/**/*.js",
"helpers/**/*.d.ts"
"auto/**",
"dist/**",
"types/**",
"helpers/**"
],
"scripts": {
"autobuild": "rollup -c -w",
Expand Down
34 changes: 29 additions & 5 deletions rollup.config.js
Expand Up @@ -6,10 +6,6 @@ const terser = require('rollup-plugin-terser').terser;
const pkg = require('./package.json');

const input = 'src/index.js';
const inputESM = {
'dist/chart.esm': 'src/index.esm.js',
'dist/helpers.esm': 'src/helpers/index.js'
};

const banner = `/*!
* Chart.js v${pkg.version}
Expand Down Expand Up @@ -60,10 +56,38 @@ module.exports = [
},

// ES6 builds
// dist/chart.mjs
// helpers/*.js
{
input: {
'dist/chart': 'src/index.esm.js',
'dist/helpers': 'src/helpers/index.js'
},
plugins: [
json(),
resolve(),
cleanup({
sourcemap: true
}),
],
output: {
dir: './',
chunkFileNames: 'dist/chunks/[name].mjs',
entryFileNames: '[name].mjs',
banner,
format: 'esm',
indent: false,
},
},

// Legacy ES6 builds for backwards compatibility. Remove for Chart.js 4.0
// dist/chart.esm.js
// helpers/*.js
{
input: inputESM,
input: {
'dist/chart.esm': 'src/index.esm.js',
'dist/helpers.esm': 'src/helpers/index.js'
},
plugins: [
json(),
resolve(),
Expand Down

0 comments on commit 6feb48b

Please sign in to comment.