This repository has been archived by the owner on May 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-webpack-config.js
140 lines (140 loc) · 4.08 KB
/
create-webpack-config.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
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
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const webpack_1 = require('webpack');
const CircularDependencyPlugin = require('circular-dependency-plugin');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const nodeExternals = require('webpack-node-externals');
const tsconfig_paths_webpack_plugin_1 = require('tsconfig-paths-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const path_1 = require('path');
const transformers_1 = require('./transformers');
function getAliases(options) {
return options.fileReplacements.reduce(
(aliases, replacement) => ({
...aliases,
[replacement.replace]: replacement.with,
}),
{},
);
}
function getStatsConfig(options) {
return {
hash: true,
timings: false,
cached: false,
cachedAssets: false,
modules: false,
warnings: true,
errors: true,
colors: !options.verbose && !options.statsJson,
chunks: !options.verbose,
assets: !!options.verbose,
chunkOrigins: !!options.verbose,
chunkModules: !!options.verbose,
children: !!options.verbose,
reasons: !!options.verbose,
version: !!options.verbose,
errorDetails: !!options.verbose,
moduleTrace: !!options.verbose,
usedExports: !!options.verbose,
};
}
function createWebpackConfig(options) {
const extensions = ['.ts', '.mjs', '.js'];
const mainFields = ['module', 'main'];
const webpackConfig = {
entry: {
main: [options.main],
},
target: 'node',
externals: [
nodeExternals({
modulesDir: path_1.resolve(options.root, '../../node_modules'),
}),
],
devtool: options.sourceMap && 'source-map',
mode: options.optimization ? 'production' : 'development',
output: {
path: options.outputPath,
filename: 'main.js',
},
module: {
rules: [
{
test: /\.(html|css)$/,
loader: 'raw-loader',
},
{
test: /\.(j|t)sx?$/,
loader: 'ts-loader',
options: {
configFile: options.tsConfig,
transpileOnly: true,
getCustomTransformers: program => ({
before: [transformers_1.inlineFilesTransformer(program), transformers_1.stripStylesTransformer(program)],
}),
},
},
],
},
resolve: {
extensions,
alias: getAliases(options),
plugins: [
new tsconfig_paths_webpack_plugin_1.default({
configFile: options.tsConfig,
extensions,
mainFields,
}),
],
mainFields,
},
performance: {
hints: false,
},
plugins: [
new ForkTsCheckerWebpackPlugin({
tsconfig: options.tsConfig,
useTypescriptIncrementalApi: options.useTypescriptIncrementalApi,
workers: options.useTypescriptIncrementalApi
? ForkTsCheckerWebpackPlugin.ONE_CPU
: options.maxWorkers || ForkTsCheckerWebpackPlugin.TWO_CPUS_FREE,
}),
],
stats: getStatsConfig(options),
};
const extraPlugins = [];
if (options.progress) {
extraPlugins.push(new webpack_1.ProgressPlugin());
}
// process asset entries
if (options.assets) {
const copyWebpackPluginPatterns = options.assets.map(asset => {
return {
context: asset.input,
// Now we remove starting slash to make Webpack place it from the output root.
to: asset.output,
ignore: asset.ignore,
from: {
glob: asset.glob,
dot: true,
},
};
});
const copyWebpackPluginOptions = {
ignore: ['.gitkeep', '**/.DS_Store', '**/Thumbs.db'],
};
const copyWebpackPluginInstance = new CopyWebpackPlugin(copyWebpackPluginPatterns, copyWebpackPluginOptions);
extraPlugins.push(copyWebpackPluginInstance);
}
if (options.showCircularDependencies) {
extraPlugins.push(
new CircularDependencyPlugin({
exclude: /[\\\/]node_modules[\\\/]/,
}),
);
}
webpackConfig.plugins = [...webpackConfig.plugins, ...extraPlugins];
return webpackConfig;
}
exports.createWebpackConfig = createWebpackConfig;