-
Notifications
You must be signed in to change notification settings - Fork 9
/
webpack.config.js
164 lines (154 loc) Β· 4.37 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
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const StyleLintPlugin = require('stylelint-webpack-plugin');
module.exports = env => {
const theme = env.theme;
const thememode = env.darkmode ? 'dark' : 'light';
const production = env.production;
const willMinify = production ? '.min' : '';
const documentation = env.documentation;
const externals = (env.noexternals) ? ['flatpickr','jquery','popper.js','chart.js'] : '';
const noextFlag = (env.noexternals) ? '-noext' : '';
const entry = setEntry(noextFlag,theme,thememode);
let outputPath = path.resolve(__dirname, 'dist');
if (documentation) {
entry.normal.push(path.resolve(__dirname, 'src/js/docs.js'));
entry.normal.push(path.resolve(__dirname, `src/js/docs/search-${theme}.js`));
entry.normal.push(path.resolve(__dirname, `src/scss/${thememode}-docs-${theme}.scss`));
entry.darkmode.push(path.resolve(__dirname, `src/scss/dark-docs-${theme}.scss`));
outputPath = path.resolve(__dirname, '_gh_pages');
}
return {
entry,
output: {
filename: (chunkData) => {
return chunkData.chunk.name === 'normal' ? `bootstrap-sncf${noextFlag + willMinify}.js` : '.generated-by-webpack'
},
path: outputPath
},
devtool: production ? 'none' : 'source-map',
mode: production ? 'production' : 'development',
externals: externals,
module: {
rules: [
{
enforce: 'pre',
test: /\.js$/,
exclude: /node_modules/,
loader: 'eslint-loader',
},
{
test: /\.js$/,
exclude: /node_modules\/(?!(dom7|swiper)\/).*/,
use: {
loader: 'babel-loader',
options: {
presets: ['env'],
plugins: ['transform-class-properties', 'transform-object-rest-spread']
}
}
},
parseCSS(noextFlag),
parseAssets(noextFlag),
{
test: require.resolve('jquery'),
use: [{
loader: 'expose-loader',
options: '$'
},{
loader: 'expose-loader',
options: 'jQuery'
},{
loader: 'expose-loader',
options: 'window.jQuery'
},{
loader: 'expose-loader',
options: 'window.$'
}]
}
]
},
plugins: getPlugins(noextFlag,willMinify)
}
};
function setEntry(noextFlag,theme,thememode) {
if (noextFlag === '') {
return {
normal: [
path.resolve(__dirname, `src/js/${theme}.js`),
path.resolve(__dirname, `src/scss/${thememode}-${theme}.scss`)
],
darkmode: [
path.resolve(__dirname, `src/scss/dark-${theme}.scss`)
]
}
} else {
return {
normal: [
path.resolve(__dirname, `src/js/${theme}.js`),
]
}
}
}
function parseAssets(noextFlag) {
if (noextFlag === '') {
return {
test: /\.(woff|woff2|eot|ttf|otf|svg)$/,
include: path.resolve(__dirname, 'src/assets/fonts'),
use: [{
loader: 'file-loader',
options: {
limit: 100000,
name: 'assets/fonts/[name].[ext]'
}
}],
}
} else {
return {}
}
}
function parseCSS(noextFlag) {
if (noextFlag === '') {
return {
test: /\.(scss|css)$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader', options: { sourceMap: true } // translates CSS into CommonJS modules
}, {
loader: 'postcss-loader',
options: {
config: {
path: path.resolve(__dirname, 'build/postcss.config.js'),
},
sourceMap: true
}
}, {
loader: 'sass-loader', options: { sourceMap: true }
}]
}
} else {
return {}
}
}
function getPlugins(noextFlag,willMinify) {
if (noextFlag === '') {
return [
new MiniCssExtractPlugin({
moduleFilename: ({ name }) => (name === 'normal') ? `bootstrap-sncf${noextFlag + willMinify}.css` : `bootstrap-sncf${noextFlag}.darkmode${willMinify}.css`
}),
new StyleLintPlugin(),
new CopyWebpackPlugin([
{
from: '**/*',
to: 'assets/img'
}
], {
context: path.resolve(__dirname, 'src/assets/img')
})
]
} else {
return []
}
}