-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
226 lines (208 loc) · 7.1 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
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 PhenomicLoaderSitemapWebpackPlugin
from "phenomic/lib/loader-sitemap-webpack-plugin"
import pkg from "./package.json"
export default (config = {}) => {
// hot loading for postcss config
// until this is officially supported
// https://github.com/postcss/postcss-loader/issues/66
const postcssPluginFile = require.resolve("./postcss.config.js")
const postcssPlugins = (webpackInstance) => {
webpackInstance.addDependency(postcssPluginFile)
delete require.cache[postcssPluginFile]
return require(postcssPluginFile)(config)
}
return {
...config.dev && {
devtool: "#cheap-module-eval-source-map",
},
module: {
noParse: /\.min\.js/,
rules: [
// *.md => consumed via phenomic special webpack loader
// allow to generate collection and rss feed.
{
// phenomic requirement
test: /\.(md|markdown)$/,
loader: phenomicLoader,
query: {
context: path.join(__dirname, config.source),
// plugins: [
// ...require("phenomic/lib/loader-preset-markdown").default
// ]
// see https://phenomic.io/docs/usage/plugins/
},
},
// *.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"),
loader: ExtractTextPlugin.extract({
fallback: "style-loader",
use: [
{
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"),
loader: ExtractTextPlugin.extract({
fallback: "style-loader",
use: [
"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({
fallback: "style-loader",
use: [
"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|eot|otf|webp|ttf|woff|woff2)$/,
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",
},
],
},
plugins: [
// 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,
},
},
},
}),
new PhenomicLoaderSitemapWebpackPlugin({
site_url: pkg.homepage,
}),
new ExtractTextPlugin({
filename: "[name].[hash].css",
disable: config.dev,
}),
...config.production && [
new webpack.optimize.UglifyJsPlugin(
{ compress: { warnings: false } }
),
],
],
output: {
path: path.join(__dirname, config.destination),
publicPath: config.baseUrl.pathname,
filename: "[name].[hash].js",
},
resolve: { extensions: [ ".js", ".json" ] },
}
}