Skip to content

Commit 52e7210

Browse files
authored
Added docs about building Plotly correctly with Angular
1 parent 46866fd commit 52e7210

File tree

1 file changed

+46
-21
lines changed

1 file changed

+46
-21
lines changed

BUILDING.md

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,32 +42,57 @@ browserify index.js > bundle.js
4242

4343
## Angular CLI
4444

45-
Currently Angular CLI uses Webpack under the hood to bundle and build your Angular application.
46-
Sadly it doesn't allow you to override its Webpack config in order to add the plugin mentioned in the [Webpack](#webpack) section.
47-
Without this plugin your build will fail when it tries to build glslify for WebGL plots.
45+
Since Angular uses webpack under the hood and doesn't allow easily to change it's webpack configuration. There is some work needed using a custom-webpack builder to get things going.
4846

49-
Currently 2 solutions exists to circumvent this issue:
47+
1. Install [`@angular-builders/custom-webpack`](https://www.npmjs.com/package/@angular-builders/custom-webpack) and [[email protected]+](https://github.com/hughsk/ify-loader)
48+
2. Create a new `extra-webpack.config.js` beside `angular.json`.
5049

51-
1) If you need to use WebGL plots, you can create a Webpack config from your Angular CLI project with [ng eject](https://github.com/angular/angular-cli/wiki/eject). This will allow you to follow the instructions regarding Webpack.
52-
2) If you don't need to use WebGL plots, you can make a custom build containing only the required modules for your plots. The clean way to do it with Angular CLI is not the method described in the [Modules](https://github.com/plotly/plotly.js/blob/master/README.md#modules) section of the README but the following:
53-
54-
```typescript
55-
// in the Component you want to create a graph
56-
import * as Plotly from 'plotly.js';
50+
> extra-webpack.config.json
51+
```javascript
52+
module.exports = {
53+
module: {
54+
rules: [
55+
{
56+
test: /\.js$/,
57+
loader: 'ify-loader'
58+
}
59+
]
60+
},
61+
};
5762
```
5863

64+
3. Change the builder in `angular.json` to `"@angular-builders/custom-webpack:browser` and configure it correctly to use our new webpack config.
65+
66+
> angular.json
5967
```json
60-
// in src/tsconfig.app.json
61-
// List here the modules you want to import
62-
// this example is for scatter plots
6368
{
64-
"compilerOptions": {
65-
"paths": {
66-
"plotly.js": [
67-
"../node_modules/plotly.js/lib/core.js",
68-
"../node_modules/plotly.js/lib/scatter.js"
69-
]
69+
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
70+
"version": 1,
71+
"newProjectRoot": "projects",
72+
"projects": {
73+
"MY_PROJECT_NAME": {
74+
"root": "",
75+
"sourceRoot": "src",
76+
"projectType": "application",
77+
"prefix": "app",
78+
"schematics": {
79+
"@schematics/angular:component": {
80+
"styleext": "scss"
81+
}
82+
},
83+
"architect": {
84+
"build": {
85+
"builder": "@angular-builders/custom-webpack:browser",
86+
"options": {
87+
....
88+
"customWebpackConfig": {
89+
"path": "./extra-webpack.config.js",
90+
"replaceDuplicatePlugins": true,
91+
"mergeStrategies": {"module.rules": "merge"}
92+
}
93+
}
7094
}
71-
}
72-
}
95+
...
7396
```
97+
It's important to set `projects.x.architect.build.builder` and `projects.x.architect.build.options.customWebpackConfig`.
98+
If you have more projects in your `angular.json` make sure to adjust their settings accordingly.

0 commit comments

Comments
 (0)