Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propose a simplified registration process #8419

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions docs/docs/getting-started/integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,17 @@ var myChart = new Chart(ctx, {...});

## Bundlers (Webpack, Rollup, etc.)

Chart.js 3 is tree-shakeable, so it is necessary to import and register the controllers, elements, scales and plugins you are going to use.
To use Chart.js, a simple import is all that is required.

```javascript
import Chart from 'chart.js';
var myChart = new Chart(ctx, {...});
```

### Tree Shaking

Chart.js 3 has optional tree shaking that is enabled via the `chart.js/tree` package. To use this, it is necessary to import and register the controllers, elements, scales and plugins you wish to use. The example below shows all of the available imports and how to register them.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think users expect the default behavior (i.e. import {...} from 'chart.js') to be tree-shakable, which may be the most common use case. It's also the natural method to import from Chart.js and it may be better to keep it that way. As a plugin developers, I would also expect the following snippet to only bring LineElement and not trigger any auto-registration:

import { LineElement } from 'chart.js';

export class CurveElement extends LineElement {
  // ...
}

It may be better to have a special case for auto-registering instead (e.g. chart.js/auto or chart.js/all).


For all available imports see the example below.
```javascript
import {
Chart,
Expand All @@ -50,7 +58,7 @@ import {
Legend,
Title,
Tooltip
} from 'chart.js';
} from 'chart.js/tree';

Chart.register(
ArcElement,
Expand Down Expand Up @@ -80,6 +88,18 @@ Chart.register(
var myChart = new Chart(ctx, {...});
```

A short registration format is also available.

```javascript
import { Chart, controllers, elements, plugins, scales } from 'chart.js';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it really working? I can't find were you export these groups.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I forgot to delete that part of the docs when I switched around how this worked.

Chart.register(...[
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need to wrap everything in an array:

Chart.register(
    ...Object.values(controllers),
    ...Object.values(elements),
    ...Object.values(plugins),
    ...Object.values(scales),
);

...Object.values(controllers),
...Object.values(elements),
...Object.values(plugins),
...Object.values(scales),
]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could make this even easier by exporting an array containing everything:

import { registrables } from 'chart.js';

Chart.register(...registrables);

You might have a better name for registrables.

```

## Require JS

