-
Notifications
You must be signed in to change notification settings - Fork 15
/
webpack.config.js
260 lines (245 loc) · 8.75 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
'use strict'
//https://github.com/wbkd/webpack-starter/blob/master/webpack/webpack.config.prod.js
const webpack = require('webpack')
//const autoprefixer = require('autoprefixer')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const FixStyleOnlyEntriesPlugin = require("webpack-fix-style-only-entries")
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const apiMocker = require('mocker-api');
const path = require("path");
const { preprocess } = require('./svelte.config');
module.exports = function(env, argv) {
const CONTENT_BASE = argv.contentBase
const CONTENT_PUBLIC = `${CONTENT_BASE}/public`
const MAIN_ENTRY = `${CONTENT_BASE}/src/main.js`
console.log("argv", argv)
let isProd = argv.mode === 'production'
let minDescriptor = ''// = isProd ? '.min' : '' //for now don't add min so grails dev is easier
let devtool = isProd ? 'source-map' : 'inline-source-map' //'eval-source-map'
let pathout = argv.pathout ? path.resolve(argv.pathout) : path.resolve('./dist')
let styleLoader = MiniCssExtractPlugin.loader //always use this as it shows the sourcemaps in browser
console.log("argv.mode", argv.mode); console.log("isProd", isProd); console.log("styleLoader", styleLoader);
let cfg = {
mode: argv.mode,// || "production", pass in with --mode
devtool: devtool,
entry: {
main: MAIN_ENTRY,
},
output: {
path: pathout, //path.join(__dirname, "dist"),
filename: (chunkData) => `[name]${minDescriptor}.js`,
libraryTarget: 'umd',
publicPath: '/'
},
optimization: {
//minimize: false,
chunkIds: "named",
moduleIds: 'hashed', //makes it so the vendor chunks are cached and not rebuilt everytime
splitChunks: {
cacheGroups: {
vendor: {
test: /node_modules/,
chunks: "all",
name: `vendor${minDescriptor}`,
priority: 10, //lower priority so it won't pick up jquery
enforce: true
}
}
}
},
module: {
rules: [
{ test: /\.js$/, loader: 'babel-loader', exclude: /(node_modules|bower_components)/},
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
{
test: /\.svelte$/,
use: {
loader: 'svelte-loader',
options: {
emitCss: true,
hotReload: true,
preprocess,
onwarn: (warning, onwarn) => {
//hides warnings for now in dev
if (warning.message.includes("unused export property")) return;
warning.code === 'css-unused-selector' || onwarn(warning);
}
}
}
},
{
test: /\.(scss|css|sass)$/,
//exclude: /themes\/.+\.scss$/,
use: [
{ loader: styleLoader },
// translates CSS into CommonJS
{ loader: 'css-loader',
options: {
// url: false, //FIXME setting to false because mincss loader is broken and blows up processing these
sourceMap: true
}
},
// Runs compiled CSS through postcss for vendor prefixing
{ loader: 'postcss-loader', options: { sourceMap: true } },
{ loader: 'sass-loader',
options: {
// Prefer `dart-sass`
implementation: require("sass"),
sourceMap: true,
sassOptions: {
quietDeps: true, // dont show warnings for sass dependencies
outputStyle: 'expanded' //'compressed', //try expanded too
}
}
}
]
},
{
// less is back, not really, but we need it for Framework7
test: /\.less$/i,
use: [
{ loader: styleLoader },
{ loader: "css-loader", options: { sourceMap: true } },
{ loader: "less-loader", options: { sourceMap: true } }
],
},
{
// Load all images as base64 encoding if they are smaller than 8192 bytes
//FIXME THIS IS BROKEN AND MAY NOT BE DOING ANYTHING
test: /\.(png|jpg|gif)$/,
use: [{
loader: 'url-loader',
options: {
// name(resourcePath, resourceQuery) {
// return isProd ? '[path][name].[ext]?hash=[hash:20]' : '[path][name].[ext]'
// },
name: '[path][name].[ext]?hash=[hash:20]',
// outputPath: 'images/',
// publicPath: '../images',
// useRelativePaths: true,
limit: 8192
}
}]
},
{
// Load all icons
test: /\.(eot|woff|woff2|svg|ttf)([\?]?.*)$/,
use: [{
loader: 'file-loader',
options: {
name: '[path][name].[ext]',
outputPath: 'assets/'
}
}]
},
{
test: /\.html$/,
use: [{
loader: 'raw-loader',
options: { esModule: false }
}]
}
] //end rules
},
plugins:[
new webpack.EnvironmentPlugin(['BASE_URL']),
//if the entry is a scss such as for themes this will remove the js file that is erroniouly created, wil be fixed in webpack5
new FixStyleOnlyEntriesPlugin(),
// config this to dump to file, by defualt it opens the page that analyses what sizes take up what
// new BundleAnalyzerPlugin(),
new HtmlWebpackPlugin({
//title: 'Custom template using lodash',
template: `${CONTENT_PUBLIC}/index.ejs`,
inject: false,
minify: false,
configPath: isProd ? './config.js' : '../config.js'
// excludeAssets: /theme.+\.css$/ //not working when we do it by hand like we are in the index.ejs
}),
new CopyWebpackPlugin([
{ from: path.resolve(CONTENT_PUBLIC) },
// { from: path.resolve(`${CONTENT_BASE}/config.js`)},
{ from: 'node_modules/jquery/dist/jquery.min.js', to: 'libs'}
]),
new MiniCssExtractPlugin({
filename: 'assets/[name].css',
}),
new webpack.DefinePlugin({
BASE_URL: JSON.stringify(process.env.BASE_URL || 'http://localhost:8080/')
})
],
//command line options
bail: true, //Fail out on the first error --bail
//profile: false //list info on whats going on
externals: {
jquery: 'jQuery',
$: "jquery",
jQuery: "jquery",
"window.jQuery": "jquery"
},
resolve: {
//aliases are not DRY, setup here for svelte, in jsconfig.json for vscode and babel for jest
alias: {
//for svelte since babel doesn't process
'@ag': path.resolve('./src/'),
'@yakit/core': path.resolve('./yakit/core/'),
'@yakit/ui': path.resolve('./yakit/ui/'),
'@yakit/svelte': path.resolve('./yakit/svelte/'),
'angle-grinder': path.resolve('./'),
svelte: path.resolve('node_modules', 'svelte'),
//demo
'~': path.resolve('./examples/demo/src/'),
},
extensions: ['.tsx', '.ts', '.mjs', '.js', '.svelte'],
mainFields: ['svelte', 'browser', 'module', 'main']
}
}
const isAnalyze = typeof process.env.BUNDLE_ANALYZE !== "undefined";
if (isAnalyze) {
cfg.plugins.push(new BundleAnalyzerPlugin())
}
if(isProd){
cfg.plugins.push(
new OptimizeCssAssetsPlugin({
cssProcessor: require('cssnano'),
cssProcessorOptions: {
map: {
inline: false,
},
discardComments: {
removeAll: true
},
discardUnused: false
},
canPrint: true
})
)
}
//add themes scss to entries
// ['light', 'dark', 'dark-red', 'dark-light', 'dark-green', 'dark-3'].forEach( (name) => {
// cfg.entry[`theme-${name}`] = `${CONTENT_BASE}/src/assets/themes/${name}.scss`
// })
// ['light', 'dark', 'dark-red', 'dark-light', 'dark-green', 'dark-3'].forEach( (name) => {
// cfg.entry[`theme-${name}`] = `./src/styles/themes/${name}.scss`
// })
cfg.devServer = {
before(app){
apiMocker(app, path.resolve('./examples/demo/mocker/index.js'))
},
//compress: true, //gzips before serving so we can see file size
disableHostCheck: true,
port: 3002,
host: '0.0.0.0'
// historyApiFallback: true,
//inline: false, //default:true script will be inserted in your bundle to take care of live reloading
}
return cfg
// let devConfig = getConfig(false)
// devConfig.entry = { 'lib': './src/app/app.js' }
}