Skip to content

Commit 4fb8deb

Browse files
committed
feature #185 Adding support for Chartjs 3 (gbere, weaverryan)
This PR was merged into the 2.x branch. Discussion ---------- Adding support for Chartjs 3 | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Tickets | Fixes #86 | License | MIT This takes the work from #126 and rebases so we can finish on the 2.x branch. Commits ------- 444b7ea Fixing chartjs test and rollup process 38bd8c2 Update chart.js. Resolves #86
2 parents a3da92d + 444b7ea commit 4fb8deb

File tree

6 files changed

+58
-18
lines changed

6 files changed

+58
-18
lines changed

rollup.config.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,39 @@ import glob from 'glob';
44
import path from 'path';
55
import pkgUp from 'pkg-up';
66

7+
/**
8+
* Guarantees that any files imported from a peer dependency are treated as an external.
9+
*
10+
* For example, if we import `chart.js/auto`, that would not normally
11+
* match the "chart.js" we pass to the "externals" config. This plugin
12+
* catches that case and adds it as an external.
13+
*
14+
* Inspired by https://github.com/oat-sa/rollup-plugin-wildcard-external
15+
*/
16+
const wildcardExternalsPlugin = (peerDependencies) => ({
17+
name: 'wildcard-externals',
18+
resolveId(source, importer) {
19+
if (importer) {
20+
let matchesExternal = false;
21+
peerDependencies.forEach((peerDependency) => {
22+
if (source.includes(`/${peerDependency}/`)) {
23+
matchesExternal = true;
24+
}
25+
});
26+
27+
if (matchesExternal) {
28+
return {
29+
id: source,
30+
external: true,
31+
moduleSideEffects: true
32+
};
33+
}
34+
}
35+
36+
return null; // other ids should be handled as usually
37+
}
38+
});
39+
740
const files = glob.sync("src/**/assets/src/*controller.ts");
841
const packages = files.map((file) => {
942
const absolutePath = path.join(__dirname, file);
@@ -23,6 +56,7 @@ const packages = files.map((file) => {
2356
plugins: [
2457
resolve(),
2558
typescript(),
59+
wildcardExternalsPlugin(peerDependencies)
2660
],
2761
};
2862
});

src/Chartjs/Resources/assets/dist/controller.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
import { Controller } from '@hotwired/stimulus';
2-
import { Chart } from 'chart.js';
2+
import Chart from 'chart.js/auto';
33

4-
class controller extends Controller {
4+
class default_1 extends Controller {
55
connect() {
66
if (!(this.element instanceof HTMLCanvasElement)) {
77
throw new Error('Invalid element');
88
}
9-
const viewData = this.element.getAttribute('data-view');
10-
if (!viewData) {
11-
throw new Error('Missing data-view attribute.');
12-
}
13-
const payload = JSON.parse(viewData);
9+
const payload = this.viewValue;
1410
if (Array.isArray(payload.options) && 0 === payload.options.length) {
1511
payload.options = {};
1612
}
@@ -28,5 +24,8 @@ class controller extends Controller {
2824
this.element.dispatchEvent(userEvent);
2925
}
3026
}
27+
default_1.values = {
28+
view: Object,
29+
};
3130

32-
export { controller as default };
31+
export { default_1 as default };

src/Chartjs/Resources/assets/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
},
1616
"peerDependencies": {
1717
"@hotwired/stimulus": "^3.0.0",
18-
"chart.js": "^2.9.4"
18+
"chart.js": "^3.4.1"
1919
},
2020
"devDependencies": {
2121
"@hotwired/stimulus": "^3.0.0",
2222
"@types/chart.js": "^2.9.34",
23-
"chart.js": "^2.9.4",
24-
"jest-canvas-mock": "^2.3.0"
23+
"chart.js": "^3.4.1",
24+
"jest-canvas-mock": "^2.3.0",
25+
"resize-observer-polyfill": "^1.5.1"
2526
}
2627
}

src/Chartjs/Resources/assets/src/controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
'use strict';
1111

1212
import { Controller } from '@hotwired/stimulus';
13-
import { Chart } from 'chart.js';
13+
import Chart from 'chart.js/auto';
1414

1515
export default class extends Controller {
1616
static values = {

src/Chartjs/Resources/assets/test/controller.test.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,20 @@ const startStimulus = () => {
3232
const application = Application.start();
3333
application.register('check', CheckController);
3434
application.register('chartjs', ChartjsController);
35+
36+
return application;
3537
};
3638

3739
describe('ChartjsController', () => {
38-
let container;
40+
let application;
3941

4042
afterEach(() => {
4143
clearDOM();
44+
application.stop();
4245
});
4346

4447
it('connect without options', async () => {
45-
container = mountDOM(`
48+
const container = mountDOM(`
4649
<canvas
4750
data-testid="canvas"
4851
data-controller="check chartjs"
@@ -53,18 +56,19 @@ describe('ChartjsController', () => {
5356
expect(getByTestId(container, 'canvas')).not.toHaveClass('pre-connected');
5457
expect(getByTestId(container, 'canvas')).not.toHaveClass('connected');
5558

56-
startStimulus();
59+
application = startStimulus();
60+
5761
await waitFor(() => {
5862
expect(getByTestId(container, 'canvas')).toHaveClass('pre-connected');
5963
expect(getByTestId(container, 'canvas')).toHaveClass('connected');
6064
});
6165

6266
const chart = getByTestId(container, 'canvas').chart;
63-
expect(chart.options.showLines).toBe(true);
67+
expect(chart.options.showLines).toBeUndefined();
6468
});
6569

6670
it('connect with options', async () => {
67-
container = mountDOM(`
71+
const container = mountDOM(`
6872
<canvas
6973
data-testid="canvas"
7074
data-controller="check chartjs"
@@ -75,7 +79,7 @@ describe('ChartjsController', () => {
7579
expect(getByTestId(container, 'canvas')).not.toHaveClass('pre-connected');
7680
expect(getByTestId(container, 'canvas')).not.toHaveClass('connected');
7781

78-
startStimulus();
82+
application = startStimulus();
7983
await waitFor(() => {
8084
expect(getByTestId(container, 'canvas')).toHaveClass('pre-connected');
8185
expect(getByTestId(container, 'canvas')).toHaveClass('connected');

src/Chartjs/Resources/assets/test/setup.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@
1010
'use strict';
1111

1212
import 'jest-canvas-mock';
13+
// eslint-disable-next-line
14+
global.ResizeObserver = require('resize-observer-polyfill');

0 commit comments

Comments
 (0)