-
Notifications
You must be signed in to change notification settings - Fork 818
/
webpack.config.js
108 lines (89 loc) · 2.77 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
// require('babel-register');
const env = process.env;
const NODE_ENV = process.env.NODE_ENV;
const isDev = NODE_ENV === 'development';
const isTest = NODE_ENV === 'test';
const webpack = require('webpack');
const marked = require('marked');
const fs = require('fs');
const path = require('path'),
join = path.join,
resolve = path.resolve;
const root = resolve(__dirname);
const src = join(root, 'src');
const examples = join(root, 'examples');
const modules = join(root, 'node_modules');
const dest = join(root, 'public');
const getConfig = require('hjs-webpack')
var config = getConfig({
isDev,
in: join(examples, 'index.js'),
out: dest,
clearBeforeBuild: true,
html: function(context, cb) {
context.publicPath = isDev ? 'http://localhost:3000/' : ''
fs.readFile(join(root, 'README.md'), (err, data) => {
if (err) {
return cb(err);
}
cb(null, {
'index.html': context.defaultTemplate(),
// 'readme.html': context.defaultTemplate({
// html: `<div id="readme">
// ${marked(data.toString('utf-8'))}
// </div>`,
// metaTags: {
// bootApp: false
// }
// })
})
})
}
});
const dotenv = require('dotenv');
const envVariables = dotenv.config();
// Converts keys to be surrounded with __
const defines =
Object.keys(envVariables)
.reduce((memo, key) => {
const val = JSON.stringify(envVariables[key]);
memo[`__${key.toUpperCase()}__`] = val;
return memo;
}, {
__NODE_ENV__: JSON.stringify(env.NODE_ENV),
__IS_DEV__: isDev
})
config.externals = {
'window.google': true
}
// Setup css modules require hook so it works when building for the server
const cssModulesNames = `${isDev ? '[path][name]__[local]__' : ''}[hash:base64:5]`;
const matchCssLoaders = /(^|!)(css-loader)($|!)/;
const findLoader = (loaders, match, fn) => {
const found = loaders.filter(l => l && l.loader && l.loader.match(match))
return found ? found[0] : null;
}
const cssloader = findLoader(config.module.loaders, matchCssLoaders);
const newloader = Object.assign({}, cssloader, {
test: /\.module\.css$/,
include: [src, examples],
loader: cssloader.loader.replace(matchCssLoaders, `$1$2?modules&localIdentName=${cssModulesNames}$3`)
})
config.module.loaders.push(newloader);
cssloader.test = new RegExp(`[^module]${cssloader.test.source}`)
cssloader.loader = 'style!css!postcss'
cssloader.include = [src, examples];
config.module.loaders.push({
test: /\.css$/,
include: [modules],
loader: 'style!css'
});
config.plugins = [
new webpack.DefinePlugin(defines)
].concat(config.plugins);
config.postcss = [].concat([
require('precss')({}),
require('autoprefixer')({}),
require('cssnano')({})
])
module.exports = config;