Skip to content

Commit

Permalink
Add a note about Moment.js in Require.js
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbrunel committed Jan 29, 2019
1 parent 93fdc53 commit 3df2440
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions docs/getting-started/integration.md
Expand Up @@ -11,6 +11,13 @@ Chart.js can be integrated with plain JavaScript or with different module loader
</script>
```

## Common JS

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

## Bundlers (Webpack, Rollup, etc.)

```javascript
Expand Down Expand Up @@ -38,19 +45,42 @@ var myChart = new Chart(ctx, {...});
}
```

## Common JS
## Require JS

**Important:** RequireJS [can **not** load CommonJS module as is](http://www.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.).

```javascript
var Chart = require('chart.js');
var myChart = new Chart(ctx, {...});
require(['path/to/chartjs/dist/Chart.min.js'], function(Chart){
var myChart = new Chart(ctx, {...});
});
```

## Require JS
**Note:** starting v2.8, Moment.js is now an optional dependency for `Chart.js` and `Chart.min.js`. That means you need to make sure Moment.js is fully loaded **before** requiring Chart.js. You can either use a shim:

```javascript
require(['path/to/chartjs/dist/Chart.js'], function(Chart){
var myChart = new Chart(ctx, {...});
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, {...});
});
```

**Important:** RequireJS [can **not** load CommonJS module as is](http://www.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.).
or simply use two nested `require()`:

```javascript
require(['moment'], function() {
require(['chartjs'], function(Chart) {
new Chart(ctx, {...});
});
});
```

0 comments on commit 3df2440

Please sign in to comment.