Skip to content

Commit 02d561b

Browse files
authored
Add an ESM build (#414)
1 parent 8194ad6 commit 02d561b

File tree

8 files changed

+69
-70
lines changed

8 files changed

+69
-70
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,3 @@ coverage/*
99

1010
nbproject/*
1111
dist/
12-
chartjs-plugin-zoom.js
13-
chartjs-plugin-zoom.min.js

gulpfile.js

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ var inquirer = require('inquirer');
1010
var semver = require('semver');
1111
var path = require('path');
1212
var fs = require('fs');
13-
var {exec} = require('child_process');
1413
var karma = require('karma');
1514
var merge = require('merge-stream');
1615
var yargs = require('yargs');
@@ -24,17 +23,6 @@ var srcDir = './src/';
2423
var srcFiles = srcDir + '**.js';
2524
var outDir = './dist/';
2625

27-
function run(bin, args, done) {
28-
var exe = '"' + process.execPath + '"';
29-
var src = require.resolve(bin);
30-
var ps = exec([exe, src].concat(args || []).join(' '));
31-
32-
ps.stdout.pipe(process.stdout);
33-
ps.stderr.pipe(process.stderr);
34-
ps.on('close', () => done());
35-
}
36-
37-
gulp.task('build', gulp.series(rollupTask, copyDistFilesTask));
3826
gulp.task('package', packageTask);
3927
gulp.task('bump', bumpTask);
4028
gulp.task('lint-html', lintHtmlTask);
@@ -43,32 +31,23 @@ gulp.task('lint', gulp.parallel('lint-html', 'lint-js'));
4331
gulp.task('unittest', unittestTask);
4432
gulp.task('test', gulp.parallel('lint', 'unittest'));
4533
gulp.task('watch', watchTask);
46-
gulp.task('default', gulp.parallel('lint', 'build', 'watch'));
47-
48-
function rollupTask(done) {
49-
run('rollup/dist/bin/rollup', ['-c'], done);
50-
}
34+
gulp.task('default', gulp.parallel('lint', 'watch'));
5135

52-
/**
53-
* Copy the files from `/dist` to the root directory.
54-
* @todo remove at version 1.0
36+
/*
37+
* Create a zip file for distribution from the project's GitHub Releases page
5538
*/
56-
function copyDistFilesTask() {
57-
return gulp.src(outDir + '*.js')
58-
.pipe(gulp.dest('./'));
59-
}
60-
6139
function packageTask() {
6240
return merge(
63-
// gather "regular" files landing in the package root
41+
// Gather "regular" files from dist. We don't pass a `base` prop, so gulp will
42+
// put these in the top level of the zip archive.
6443
gulp.src([outDir + '*.js', 'LICENSE.md']),
6544

66-
// since we moved the dist files one folder up (package root), we need to rewrite
45+
// Since we move the dist files one folder up in the archive, we need to rewrite
6746
// samples src="../dist/ to src="../ and then copy them in the /samples directory.
6847
gulp.src('./samples/**/*', {base: '.'})
6948
.pipe(streamify(replace(/src="((?:\.\.\/)+)dist\//g, 'src="$1')))
7049
)
71-
// finally, create the zip archive
50+
// Finally, create the zip archive.
7251
.pipe(zip(pkg.name + '.zip'))
7352
.pipe(gulp.dest(outDir));
7453
}
@@ -151,5 +130,5 @@ function unittestTask(done) {
151130
}
152131

153132
function watchTask() {
154-
return gulp.watch(srcFiles, gulp.parallel('lint', 'build'));
133+
return gulp.watch(srcFiles, gulp.parallel('lint'));
155134
}

karma.conf.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ module.exports = function(karma) {
4949
// {pattern: 'test/fixtures/**/*.png', included: false},
5050
'node_modules/chart.js/dist/chart.js',
5151
'test/index.js',
52-
'src/plugin.js'
52+
'src/index.js'
5353
].concat(args.inputs),
5454

5555
preprocessors: {
5656
// 'test/fixtures/**/*.js': ['fixtures'],
5757
'test/specs/**/*.js': ['rollup'],
5858
'test/index.js': ['rollup'],
59-
'src/plugin.js': ['sources']
59+
'src/index.js': ['sources']
6060
},
6161

6262
rollupPreprocessor: {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
"jsdelivr": "dist/chartjs-plugin-zoom.min.js",
77
"unpkg": "dist/chartjs-plugin-zoom.min.js",
88
"main": "dist/chartjs-plugin-zoom.js",
9+
"module": "dist/chartjs-plugin-zoom.esm.js",
910
"repository": {
1011
"type": "git",
1112
"url": "https://github.com/chartjs/chartjs-plugin-zoom.git"
1213
},
1314
"scripts": {
14-
"build": "gulp build",
15+
"build": "rollup -c",
1516
"test": "gulp test"
1617
},
1718
"devDependencies": {

rollup.config.js

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
const commonjs = require('rollup-plugin-commonjs');
22
const nodeResolve = require('rollup-plugin-node-resolve');
33
const terser = require('rollup-plugin-terser').terser;
4+
45
const pkg = require('./package.json');
5-
const dependencies = Object.keys(pkg.dependencies)
6-
const peerDependencies = Object.keys(pkg.peerDependencies)
7-
const allDependencies = dependencies.concat(peerDependencies)
6+
const dependencies = Object.keys(pkg.dependencies);
7+
const peerDependencies = Object.keys(pkg.peerDependencies);
8+
const allDependencies = dependencies.concat(peerDependencies);
89

910
const banner = `/*!
1011
* @license
@@ -17,19 +18,24 @@ const banner = `/*!
1718
* https://github.com/chartjs/${pkg.name}/blob/master/LICENSE.md
1819
*/`;
1920

21+
const name = 'ChartZoom';
22+
const globals = {
23+
'chart.js': 'Chart',
24+
'chart.js/helpers': 'Chart.helpers',
25+
hammerjs: 'Hammer'
26+
};
27+
allDependencies.push('chart.js/helpers');
28+
2029
module.exports = [
2130
{
22-
input: 'src/plugin.js',
31+
input: 'src/index.js',
2332
output: {
24-
name: 'ChartZoom',
33+
name,
2534
file: `dist/${pkg.name}.js`,
26-
banner: banner,
35+
banner,
2736
format: 'umd',
2837
indent: false,
29-
globals: {
30-
'chart.js': 'Chart',
31-
hammerjs: 'Hammer'
32-
}
38+
globals
3339
},
3440
plugins: [
3541
commonjs({
@@ -40,17 +46,14 @@ module.exports = [
4046
external: allDependencies
4147
},
4248
{
43-
input: 'src/plugin.js',
49+
input: 'src/index.js',
4450
output: {
45-
name: 'ChartZoom',
51+
name,
4652
file: `dist/${pkg.name}.min.js`,
47-
banner: banner,
53+
banner,
4854
format: 'umd',
4955
indent: false,
50-
globals: {
51-
'chart.js': 'Chart',
52-
hammerjs: 'Hammer'
53-
}
56+
globals
5457
},
5558
plugins: [
5659
commonjs({
@@ -60,5 +63,19 @@ module.exports = [
6063
terser({output: {comments: 'some'}})
6164
],
6265
external: allDependencies
63-
}
66+
},
67+
{
68+
input: 'src/index.esm.js',
69+
plugins: [
70+
nodeResolve()
71+
],
72+
output: {
73+
name,
74+
file: `dist/${pkg.name}.esm.js`,
75+
banner,
76+
format: 'esm',
77+
indent: false
78+
},
79+
external: allDependencies
80+
},
6481
];

src/index.esm.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {default as Zoom} from './plugin';

src/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import {Chart} from 'chart.js';
2+
import Zoom from './plugin';
3+
4+
Chart.register(Zoom);
5+
6+
export default Zoom;

src/plugin.js

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
'use strict';
22

3-
import Chart from 'chart.js';
3+
import {clone, each, isNullOrUndef, merge} from 'chart.js/helpers';
44
import Hammer from 'hammerjs';
55

6-
var helpers = Chart.helpers;
7-
8-
// Take the zoom namespace of Chart
9-
var zoomNS = Chart.Zoom = Chart.Zoom || {};
6+
// Zoom namespace (kept under Chart prior to Chart.js 3)
7+
var zoomNS = {};
108

119
// Where we store functions to handle different scale types
1210
var zoomFunctions = zoomNS.zoomFunctions = zoomNS.zoomFunctions || {};
@@ -21,7 +19,7 @@ function resolveOptions(chart, options) {
2119
deprecatedOptions.zoom = chart.options.zoom;
2220
}
2321
var props = chart.$zoom;
24-
options = props._options = helpers.merge({}, [options, deprecatedOptions]);
22+
options = props._options = merge({}, [options, deprecatedOptions]);
2523

2624
// Install listeners. Do this dynamically based on options so that we can turn zoom on and off
2725
// We also want to make sure listeners aren't always on. E.g. if you're scrolling down a page
@@ -46,12 +44,12 @@ function resolveOptions(chart, options) {
4644

4745
function storeOriginalOptions(chart) {
4846
var originalOptions = chart.$zoom._originalOptions;
49-
helpers.each(chart.scales, function(scale) {
47+
each(chart.scales, function(scale) {
5048
if (!originalOptions[scale.id]) {
51-
originalOptions[scale.id] = helpers.clone(scale.options);
49+
originalOptions[scale.id] = clone(scale.options);
5250
}
5351
});
54-
helpers.each(originalOptions, function(opt, key) {
52+
each(originalOptions, function(opt, key) {
5553
if (!chart.scales[key]) {
5654
delete originalOptions[key];
5755
}
@@ -77,7 +75,7 @@ function directionEnabled(mode, dir, chart) {
7775

7876
function rangeMaxLimiter(zoomPanOptions, newMax) {
7977
if (zoomPanOptions.scaleAxes && zoomPanOptions.rangeMax &&
80-
!helpers.isNullOrUndef(zoomPanOptions.rangeMax[zoomPanOptions.scaleAxes])) {
78+
!isNullOrUndef(zoomPanOptions.rangeMax[zoomPanOptions.scaleAxes])) {
8179
var rangeMax = zoomPanOptions.rangeMax[zoomPanOptions.scaleAxes];
8280
if (newMax > rangeMax) {
8381
newMax = rangeMax;
@@ -88,7 +86,7 @@ function rangeMaxLimiter(zoomPanOptions, newMax) {
8886

8987
function rangeMinLimiter(zoomPanOptions, newMin) {
9088
if (zoomPanOptions.scaleAxes && zoomPanOptions.rangeMin &&
91-
!helpers.isNullOrUndef(zoomPanOptions.rangeMin[zoomPanOptions.scaleAxes])) {
89+
!isNullOrUndef(zoomPanOptions.rangeMin[zoomPanOptions.scaleAxes])) {
9290
var rangeMin = zoomPanOptions.rangeMin[zoomPanOptions.scaleAxes];
9391
if (newMin < rangeMin) {
9492
newMin = rangeMin;
@@ -183,7 +181,7 @@ function doZoom(chart, percentZoomX, percentZoomY, focalPoint, whichAxes, animat
183181
// Do the zoom here
184182
var zoomMode = typeof zoomOptions.mode === 'function' ? zoomOptions.mode({chart: chart}) : zoomOptions.mode;
185183

186-
// Which axe should be modified when figers were used.
184+
// Which axes should be modified when fingers were used.
187185
var _whichAxes;
188186
if (zoomMode === 'xy' && whichAxes !== undefined) {
189187
// based on fingers positions
@@ -193,7 +191,7 @@ function doZoom(chart, percentZoomX, percentZoomY, focalPoint, whichAxes, animat
193191
_whichAxes = 'xy';
194192
}
195193

196-
helpers.each(chart.scales, function(scale) {
194+
each(chart.scales, function(scale) {
197195
if (scale.isHorizontal() && directionEnabled(zoomMode, 'x', chart) && directionEnabled(_whichAxes, 'x', chart)) {
198196
zoomOptions.scaleAxes = 'x';
199197
zoomScale(scale, percentZoomX, focalPoint, zoomOptions);
@@ -254,11 +252,11 @@ function panNumericalScale(scale, delta, panOptions) {
254252
var diff;
255253

256254
if (panOptions.scaleAxes && panOptions.rangeMin &&
257-
!helpers.isNullOrUndef(panOptions.rangeMin[panOptions.scaleAxes])) {
255+
!isNullOrUndef(panOptions.rangeMin[panOptions.scaleAxes])) {
258256
rangeMin = panOptions.rangeMin[panOptions.scaleAxes];
259257
}
260258
if (panOptions.scaleAxes && panOptions.rangeMax &&
261-
!helpers.isNullOrUndef(panOptions.rangeMax[panOptions.scaleAxes])) {
259+
!isNullOrUndef(panOptions.rangeMax[panOptions.scaleAxes])) {
262260
rangeMax = panOptions.rangeMax[panOptions.scaleAxes];
263261
}
264262

@@ -289,7 +287,7 @@ function doPan(chartInstance, deltaX, deltaY) {
289287
if (panOptions.enabled) {
290288
var panMode = typeof panOptions.mode === 'function' ? panOptions.mode({chart: chartInstance}) : panOptions.mode;
291289

292-
helpers.each(chartInstance.scales, function(scale) {
290+
each(chartInstance.scales, function(scale) {
293291
if (scale.isHorizontal() && directionEnabled(panMode, 'x', chartInstance) && deltaX !== 0) {
294292
panOptions.scaleAxes = 'x';
295293
panScale(scale, deltaX, panOptions);
@@ -580,7 +578,7 @@ var zoomPlugin = {
580578
chartInstance.resetZoom = function() {
581579
storeOriginalOptions(chartInstance);
582580
var originalOptions = chartInstance.$zoom._originalOptions;
583-
helpers.each(chartInstance.scales, function(scale) {
581+
each(chartInstance.scales, function(scale) {
584582

585583
var scaleOptions = scale.options;
586584
if (originalOptions[scale.id]) {
@@ -671,5 +669,4 @@ var zoomPlugin = {
671669
}
672670
};
673671

674-
Chart.register(zoomPlugin);
675672
export default zoomPlugin;

0 commit comments

Comments
 (0)