-
Notifications
You must be signed in to change notification settings - Fork 3
/
webpack.config.js
283 lines (266 loc) · 8.47 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import path from "path"
import webpack from "webpack"
import ExtractTextPlugin from "extract-text-webpack-plugin"
import { phenomicLoader } from "phenomic"
import PhenomicLoaderFeedWebpackPlugin
from "phenomic/lib/loader-feed-webpack-plugin"
import pkg from "./package.json"
export default (config = {}) => {
const postcssPlugins = () => [
require("stylelint")(),
require("postcss-cssnext")({
browsers: "last 2 versions",
features: {
customProperties: {
variables: {
mainColor: "#f01",
mainColor2: "#c00",
mainColorContrasted: "#eee",
},
},
},
}),
require("postcss-reporter")(),
...!config.production ? [
require("postcss-browser-reporter")(),
] : [],
]
return {
...config.dev && {
devtool: "#cheap-module-eval-source-map",
},
module: {
noParse: /\.min\.js/,
// webpack 1
loaders: [
// webpack 2
/*
rules: [
*/
// *.md => consumed via phenomic special webpack loader
// allow to generate collection and rss feed.
{
// phenomic requirement
test: /\.md$/,
loader: phenomicLoader,
query: {
context: path.join(__dirname, config.source),
// plugins: [
// ...require("phenomic/lib/loader-preset-markdown").default
// ]
// see https://phenomic.io/docs/usage/plugins/
},
},
// *.json => like in node, return json
// (not handled by webpack by default)
{
test: /\.json$/,
loader: "json-loader",
},
// *.js => babel + eslint
{
test: /\.js$/,
include: [
path.resolve(__dirname, "scripts"),
path.resolve(__dirname, "src"),
],
loaders: [
"babel-loader?cacheDirectory",
"eslint-loader" + (config.dev ? "?emitWarning" : ""),
],
},
// ! \\
// by default *.css files are considered as CSS Modules
// And *.global.css are considered as global (normal) CSS
// *.css => CSS Modules
{
test: /\.css$/,
exclude: /\.global\.css$/,
include: path.resolve(__dirname, "src"),
// webpack 1
loader: ExtractTextPlugin.extract(
"style-loader",
[ `css-loader?modules&localIdentName=${
config.production
? "[hash:base64:5]"
: "[path][name]--[local]--[hash:base64:5]"
}`,
"postcss-loader",
].join("!"),
),
// webpack 2
/*
loader: ExtractTextPlugin.extract({
fallbackLoader: "style-loader",
loader: [
{
loader: "css-loader",
query: {
modules: true,
localIdentName: (
config.production
? "[hash:base64:5]"
: "[path][name]--[local]--[hash:base64:5]"
),
},
},
{
loader: "postcss-loader",
// query for postcss can't be used right now
// https://github.com/postcss/postcss-loader/issues/99
// meanwhile, see webpack.LoaderOptionsPlugin in plugins list
// query: { plugins: postcssPlugins },
},
],
}),
*/
},
// *.global.css => global (normal) css
{
test: /\.global\.css$/,
include: path.resolve(__dirname, "src"),
// webpack 1
loader: ExtractTextPlugin.extract(
"style-loader",
[ "css-loader", "postcss-loader" ].join("!"),
),
// webpack 2
/*
loader: ExtractTextPlugin.extract({
fallbackLoader: "style-loader",
loader: [
"css-loader",
{
loader: "postcss-loader",
// query for postcss can't be used right now
// https://github.com/postcss/postcss-loader/issues/99
// meanwhile, see webpack.LoaderOptionsPlugin in plugins list
// query: { plugins: postcssPlugins },
},
],
}),
*/
},
// ! \\
// If you want global CSS only, just remove the 2 sections above
// and use the following one
// ! \\ If you want global CSS for node_modules only, just uncomment
// this section and the `include` part
// {
// test: /\.css$/,
// // depending on your need, you might need to scope node_modules
// // for global CSS if you want to keep CSS Modules by default
// // for your own CSS. If so, uncomment the line below
// // include: path.resolve(__dirname, "node_modules"),
// loader: ExtractTextPlugin.extract(
// fallbackLoader: "style-loader",
// loader: [
// "css-loader",
// {
// loader: "postcss-loader",
// query: { plugins: postcssPlugins },
// },
// ),
// },
// ! \\ if you want to use Sass or LESS, you can add sass-loader or
// less-loader after postcss-loader (or replacing it).
// ! \\ You will also need to adjust the file extension
// and to run the following command
//
// Sass: `npm install --save-dev node-sass sass-loader`
// https://github.com/jtangelder/sass-loader
//
// LESS: npm install --save-dev less less-loader
// https://github.com/webpack/less-loader
// copy assets and return generated path in js
{
test: /\.(html|ico|jpe?g|png|gif)$/,
loader: "file-loader",
query: {
name: "[path][name].[hash].[ext]",
context: path.join(__dirname, config.source),
},
},
// svg as raw string to be inlined
{
test: /\.svg$/,
loader: "raw-loader",
},
],
},
// webpack 1
postcss: postcssPlugins,
plugins: [
// webpack 2
/*
// You should be able to remove the block below when the following
// issue has been correctly handled (and postcss-loader supports
// "plugins" option directly in query, see postcss-loader usage above)
// https://github.com/postcss/postcss-loader/issues/99
new webpack.LoaderOptionsPlugin({
test: /\.css$/,
options: {
postcss: postcssPlugins,
// required to avoid issue css-loader?modules
// this is normally the default value, but when we use
// LoaderOptionsPlugin, we must specify it again, otherwise,
// context is missing (and css modules names can be broken)!
context: __dirname,
},
}),
*/
new PhenomicLoaderFeedWebpackPlugin({
// here you define generic metadata for your feed
feedsOptions: {
title: pkg.name,
site_url: pkg.homepage,
},
feeds: {
// here we define one feed, but you can generate multiple, based
// on different filters
"feed.xml": {
collectionOptions: {
filter: { layout: "Post" },
sort: "date",
reverse: true,
limit: 20,
},
},
},
}),
// webpack 1
new ExtractTextPlugin("[name].[hash].css", { disable: config.dev }),
// webpack 2
/*
new ExtractTextPlugin({
filename: "[name].[hash].css",
disable: config.dev,
}),
*/
...config.production && [
// webpack 2
// DedupePlugin does not work correctly with Webpack 2, yet ;)
// https://github.com/webpack/webpack/issues/2644
new webpack.optimize.DedupePlugin(),
new webpack.optimize.UglifyJsPlugin(
{ compress: { warnings: false } }
),
],
],
output: {
path: path.join(__dirname, config.destination),
publicPath: config.baseUrl.pathname,
filename: "[name].[hash].js",
},
// webpack 1
resolve: {
extensions: [ ".js", ".json", "" ],
root: [ path.join(__dirname, "node_modules") ],
},
resolveLoader: { root: [ path.join(__dirname, "node_modules") ] },
// webpack 2
/*
resolve: { extensions: [ ".js", ".json" ] },
*/
}
}