-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
215 lines (183 loc) · 5.68 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
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
205
206
207
208
209
210
211
212
213
214
215
/* eslint-disable @typescript-eslint/no-var-requires */
// UTILITIES
const currentTask = process.env.npm_lifecycle_event;
const fse = require('fs-extra');
const path = require('path');
// WEBPACK PLUGINS
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const StyleLintPlugin = require('stylelint-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
// POSTCSS PLUGINS
const colorModFunction = require('postcss-color-mod-function');
const atImport = require('postcss-import');
const atEach = require('postcss-each');
const atRulesVariables = require('postcss-at-rules-variables');
const atIf = require('postcss-conditionals');
const atFor = require('postcss-for');
const cssVariables = require('postcss-css-variables');
const cssSimpleVariables = require('postcss-simple-vars');
const nested = require('postcss-nested');
const autoprefixer = require('autoprefixer');
const mixins = require('postcss-mixins');
const math = require('postcss-math');
// POSTCSS PLUGINS CONFIG
const postcssPlugins = [
atRulesVariables({ /* atRules: ['media'] */ }),
atImport({
plugins: [
atRulesVariables({ /* options */ }),
atImport()
]
}),
atEach(),
atFor(),
atIf(),
mixins(),
cssSimpleVariables(),
cssVariables({ preserve: true }),
math(),
colorModFunction(),
nested(),
autoprefixer(),
];
// CONFIG
class RunAfterCompile {
apply(compiler) {
compiler.hooks.done.tap('Copy images', function () {
fse.copySync('./src/assets/images', './dist/assets/images');
fse.copySync('./src/assets/icons', './dist/assets/icons');
});
}
}
// CONFIG
let cssConfig = {
test: /\.css$/i,
use: [
'css-loader?url=false',
{
loader: 'postcss-loader',
options: {
plugins: postcssPlugins,
},
},
],
};
// CONFIG
let eslintConfig = {
test: /\.js$/,
enforce: 'pre',
exclude: /node_modules/,
use: ['eslint-loader'],
};
// CONFIG
let pages = fse
.readdirSync('src')
.filter(function (file) {
return file.endsWith('.html');
})
.map(function (page) {
return new HtmlWebpackPlugin({
filename: page,
template: `./src/${page}`,
});
});
/* START COMMON CONFIGURATION
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >
------------------------------------------------------------------------------------------------------------- */
// SET BASE CONFIGURATION
let config = {
entry: './src/main.ts',
plugins: pages,
resolve: {
extensions: ['.tsx', '.ts', '.js'],
},
// configuration regarding modules
module: {
// rules for modules (configure loaders, parser options, etc.)
rules: [cssConfig, eslintConfig],
},
};
// ADD PLUGINS TO CONFIGURATION
config.plugins.push(
new StyleLintPlugin({
configFile: '.stylelintrc',
context: 'src/styles/',
files: ['**/*.css'],
failOnError: false,
quiet: false,
})
);
// ADD RULES TO CONFIGURATION
config.module.rules.push({
test: /\.tsx?$/,
exclude: /(node_modules)/,
use: {
// the loader which should be applied, it'll be resolved relative to the context
loader: 'ts-loader',
},
});
// END COMMON CONFIGURATION
/* START DEVELOPMENT CONFIGURATION
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >
------------------------------------------------------------------------------------------------------------- */
if (currentTask == 'dev') {
// add loader to start of array
cssConfig.use.unshift('style-loader');
// options related to how webpack emits results
config.output = {
filename: '[name].bundle.js',
// the target directory for all output files
// must be an absolute path (use the Node.js path module)
path: path.resolve(__dirname, 'src'),
};
config.devServer = {
before: function (app, server) {
server._watch(`src/**/*.html`);
},
contentBase: path.resolve(__dirname, './src'),
hot: true, // hot module replacement. Depends on HotModuleReplacementPlugin
port: 3000,
host: '0.0.0.0',
noInfo: false, // only errors & warns on hot reload
historyApiFallback: true, // for SPAs, always serve index.html
};
// Chosen mode tells webpack to use its built-in optimizations accordingly.
config.mode = 'development';
// set devtools with source maps
config.devtool = 'source-map';
}
// END DEVELOPMENT CONFIGURATION
/* START PRODUCTION CONFIGURATION
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >
------------------------------------------------------------------------------------------------------------- */
if (currentTask == 'build') {
// add loader to start of array
cssConfig.use.unshift(MiniCssExtractPlugin.loader);
// add plugin to end of postcss plugin array
postcssPlugins.push(require('cssnano'));
// options related to how webpack emits results
config.output = {
// the target directory for all output files
// must be an absolute path (use the Node.js path module)
path: path.resolve(__dirname, 'dist'),
// the filename template for entry chunks
filename: `[name].[hash:10].js`,
chunkFilename: `./scripts/[name].js`,
};
// Chosen mode tells webpack to use its built-in optimizations accordingly.
config.mode = 'production';
config.optimization = {
splitChunks: { chunks: 'all' },
};
config.plugins.push(
new CleanWebpackPlugin(),
new MiniCssExtractPlugin({
// the filename template for entry chunks
filename: `styles.[chunkhash].css`,
}),
new RunAfterCompile()
);
}
// END PRODUCTION CONFIGURATION
module.exports = config;