Skip to content

Commit 8e2771e

Browse files
authored
Merge pull request #4182 from marcj/patch-1
Added docs about building Plotly with Angular
2 parents 38c40c3 + 24194b8 commit 8e2771e

File tree

1 file changed

+49
-21
lines changed

1 file changed

+49
-21
lines changed

BUILDING.md

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,32 +42,60 @@ 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+
include: [
58+
path.resolve(__dirname, "node_modules/plotly.js")
59+
],
60+
loader: 'ify-loader'
61+
}
62+
]
63+
},
64+
};
5765
```
5866

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

0 commit comments

Comments
 (0)