**Important:** RequireJS [can **not** load CommonJS module as is](https://requirejs.org/docs/commonjs.html#intro), so be sure to require one of the UMD builds instead (i.e. `dist/chart.js`, `dist/chart.min.js`, etc.).
Expand Down
29 changes: 1 addition & 28 deletions docs/docs/getting-started/v3-migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,7 @@ Chart.js 3.0 introduces a number of breaking changes. Chart.js 2.0 was released
* 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` or `timeseries` scales, 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.
* The `Chart` constructor will throw an error if the canvas/context provided is already in use
* Chart.js 3 is tree-shakeable. So if you are using it as an `npm` module in a project, you need to import and register the controllers, elements, scales and plugins you want to use. You will not have to call `register` if importing Chart.js via a `script` tag, but will not get the tree shaking benefits in this case. Here is an example of registering components:

```javascript
import { Chart, LineController, LineElement, PointElement, LinearScale, Title } from `chart.js`

Chart.register(LineController, LineElement, PointElement, LinearScale, Title);

const chart = new Chart(ctx, {
type: 'line',
// data: ...
options: {
plugins: {
title: {
display: true,
text: 'Chart Title'
}
},
scales: {
x: {
type: 'linear'
},
y: {
type: 'linear'
}
}
}
})
```
* Chart.js 3 has optional tree shaking when used as an `npm` module. In v3.0.0-beta.10 and below, this was the always enabled. Starting in v3.0.0-beta.11, tree shaking was moved to the `chart.js/tree` package and made into an optional enhancement. For details on how to enable tree-shaking, see the [integration](integration.md#tree-shaking) documentation.

### Chart types

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
"dist/chunks/*.js",
"dist/chunks/*.d.ts",
"helpers/**/*.js",
"helpers/**/*.d.ts"
"helpers/**/*.d.ts",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of the scope of this PR: could this be moved under a single folder and copied when publishing to npm? For example: ./esm/helpers, ./esm/tree, etc. (or a different folder name).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think NPM in that case might consider the package to be chart.js/dist/helpers but we'd have to experiment and see

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These extra packages could even be generated by a script as we did with bowser.json

"tree/**/*.js",
"tree/**/*.d.ts"
],
"scripts": {
"autobuild": "rollup -c -w",
Expand Down
2 changes: 2 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ const pkg = require('./package.json');
const input = 'src/index.js';
const inputESM = {
'dist/chart.esm': 'src/index.esm.js',
'dist/chart.shakeable.esm': 'src/index.shakeable.esm.js',
'dist/helpers.esm': 'src/helpers/index.js'
};
const inputESMTypings = {
'dist/chart.esm': 'types/index.esm.d.ts',
'dist/chart.shakeable.esm': 'types/index.shakeable.esm.d.ts',
'dist/helpers.esm': 'types/helpers/index.d.ts'
};

Expand Down
12 changes: 12 additions & 0 deletions src/index.esm.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
// Export all of these as we do in the tree-shakeable version
export * from './controllers';
export * from './core';
export * from './elements';
export * from './platform';
export * from './plugins';
export * from './scales';

import {Chart} from './core';
import * as controllers from './controllers';
import * as elements from './elements';
import * as plugins from './plugins';
import * as scales from './scales';

Chart.register(controllers, elements, plugins, scales);

// Since everything is already registered, provide a default Chart export for convenience
export default Chart;
6 changes: 6 additions & 0 deletions src/index.shakeable.esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export * from './controllers';
export * from './core';
export * from './elements';
export * from './platform';
export * from './plugins';
export * from './scales';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You still need to export those groups:

// ...
export * from './plugins';
export * from './scales';

// Exporting groups to simplify the registration.
export * as controllers from './controllers';
export * as elements from './elements';
export * as plugins from './plugins';
export * as scales from './scales';

8 changes: 8 additions & 0 deletions tree/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "chart.js-tree-shaking",
"private": true,
"description": "tree shakeable version of chart.js ESM build",
"main": "tree.js",
"module": "tree.esm.js",
"types": "tree.esm.d.ts"
}
1 change: 1 addition & 0 deletions tree/tree.esm.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '../dist/chart.shakeable.esm';
1 change: 1 addition & 0 deletions tree/tree.esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '../dist/chart.shakeable.esm';
1 change: 1 addition & 0 deletions tree/tree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('..').tree;
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"typedocOptions": {
"name": "Chart.js",
"entryPoints": ["src/index.esm.js"],
"entryPoints": ["src/index.esm.js", "src/index.shakeable.esm.js"],
"excludeExternals": true,
"excludeNotExported": true,
"includeVersion": true,
Expand Down
2 changes: 1 addition & 1 deletion types/animation.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Chart } from './index.esm';
import { Chart } from './index.shakeable.esm';
import { AnyObject } from './basic';

export class Animation {
Expand Down
2 changes: 1 addition & 1 deletion types/helpers/helpers.canvas.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PointStyle } from '../index.esm';
import { PointStyle } from '../index.shakeable.esm';
import { Color } from '../color';
import { ChartArea } from '../geometric';
import { CanvasFontSpec } from './helpers.options';
Expand Down
2 changes: 1 addition & 1 deletion types/helpers/helpers.easing.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EasingFunction } from '../index.esm';
import { EasingFunction } from '../index.shakeable.esm';

export type EasingFunctionSignature = (t: number) => number;

Expand Down
2 changes: 1 addition & 1 deletion types/helpers/helpers.options.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FontSpec } from '../index.esm';
import { FontSpec } from '../index.shakeable.esm';

export interface CanvasFontSpec extends FontSpec {
string: string;
Expand Down
Loading