-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
157 lines (135 loc) · 4.16 KB
/
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
'use strict';
const path = require('path');
const cwd = process.cwd();
const webpack = tars.require('webpack');
const TerserJsPlugin = tars.require('terser-webpack-plugin');
const staticFolderName = tars.config.fs.staticFolderName;
const compressJs = tars.flags.release || tars.flags.min || tars.flags.m;
const generateSourceMaps = tars.config.sourcemaps.js.active && tars.isDevMode;
const sourceMapsDest = tars.config.sourcemaps.js.inline ? 'inline-' : '';
const sourceMapsType = `#${sourceMapsDest}source-map`;
const webpackMode = !compressJs ? 'development' : 'production';
let outputFileNameTemplate = '[name]';
let modulesDirectories = ['node_modules'];
let rules = [
{
test: /\.js$/,
loader: 'source-map-loader',
enforce: 'pre'
}
];
let plugins = [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify(process.env.NODE_ENV)
}
})
];
let minimizers = [];
if (process.env.npmRoot) {
modulesDirectories.push(process.env.npmRoot);
}
if (compressJs) {
outputFileNameTemplate += `${tars.options.build.hash}.min`;
minimizers.push(
new TerserJsPlugin({
terserOptions: {
compress: {
/* eslint-disable camelcase */
drop_console: tars.config.js.removeConsoleLog,
drop_debugger: tars.config.js.removeConsoleLog
/* eslint-enable camelcase */
},
mangle: false
}
})
);
}
if (tars.config.js.webpack.providePlugin) {
plugins.push(
new webpack.ProvidePlugin(tars.config.js.webpack.providePlugin)
);
}
if (tars.options.watch.isActive && tars.config.js.webpack.useHMR) {
plugins.push(
new webpack.HotModuleReplacementPlugin()
);
}
if (tars.config.js.lint) {
rules.push(
{
test: /\.js$/,
loader: 'eslint-loader',
enforce: 'pre',
include: `${cwd}/markup`,
options: {
configFile: `${cwd}/.eslintrc`
}
}
);
}
if (tars.config.js.useBabel) {
rules.push(
{
test: /\.js$/,
loader: 'babel-loader',
include: /markup/
}
);
}
/**
* Add to each entry point entries for webpack dev-server and webpack-hot-middleware
* @param {Object} entryConfig
* @return {Object}
*/
function prepareEntryPoints(entryConfig) {
const useHMR = tars.config.js.webpack.useHMR;
let devServerEntryPoints = [
'webpack/hot/dev-server',
'webpack-hot-middleware/client?reload=true'
];
if (!useHMR || !tars.useLiveReload) {
return entryConfig;
}
// Take webpack dev-server and webpack-hot-middleware from TARS-CLI, if TARS has been started by TARS-CLI
if (process.env.npmRoot) {
devServerEntryPoints = devServerEntryPoints.map(devServerEntryPoint => process.env.npmRoot + devServerEntryPoint);
}
/* eslint-disable guard-for-in */
for (let entryPointName in entryConfig) {
entryConfig[entryPointName] = devServerEntryPoints.concat(entryConfig[entryPointName]);
}
/* eslint-disable guard-for-in */
return entryConfig;
}
module.exports = {
mode: webpackMode,
// We have to add some pathes to entry point in case of using HMR
entry: prepareEntryPoints({
main: path.resolve(`${cwd}/markup/${staticFolderName}/js/main.js`)
}),
output: {
path: path.resolve(`${cwd}/dev/${staticFolderName}/js`),
publicPath: `./${staticFolderName}/js/`,
filename: `${outputFileNameTemplate}.js`
},
devtool: generateSourceMaps ? sourceMapsType : false,
watch: tars.options.watch.isActive && !tars.config.js.webpack.useHMR,
module: {
rules
},
plugins,
resolveLoader: {
modules: modulesDirectories
},
optimization: {
minimizer: minimizers
},
resolve: {
alias: {
modules: path.resolve(`./markup/${tars.config.fs.componentsFolderName}`),
components: path.resolve(`./markup/${tars.config.fs.componentsFolderName}`),
static: path.resolve(`./markup/${staticFolderName}`)
}
}
};