diff --git a/docs/axes/cartesian/time.md b/docs/axes/cartesian/time.md index c2d90abad68..939d6dd434e 100644 --- a/docs/axes/cartesian/time.md +++ b/docs/axes/cartesian/time.md @@ -4,7 +4,7 @@ The time scale is used to display times and dates. When building its ticks, it w ## Date Adapters -The time scale requires both a date library and corresponding adapter to be present. By default, Chart.js includes an adapter for Moment.js. You may wish to choose from [other available adapters](https://github.com/chartjs/awesome#adapters) instead. +The time scale **requires** both a date library and corresponding adapter to be present. Please choose from the [available adapters](https://github.com/chartjs/awesome#adapters). ## Data Sets diff --git a/docs/getting-started/integration.md b/docs/getting-started/integration.md index 049e23a56b2..257607fa986 100644 --- a/docs/getting-started/integration.md +++ b/docs/getting-started/integration.md @@ -35,32 +35,14 @@ require(['path/to/chartjs/dist/Chart.min.js'], function(Chart){ }); ``` -**Note:** in order to use the time scale, you need to make sure [one of the available date adapters](https://github.com/chartjs/awesome#adapters) and corresponding date library are fully loaded **before** requiring Chart.js. The date adapter for Moment.js is included with Chart.js, but you still need to include Moment.js itself if this is the date adapter you choose to use. You can either use a shim: +**Note:** in order to use the time scale, you need to make sure [one of the available date adapters](https://github.com/chartjs/awesome#adapters) and corresponding date library are fully loaded **after** requiring Chart.js. For this you can use nested requires: ```javascript -require.config({ - shim: { - 'chartjs': { - deps: ['moment'] // enforce moment to be loaded before chartjs - } - }, - paths: { - 'chartjs': 'path/to/chartjs/dist/Chart.min.js', - 'moment': 'path/to/moment' - } -}); - require(['chartjs'], function(Chart) { - new Chart(ctx, {...}); -}); -``` - -or simply use two nested `require()`: - -```javascript -require(['moment'], function() { - require(['chartjs'], function(Chart) { - new Chart(ctx, {...}); + require(['moment'], function() { + require(['chartjs-adapter-moment'], function() { + new Chart(ctx, {...}); + }); }); }); ``` diff --git a/docs/getting-started/v3-migration.md b/docs/getting-started/v3-migration.md index 9f3dc4f40b4..ed09de3923c 100644 --- a/docs/getting-started/v3-migration.md +++ b/docs/getting-started/v3-migration.md @@ -15,7 +15,7 @@ Chart.js 3.0 introduces a number of breaking changes. Chart.js 2.0 was released ### Setup and installation * Chart.js is no longer providing the `Chart.bundle.js` and `Chart.bundle.min.js`. Please see the [installation](installation.md) and [integration](integration.md) docs for details on the recommended way to setup Chart.js if you were using these builds. -* `moment` is no longer specified as an npm dependency. If you are using the time scale, you must include one of [the available adapters](https://github.com/chartjs/awesome#adapters) and corresponding date library. If you are using a date library other than moment, you no longer need to exclude moment from your build. +* `moment` is no longer specified as an npm dependency. If you are using the time scale, you must include one of [the available adapters](https://github.com/chartjs/awesome#adapters) and corresponding date library. You no longer need to exclude moment from your build. ### Ticks diff --git a/karma.conf.js b/karma.conf.js index 2f115617e55..23777ccffdc 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -55,7 +55,8 @@ module.exports = function(karma) { {pattern: 'test/fixtures/**/*.png', included: false}, 'node_modules/moment/min/moment.min.js', 'test/index.js', - 'src/index.js' + 'src/index.js', + 'node_modules/chartjs-adapter-moment/dist/chartjs-adapter-moment.js' ].concat((args.inputs || 'test/specs/**/*.js').split(';')), preprocessors: { diff --git a/package-lock.json b/package-lock.json index 29cb3ce9c93..68c822db885 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3932,6 +3932,12 @@ "integrity": "sha512-mT8iDcrh03qDGRRmoA2hmBJnxpllMR+0/0qlzjqZES6NdiWDcZkCNAk4rPFZ9Q85r27unkiNNg8ZOiwZXBHwcA==", "dev": true }, + "chartjs-adapter-moment": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/chartjs-adapter-moment/-/chartjs-adapter-moment-0.1.1.tgz", + "integrity": "sha512-lFGCiqpn+0e6CEbEKdG1L3LYR0jnKFqtXsExzmEWXGCxdymTFSqntiWAqWAYDblL/QB3sUk881xVGzhWZdn4kQ==", + "dev": true + }, "chokidar": { "version": "2.1.8", "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-2.1.8.tgz", diff --git a/package.json b/package.json index 1953bcf09cd..0f4c95ed682 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "@rollup/plugin-node-resolve": "^7.1.1", "babel-plugin-istanbul": "^6.0.0", "babel-preset-es2015-rollup": "^3.0.0", + "chartjs-adapter-moment": "^0.1.1", "coveralls": "^3.0.9", "eslint": "^6.8.0", "eslint-config-chartjs": "^0.2.0", diff --git a/rollup.config.js b/rollup.config.js index 877a3b7f1af..607221dc277 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -4,7 +4,6 @@ const babel = require('rollup-plugin-babel'); const cleanup = require('rollup-plugin-cleanup'); const json = require('@rollup/plugin-json'); -const optional = require('./rollup.plugins').optional; const resolve = require('@rollup/plugin-node-resolve'); const terser = require('rollup-plugin-terser').terser; const pkg = require('./package.json'); @@ -28,9 +27,6 @@ module.exports = [ json(), resolve(), babel(), - optional({ - include: ['moment'] - }), cleanup({ sourcemap: true }) @@ -41,13 +37,7 @@ module.exports = [ banner, format: 'umd', indent: false, - globals: { - moment: 'moment' - } }, - external: [ - 'moment' - ] }, { input, @@ -55,9 +45,6 @@ module.exports = [ json(), resolve(), babel(), - optional({ - include: ['moment'] - }), terser({ output: { preamble: banner @@ -69,13 +56,7 @@ module.exports = [ file: 'dist/Chart.min.js', format: 'umd', indent: false, - globals: { - moment: 'moment' - } }, - external: [ - 'moment' - ] }, // ES6 builds @@ -97,13 +78,7 @@ module.exports = [ banner, format: 'esm', indent: false, - globals: { - moment: 'moment' - } }, - external: [ - 'moment' - ] }, { input, @@ -122,12 +97,6 @@ module.exports = [ file: 'dist/Chart.esm.min.js', format: 'esm', indent: false, - globals: { - moment: 'moment' - } }, - external: [ - 'moment' - ] }, ]; diff --git a/rollup.plugins.js b/rollup.plugins.js deleted file mode 100644 index 0a8598d124d..00000000000 --- a/rollup.plugins.js +++ /dev/null @@ -1,60 +0,0 @@ -/* eslint-disable import/no-commonjs */ -const UMD_WRAPPER_RE = /(\(function \(global, factory\) \{)((?:\s.*?)*)(\}\(this,)/; -const CJS_FACTORY_RE = /(module.exports = )(factory\(.*?\))( :)/; -const AMD_FACTORY_RE = /(define\()(.*?, factory)(\) :)/; - -function optional(config = {}) { - return { - name: 'optional', - renderChunk(code, chunk, options) { - if (options.format !== 'umd') { - this.error('only UMD format is currently supported'); - } - - const wrapper = UMD_WRAPPER_RE.exec(code); - const include = config.include; - if (!wrapper) { - this.error('failed to parse the UMD wrapper'); - } - - let content = wrapper[2]; - let factory = (CJS_FACTORY_RE.exec(content) || [])[2]; - let updated = false; - - for (const lib of chunk.imports) { - if (!include || include.indexOf(lib) !== -1) { - const regex = new RegExp(`require\\('${lib}'\\)`); - if (!regex.test(factory)) { - this.error(`failed to parse the CJS require for ${lib}`); - } - - // We need to write inline try / catch with explicit require - // in order to enable statical extraction of dependencies: - // try { return require('moment'); } catch(e) {} - const loader = `function() { try { return require('${lib}'); } catch(e) { } }()`; - factory = factory.replace(regex, loader); - updated = true; - } - } - - if (!updated) { - return; - } - - // Replace the CJS factory by our updated one. - content = content.replace(CJS_FACTORY_RE, `$1${factory}$3`); - - // Replace the AMD factory by our updated one: we need to use the - // following AMD form in order to be able to try/catch require: - // define(['require'], function(require) { ... require(...); ... }) - // https://github.com/amdjs/amdjs-api/wiki/AMD#using-require-and-exports - content = content.replace(AMD_FACTORY_RE, `$1['require'], function(require) { return ${factory}; }$3`); - - return code.replace(UMD_WRAPPER_RE, `$1${content}$3`); - } - }; -} - -module.exports = { - optional -}; diff --git a/samples/.eslintrc.yml b/samples/.eslintrc.yml index 5a16375624f..bdf8eab325c 100644 --- a/samples/.eslintrc.yml +++ b/samples/.eslintrc.yml @@ -3,6 +3,7 @@ globals: Chart: true Samples: true moment: true + luxon: true randomScalingFactor: true rules: diff --git a/samples/scales/financial.html b/samples/scales/financial.html index 32171add098..c92ac336880 100644 --- a/samples/scales/financial.html +++ b/samples/scales/financial.html @@ -3,8 +3,9 @@ Line Chart - + +