-
Notifications
You must be signed in to change notification settings - Fork 2
/
webpack.config.js
112 lines (100 loc) · 2.44 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
const webpack = require('webpack'); // eslint-disable-line
const path = require('path');
const glob = require('glob');
const SWPrecacheWebpackPlugin = require('sw-precache-webpack-plugin');
module.exports = (config = {}, param = {}) => {
const { dev } = param;
// Fixes npm packages that depend on `fs` module
config.node = {
fs: 'empty'
};
config.mode = process.env.NODE_ENV === 'production' ? 'production' : 'development';
config.resolve = {
modules: ['node_modules', './app']
};
config.module = {
rules: [
...config.module && config.module.rules ? config.module.rules : [],
{
test: /\.css$/,
use: ['babel-loader', 'raw-loader', {
loader: 'postcss-loader',
options: {
sourceMap: dev
}
}]
},
{
test: /\.s(a|c)ss$/,
use: ['babel-loader', 'raw-loader', {
loader: 'postcss-loader',
options: {
sourceMap: dev ? 'inline' : false
}
},
{
loader: 'sass-loader',
options: {
sourceMap: dev,
includePaths: ['styles', 'node_modules']
.map(d => path.join(__dirname, d))
.map(g => glob.sync(g))
.reduce((a, c) => a.concat(c), [])
}
}
]
}
]
};
let plugins = [];
if (!process.env.JEST) plugins = [...config.plugins];
// Enable Only in Development
if (dev) {
config.module.rules.push({
test: /\.js$/,
enforce: 'pre',
exclude: /node_modules/,
loader: 'eslint-loader',
options: {
emitWarning: dev
// configFile: path.resolve('./.eslintrc.js')
}
});
}
/* Enable only in Production */
if (!dev) {
const oldEntry = config.entry;
config.entry = () => oldEntry()
.then((entry) => {
if (entry['main.js']) entry['main.js'].push(path.resolve('./app/utils/offline'));
return entry;
});
config.plugins = [
...plugins,
new SWPrecacheWebpackPlugin({
cacheId: 'next-ss',
filepath: './app/static/sw.js',
minify: true,
staticFileGlobsIgnorePatterns: [/\.next\//],
staticFileGlobs: [
'static/**/*' // Precache all static files by default
],
runtimeCaching: [
// Example with different handlers
{
handler: 'fastest',
urlPattern: /[.](png|jpg|css)/
},
{
handler: 'networkFirst',
urlPattern: /^http.*/ // cache all files
}
]
}),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('production')
})
];
}
return config;
};