-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
angular-cli-webpack.js
96 lines (88 loc) · 2.83 KB
/
angular-cli-webpack.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Private angular devkit stuff
const {
generateI18nBrowserWebpackConfigFromContext,
} = require('@angular-devkit/build-angular/src/utils/webpack-browser-config');
const {
getCommonConfig,
getStylesConfig,
getDevServerConfig,
getTypeScriptConfig,
} = require('@angular-devkit/build-angular/src/webpack/configs');
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
const { filterOutStylingRules } = require('./utils/filter-out-styling-rules');
const {
default: StorybookNormalizeAngularEntryPlugin,
} = require('./plugins/storybook-normalize-angular-entry-plugin');
/**
* Extract webpack config from angular-cli 13.x.x
* ⚠️ This file is in JavaScript to not use TypeScript. Because current storybook TypeScript version is not compatible with Angular CLI.
* FIXME: Try another way with TypeScript on future storybook version (7 maybe 🤞)
*
* @param {*} baseConfig Previous webpack config from storybook
* @param {*} options { builderOptions, builderContext }
*/
exports.getWebpackConfig = async (baseConfig, { builderOptions, builderContext }) => {
/**
* Get angular-cli Webpack config
*/
const { config: cliConfig } = await generateI18nBrowserWebpackConfigFromContext(
{
// Default options
index: 'noop-index',
main: 'noop-main',
outputPath: 'noop-out',
// Options provided by user
...builderOptions,
// Fixed options
optimization: false,
namedChunks: false,
progress: false,
buildOptimizer: false,
aot: false,
},
builderContext,
(wco) => [
getCommonConfig(wco),
getStylesConfig(wco),
getTypeScriptConfig ? getTypeScriptConfig(wco) : getDevServerConfig(wco),
]
);
/**
* Merge baseConfig Webpack with angular-cli Webpack
*/
const entry = [
...baseConfig.entry,
...(cliConfig.entry.styles ?? []),
...(cliConfig.entry.polyfills ?? []),
];
// Don't use storybooks styling rules because we have to use rules created by @angular-devkit/build-angular
// because @angular-devkit/build-angular created rules have include/exclude for global style files.
const rulesExcludingStyles = filterOutStylingRules(baseConfig);
const module = {
...baseConfig.module,
rules: [...cliConfig.module.rules, ...rulesExcludingStyles],
};
const plugins = [
...(cliConfig.plugins ?? []),
...baseConfig.plugins,
new StorybookNormalizeAngularEntryPlugin(),
];
const resolve = {
...baseConfig.resolve,
modules: Array.from(new Set([...baseConfig.resolve.modules, ...cliConfig.resolve.modules])),
plugins: [
new TsconfigPathsPlugin({
configFile: builderOptions.tsConfig,
mainFields: ['browser', 'module', 'main'],
}),
],
};
return {
...baseConfig,
entry,
module,
plugins,
resolve,
resolveLoader: cliConfig.resolveLoader,
};
};