-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
197 lines (192 loc) · 6 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
const path = require('path')
const fs = require('fs')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const projectPath = require(path.resolve(process.env.PWD + '/paths.config.js'))
const packageJson = require(path.resolve(process.env.PROJECT_CWD + '/package.json'))
const srcPath = path.join(process.env.PROJECT_CWD, './src')
const distPath = path.join(process.env.PROJECT_CWD, './web-ui/assets/js')
const getProjectConfig = () => `${process.env.PROJECT_CWD}/project.config.js`
const config = require(getProjectConfig())
let babelConfig
try {
fs.accessSync(`${process.env.PROJECT_CWD}/babel.config.js`, fs.constants.R_OK | fs.constants.W_OK)
babelConfig = `${process.env.PROJECT_CWD}/babel.config.js`
console.error('✳️ using local babel config!')
} catch (err) {
console.error('⚛︎ using Taskrunner babel config!')
babelConfig = `./babel.config.js`
}
const aliasEntries = () => {
const aliases = projectPath.webpackAlias
const paths = {}
for (const key in aliases) {
paths[key] = path.resolve(process.env.PROJECT_CWD, aliases[key])
}
return paths
}
/**
* Resources for lazy loading:
* https://medium.com/front-end-hacking/webpack-and-dynamic-imports-doing-it-right-72549ff49234
* https://webpack.js.org/guides/public-path/
*/
module.exports = {
context: srcPath,
bail: true, // force webpack to exit on error
entry: {
app: config.taskrunner.ts ? './assets/js/app.ts' : './assets/js/app.js',
global: './globals/style/style.global.scss',
styleguide: './styleguide/style/styleguide.global.scss',
},
output: {
path: distPath,
chunkFilename: 'jvm-[name].js',
filename: 'jvm-[name].js'
},
resolve: {
extensions: ['.ts', '.js', '.scss'],
alias: {
...aliasEntries()
}
},
plugins: [
new CleanWebpackPlugin({
// clean webui js folder before watch and build
cleanAfterEveryBuildPatterns: ['*']
}),
new MiniCssExtractPlugin({
// path is relative to the distPath, see prod path
filename: '../css/[name].css',
chunkFilename: '../css/[id].css'
})
],
module: {
rules: [
// handle modules css
{
// test: /\.s[ac]ss$/i,
test: /\.module\.scss$/,
use: [
// Creates `style` nodes from JS strings
{
loader: 'style-loader',
options: {
esModule: false
}
},
// Translates CSS into CommonJS
{
loader: 'css-loader',
options: {
esModule: false,
modules: false,
sourceMap: true
}
},
// Add autoprefixer to css
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: [
require('autoprefixer')
]
}
}
},
// Compiles Sass to CSS
{
loader: 'sass-loader',
options: {
additionalData: (content, loaderContext) => {
// Inject global scss vars/mixins before each module
const { rootContext } = loaderContext
const injectPath = path.resolve(rootContext, './globals/style/_modules_inject')
const versionString = '/*! @preserve: Version: '+packageJson.version+', Build date: '+ new Date().toISOString() + ' */\n\n'
return `${versionString} @import "${injectPath}"; ${content}`
},
sourceMap: true
}
},
],
},
// handle global css
{
test: /\.global\.scss$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
esModule: false
},
},
// Translates CSS into CommonJS
{
loader: 'css-loader',
options: {
esModule: false,
url: false, // use relative urls to to the css folder
modules: false,
sourceMap: true
}
},
// Add autoprefixer to css
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: [
require('autoprefixer')
]
}
}
},
// Compiles Sass to CSS
{
loader: 'sass-loader',
options: {
sassOptions: () => {
// we include the src (/src/assets) path as our base url for file imports in the scss files
// any url() imports in the css should be set relative to the assets folder
return {
includePaths: [srcPath + '/']
}
},
additionalData: (content) => {
// Inject package version
const versionString = '/*! @preserve: Version: '+packageJson.version+', Build date: '+ new Date().toISOString() + ' */\n\n'
return `${versionString} ${content}`
},
sourceMap: true
}
},
]
},
// handle js/ts
{
test: /^(?!.*\.config\.(ts|js)$).*\.(ts|js)$/,
exclude: /(node_modules)/,
/* if you decide to bundle GSAP and not use the umd verion you would need to
* use include instead of exclude to properly babelify GSAP or it fails on older browsers.
*/
// include: [
// srcPath,
// /(node_modules\/gsap)/
// ],
use: {
loader: 'babel-loader',
options: {
configFile: babelConfig
}
}
}
]
},
/** Externals
* In case you want to include external plugins and they need dependencies that are part of the bundle.
* Useful for some GSAP plugins like DrawSVGPlugin which are not yet optimized for Webpack.
**/
// externals: {
// 'TweenLite': 'TweenLite'
// },
}