1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4 | return new (P || (P = Promise))(function (resolve, reject) {
|
5 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9 | });
|
10 | };
|
11 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
12 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
13 | };
|
14 | Object.defineProperty(exports, "__esModule", { value: true });
|
15 | exports.getWebpackConfig = void 0;
|
16 | const node_logger_1 = require("@storybook/node-logger");
|
17 | const tsconfig_paths_webpack_plugin_1 = __importDefault(require("tsconfig-paths-webpack-plugin"));
|
18 | const architect_1 = require("@angular-devkit/architect");
|
19 | const angular_read_workspace_1 = require("./angular-read-workspace");
|
20 | const angular_devkit_build_webpack_1 = require("./angular-devkit-build-webpack");
|
21 | const filter_out_styling_rules_1 = require("./utils/filter-out-styling-rules");
|
22 |
|
23 |
|
24 |
|
25 | function getWebpackConfig(baseConfig, options) {
|
26 | return __awaiter(this, void 0, void 0, function* () {
|
27 | const dirToSearch = process.cwd();
|
28 |
|
29 | let workspaceConfig;
|
30 | try {
|
31 | workspaceConfig = yield angular_read_workspace_1.readAngularWorkspaceConfig(dirToSearch);
|
32 | }
|
33 | catch (error) {
|
34 | node_logger_1.logger.error(`=> Could not find angular workspace config (angular.json) on this path "${dirToSearch}"`);
|
35 | node_logger_1.logger.info(`=> Fail to load angular-cli config. Using base config`);
|
36 | return baseConfig;
|
37 | }
|
38 |
|
39 | let project;
|
40 | let target;
|
41 | let confName;
|
42 | try {
|
43 |
|
44 | if (options.angularBrowserTarget !== null) {
|
45 | const browserTarget = options.angularBrowserTarget
|
46 | ? architect_1.targetFromTargetString(options.angularBrowserTarget)
|
47 | : {
|
48 | configuration: undefined,
|
49 | project: angular_read_workspace_1.getDefaultProjectName(workspaceConfig),
|
50 | target: 'build',
|
51 | };
|
52 | const fondProject = angular_read_workspace_1.findAngularProjectTarget(workspaceConfig, browserTarget.project, browserTarget.target);
|
53 | project = fondProject.project;
|
54 | target = fondProject.target;
|
55 | confName = browserTarget.configuration;
|
56 | node_logger_1.logger.info(`=> Using angular project "${browserTarget.project}:${browserTarget.target}${confName ? `:${confName}` : ''}" for configuring Storybook`);
|
57 | }
|
58 |
|
59 | if (options.angularBrowserTarget === null && options.tsConfig) {
|
60 | node_logger_1.logger.info(`=> Using default angular project with "tsConfig:${options.tsConfig}"`);
|
61 | project = { root: '', extensions: {}, targets: undefined };
|
62 | target = { builder: '', options: { tsConfig: options.tsConfig } };
|
63 | }
|
64 | }
|
65 | catch (error) {
|
66 | node_logger_1.logger.error(`=> Could not find angular project: ${error.message}`);
|
67 | node_logger_1.logger.info(`=> Fail to load angular-cli config. Using base config`);
|
68 | return baseConfig;
|
69 | }
|
70 |
|
71 | let angularCliWebpackConfig;
|
72 | try {
|
73 | angularCliWebpackConfig = yield angular_devkit_build_webpack_1.extractAngularCliWebpackConfig(dirToSearch, project, target, confName);
|
74 | node_logger_1.logger.info(`=> Using angular-cli webpack config`);
|
75 | }
|
76 | catch (error) {
|
77 | node_logger_1.logger.error(`=> Could not get angular cli webpack config`);
|
78 | throw error;
|
79 | }
|
80 | return mergeAngularCliWebpackConfig(angularCliWebpackConfig, baseConfig);
|
81 | });
|
82 | }
|
83 | exports.getWebpackConfig = getWebpackConfig;
|
84 | function mergeAngularCliWebpackConfig({ cliCommonWebpackConfig, cliStyleWebpackConfig, tsConfigPath }, baseConfig) {
|
85 |
|
86 |
|
87 | const rulesExcludingStyles = filter_out_styling_rules_1.filterOutStylingRules(baseConfig);
|
88 |
|
89 | const entry = [
|
90 | ...baseConfig.entry,
|
91 | ...Object.values(cliStyleWebpackConfig.entry).reduce((acc, item) => acc.concat(item), []),
|
92 | ];
|
93 | const module = Object.assign(Object.assign({}, baseConfig.module), { rules: [...cliStyleWebpackConfig.module.rules, ...rulesExcludingStyles] });
|
94 |
|
95 | const plugins = [
|
96 | ...cliStyleWebpackConfig.plugins,
|
97 | ...cliCommonWebpackConfig.plugins,
|
98 | ...baseConfig.plugins,
|
99 | ];
|
100 | const resolve = Object.assign(Object.assign({}, baseConfig.resolve), { modules: Array.from(new Set([...baseConfig.resolve.modules, ...cliCommonWebpackConfig.resolve.modules])), plugins: [
|
101 | new tsconfig_paths_webpack_plugin_1.default({
|
102 | configFile: tsConfigPath,
|
103 | mainFields: ['browser', 'module', 'main'],
|
104 | }),
|
105 | ] });
|
106 | return Object.assign(Object.assign({}, baseConfig), { entry,
|
107 | module,
|
108 | plugins,
|
109 | resolve, resolveLoader: cliCommonWebpackConfig.resolveLoader });
|
110 | }
|