-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
angular-cli_config.ts
204 lines (174 loc) · 5.94 KB
/
angular-cli_config.ts
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import { CompilerOptions } from 'typescript';
import { Path } from '@angular-devkit/core';
import path from 'path';
import fs from 'fs';
import { logger } from '@storybook/node-logger';
import { TsconfigPathsPlugin } from 'tsconfig-paths-webpack-plugin';
import stripJsonComments from 'strip-json-comments';
import {
isBuildAngularInstalled,
normalizeAssetPatterns,
filterOutStylingRules,
getAngularCliParts,
} from './angular-cli_utils';
// todo add more accurate typings
interface BasicOptions {
options: {
baseUrl?: string | undefined;
};
raw: object;
fileNames: string[];
errors: any[];
}
function getTsConfigOptions(tsConfigPath: Path) {
const basicOptions: BasicOptions = {
options: {},
raw: {},
fileNames: [],
errors: [],
};
if (!fs.existsSync(tsConfigPath)) {
return basicOptions;
}
const tsConfig = JSON.parse(stripJsonComments(fs.readFileSync(tsConfigPath, 'utf8')));
const { baseUrl } = tsConfig.compilerOptions as CompilerOptions;
if (baseUrl) {
const tsConfigDirName = path.dirname(tsConfigPath);
basicOptions.options.baseUrl = path.resolve(tsConfigDirName, baseUrl);
}
return basicOptions;
}
export function getAngularCliConfig(dirToSearch: string) {
const fname = path.join(dirToSearch, 'angular.json');
if (!fs.existsSync(fname)) {
logger.error(`Could not find angular.json using ${fname}`);
return undefined;
}
return JSON.parse(stripJsonComments(fs.readFileSync(fname, 'utf8')));
}
export function getLeadingAngularCliProject(ngCliConfig: any) {
if (!ngCliConfig) {
return null;
}
const { defaultProject } = ngCliConfig;
const { projects } = ngCliConfig;
if (!projects || !Object.keys(projects).length) {
throw new Error('angular.json must have projects entry.');
}
let projectName;
const firstProjectName = Object.keys(projects)[0];
if (projects.storybook) {
projectName = 'storybook';
} else if (defaultProject && projects[defaultProject]) {
projectName = defaultProject;
} else if (projects[firstProjectName]) {
projectName = firstProjectName;
}
const project = projects[projectName];
if (!project) {
logger.error(`Could not find angular project '${projectName}' in angular.json.`);
} else {
logger.info(`=> Using angular project '${projectName}' for configuring Storybook.`);
}
if (!project.architect.build) {
logger.error(`architect.build is not defined for project '${projectName}'.`);
}
return project;
}
export function getAngularCliWebpackConfigOptions(dirToSearch: Path) {
const angularCliConfig = getAngularCliConfig(dirToSearch);
const project = getLeadingAngularCliProject(angularCliConfig);
if (!angularCliConfig || !project.architect.build) {
return null;
}
const { options: projectOptions } = project.architect.build;
const normalizedAssets = normalizeAssetPatterns(
projectOptions.assets,
dirToSearch,
project.sourceRoot
);
const projectRoot = path.resolve(dirToSearch, project.root);
const tsConfigPath = path.resolve(dirToSearch, projectOptions.tsConfig) as Path;
const tsConfig = getTsConfigOptions(tsConfigPath);
const budgets = projectOptions.budgets || [];
const scripts = projectOptions.scripts || [];
const outputPath = projectOptions.outputPath || 'dist/storybook-angular';
const styles = projectOptions.styles || [];
return {
root: dirToSearch,
projectRoot,
tsConfigPath,
tsConfig,
supportES2015: false,
buildOptions: {
sourceMap: false,
optimization: {},
...projectOptions,
assets: normalizedAssets,
budgets,
scripts,
styles,
outputPath,
},
};
}
// todo add types
export function applyAngularCliWebpackConfig(baseConfig: any, cliWebpackConfigOptions: any) {
if (!cliWebpackConfigOptions) {
return baseConfig;
}
if (!isBuildAngularInstalled()) {
logger.info('=> Using base config because @angular-devkit/build-angular is not installed.');
return baseConfig;
}
const cliParts = getAngularCliParts(cliWebpackConfigOptions);
if (!cliParts) {
logger.warn('=> Failed to get angular-cli webpack config.');
return baseConfig;
}
logger.info('=> Get angular-cli webpack config.');
const { cliCommonConfig, cliStyleConfig } = cliParts;
// 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);
// cliStyleConfig.entry adds global style files to the webpack context
// todo add type for acc
const entry = [
...baseConfig.entry,
...Object.values(cliStyleConfig.entry).reduce((acc: any, item) => acc.concat(item), []),
];
const module = {
...baseConfig.module,
rules: [...cliStyleConfig.module.rules, ...rulesExcludingStyles],
};
// We use cliCommonConfig plugins to serve static assets files.
const plugins = [...cliStyleConfig.plugins, ...cliCommonConfig.plugins, ...baseConfig.plugins];
const resolve = {
...baseConfig.resolve,
modules: Array.from(
new Set([...baseConfig.resolve.modules, ...cliCommonConfig.resolve.modules])
),
plugins: [
new TsconfigPathsPlugin({
configFile: cliWebpackConfigOptions.buildOptions.tsConfig,
// After ng build my-lib the default value of 'main' in the package.json is 'umd'
// This causes that you cannot import components directly from dist
// https://github.com/angular/angular-cli/blob/9f114aee1e009c3580784dd3bb7299bdf4a5918c/packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/browser.ts#L68
mainFields: [
...(cliWebpackConfigOptions.supportES2015 ? ['es2015'] : []),
'browser',
'module',
'main',
],
}),
],
};
return {
...baseConfig,
entry,
module,
plugins,
resolve,
resolveLoader: cliCommonConfig.resolveLoader,
};
}