-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathwebpack.config.js
146 lines (131 loc) · 4.06 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
const webpack = require("webpack")
const path = require('path')
// const Offline = require('offline-plugin')
const OptimizeJsPlugin = require("optimize-js-plugin")
const Extract = require('extract-text-webpack-plugin')
// debug?
const isDebug = process.env.NODE_ENV !== "production"
// load and process styles
const CSS_LOADERS = [
`css-loader?${JSON.stringify({
sourceMap: isDebug,
modules: false,
importLoaders: 1,
localIdentName: isDebug ? '[name]_[local]_[hash:base64:3]' : '[hash:base64:4]',
minimize: !isDebug,
})}`,
'postcss-loader',
]
const extract_css = new Extract({ filename: 'style.css', allChunks: true })
// Webpack configuration
// http://webpack.github.io/docs/configuration.html
let u = {
// Developer tool to enhance debugging, source maps
// http://webpack.github.io/docs/configuration.html#devtool
devtool: isDebug ? 'inline-source-map' : false,
// Options affecting the normal modules
// https://webpack.github.io/docs/list-of-loaders.html
module: {
loaders: [
{
exclude: /node_modules/,
loader: "babel-loader",
query: { cacheDirectory: true, compact: true },
test: /\.(jsx|js|es|es6|mjs)$/,
}
]
},
// http://webpack.github.io/docs/configuration.html#resolve
resolve: {
alias: {
// components: path.resolve('src/components/'),
}
},
node: {
__filename: true,
__dirname: true,
},
output: {
devtoolModuleFilenameTemplate: "[absolute-resource-path]",
filename: "[name].js",
chunkFilename: "[id].js",
sourceMapFilename: "[name].js.map",
path: path.resolve(__dirname, './assets/build'),
publicPath: '/',
},
}
const defaultPlugins = () => [
new webpack.NoErrorsPlugin,
new webpack.LoaderOptionsPlugin({
minimize: isDebug,
debug: isDebug,
options: {
context: __dirname,
postcss: [
require('precss'),
require('postcss-calc')(),
require('pleeease-filters')(),
require('pixrem')(),
require('autoprefixer')(),
]
}
}),
new webpack.NamedModulesPlugin(),
new webpack.ProvidePlugin({
React: 'react',
DOM: 'react-dom',
ReactDOM: 'react-dom',
utils: 'universal-utils',
}),
new OptimizeJsPlugin({sourceMap: false})
// new Offline({
// ServiceWorker: {
// events: true,
// caches: {
// main: ['/', '/**.js'],
// }
// }
// }),
]//.concat(isDebug ? [new webpack.HotModuleReplacementPlugin] : [])
let configs = [
// browser
{
entry: {
"app.min": ["./assets/js/app.js"],
},
target: "web",
plugins: defaultPlugins().concat([extract_css]),
module: {
loaders: u.module.loaders.concat([
{
test: /\.(scss|sass|css)/, // load sass, scss, or css files and compile
exclude: /node_modules/, // find modules NOT imported from node_modules
loaders: extract_css.extract(CSS_LOADERS)
},
{
test: /\.(scss|sass|css)/, // load sass, scss, or css files and compile
include: /node_modules/, // find files imported from node_modules
loader: extract_css.extract('css?sourceMap&minimize')
},
{
test: /\.(png|jpg|jpeg|gif|svg|woff|woff2)$/,
loader: 'url-loader?limit=100000',
},
])
},
},
]
configs = configs.map(x => Object.assign({}, u, x))
// Optimize the bundle in release (production) mode
const productionify = (c => {
if(!isDebug){
// In Webpack 2: the occurrence order plugin is on by default and is no longer needed
// See https://gist.github.com/sokra/27b24881210b56bbaff7#occurrence-order for more info
c.plugins.push(new webpack.optimize.UglifyJsPlugin({ compress: { warnings: false } }))
// c.plugins.push(new OptimizeJsPlugin({sourceMap: false}))
c.plugins.push(new webpack.optimize.AggressiveMergingPlugin)
}
return c
})
configs[0] = productionify(configs[0])
module.exports = {default: configs[0], isDebug }