1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.WebpackCompiler = void 0;
|
4 | const fs_1 = require("fs");
|
5 | const path_1 = require("path");
|
6 | const ui_1 = require("../ui");
|
7 | const webpack_defaults_1 = require("./defaults/webpack-defaults");
|
8 | const get_value_or_default_1 = require("./helpers/get-value-or-default");
|
9 | const webpack = require("webpack");
|
10 | class WebpackCompiler {
|
11 | constructor(pluginsLoader) {
|
12 | this.pluginsLoader = pluginsLoader;
|
13 | }
|
14 | run(configuration, webpackConfigFactoryOrConfig, tsConfigPath, appName, isDebugEnabled = false, watchMode = false, assetsManager, onSuccess) {
|
15 | const cwd = process.cwd();
|
16 | const configPath = (0, path_1.join)(cwd, tsConfigPath);
|
17 | if (!(0, fs_1.existsSync)(configPath)) {
|
18 | throw new Error(`Could not find TypeScript configuration file "${tsConfigPath}".`);
|
19 | }
|
20 | const pluginsConfig = (0, get_value_or_default_1.getValueOrDefault)(configuration, 'compilerOptions.plugins', appName);
|
21 | const plugins = this.pluginsLoader.load(pluginsConfig);
|
22 | const relativeRootPath = (0, path_1.dirname)((0, path_1.relative)(cwd, configPath));
|
23 | const sourceRoot = (0, get_value_or_default_1.getValueOrDefault)(configuration, 'sourceRoot', appName);
|
24 | const pathToSource = (0, path_1.normalize)(sourceRoot).indexOf((0, path_1.normalize)(relativeRootPath)) >= 0
|
25 | ? (0, path_1.join)(cwd, sourceRoot)
|
26 | : (0, path_1.join)(cwd, relativeRootPath, sourceRoot);
|
27 | const entryFile = (0, get_value_or_default_1.getValueOrDefault)(configuration, 'entryFile', appName);
|
28 | const entryFileRoot = (0, get_value_or_default_1.getValueOrDefault)(configuration, 'root', appName) || '';
|
29 | const defaultOptions = (0, webpack_defaults_1.webpackDefaultsFactory)(pathToSource, entryFileRoot, entryFile, isDebugEnabled, tsConfigPath, plugins);
|
30 | const projectWebpackOptions = typeof webpackConfigFactoryOrConfig !== 'function'
|
31 | ? webpackConfigFactoryOrConfig
|
32 | : webpackConfigFactoryOrConfig(defaultOptions, webpack);
|
33 | const webpackConfiguration = Object.assign(Object.assign(Object.assign({}, defaultOptions), { mode: watchMode ? 'development' : defaultOptions.mode }), projectWebpackOptions);
|
34 | const compiler = webpack(webpackConfiguration);
|
35 | const afterCallback = (err, stats) => {
|
36 | if (err && stats === undefined) {
|
37 |
|
38 |
|
39 | console.log(err);
|
40 | return process.exit(1);
|
41 | }
|
42 | const statsOutput = stats.toString({
|
43 | chunks: false,
|
44 | colors: true,
|
45 | modules: false,
|
46 | assets: false,
|
47 | });
|
48 | if (!err && !stats.hasErrors()) {
|
49 | if (!onSuccess) {
|
50 | assetsManager.closeWatchers();
|
51 | }
|
52 | else {
|
53 | onSuccess();
|
54 | }
|
55 | }
|
56 | else if (!watchMode && !webpackConfiguration.watch) {
|
57 | console.log(statsOutput);
|
58 | return process.exit(1);
|
59 | }
|
60 | console.log(statsOutput);
|
61 | };
|
62 | if (watchMode || webpackConfiguration.watch) {
|
63 | compiler.hooks.watchRun.tapAsync('Rebuild info', (params, callback) => {
|
64 | console.log(`\n${ui_1.INFO_PREFIX} Webpack is building your sources...\n`);
|
65 | callback();
|
66 | });
|
67 | compiler.watch(webpackConfiguration.watchOptions || {}, afterCallback);
|
68 | }
|
69 | else {
|
70 | compiler.run(afterCallback);
|
71 | }
|
72 | }
|
73 | }
|
74 | exports.WebpackCompiler = WebpackCompiler;
|