-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.static.config.js
More file actions
144 lines (130 loc) · 3.21 KB
/
webpack.static.config.js
File metadata and controls
144 lines (130 loc) · 3.21 KB
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
const fs = require('fs-extra')
const mkdirp = require('mkdirp')
const path = require('path')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
function gather(routes, parentPath) {
var result = []
routes = Array.isArray(routes) ? routes : [routes]
// strips regexp chars out of the route, eg a trailing ?
function fix(routePath) {
return routePath.replace(/\?$/, '')
}
routes.forEach(function(route) {
const props = route.props
var routePath = props.path
if (routePath) {
routePath = parentPath ? path.join(parentPath, routePath) : routePath
result.push(fix(routePath))
}
if (props.children) {
result = result.concat(gather(props.children, routePath))
}
})
return result
}
function writeHtmlPage(fileName, html) {
// Replace trailing / with index.html
if (fileName.match(/\/$/)) {
fileName += 'index.html'
} else {
fileName += '/index.html'
}
const filePath = path.join('./build', fileName)
// make all of the directories in filePath, like mkdir -p
mkdirp.sync(path.dirname(filePath))
fs.writeFileSync(filePath, html)
}
function injectHtmlPlugin() {
this.plugin('emit', function(compilation, cb) {
global.window = {} // mock window
const app = require('./build/prerender')
const webpackStatsJson = compilation.getStats().toJson()
const scriptHash = webpackStatsJson.hash
console.log(scriptHash)
const paths = gather(app.routes)
console.log(paths)
var pageCount = paths.length
paths.forEach(function(p) {
app.routeToString(p, scriptHash, function(html) {
pageCount--
writeHtmlPage(p, html)
if (pageCount === 0) {
fs.unlinkSync('./build/prerender.js')
cb()
}
})
})
})
}
function copyAssets() {
this.plugin('emit', function(compilation, cb) {
fs.copy('./app/assets', './build', cb)
})
}
const prerenderConfig = {
entry: './app/static.js',
target: 'node',
output: {
path: './build',
filename: 'prerender.js',
publicPath: '/',
libraryTarget: 'commonjs2'
},
module: {
loaders: [{
test: /\.scss/,
loader: ExtractTextPlugin.extract('css!autoprefixer!sass')
}, {
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel'
}, {
test: /\.(svg|jpg|png)$/,
loader: 'file!image-webpack'
}, {
test: /\.(ttf|eot|woff|woff2)$/,
loader: 'null'
}]
},
plugins: [
new ExtractTextPlugin('style.css', {
allChunks: true
}),
],
resolve: {
extensions: ['', '.js']
}
}
const renderConfig = {
entry: './app/client.js',
output: {
path: './build',
filename: 'bundle.[hash].js',
publicPath: '/'
},
module: {
loaders: [{
test: /\.scss/,
loader: 'style!css!autoprefixer!sass'
}, {
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel'
}, {
test: /\.(svg|jpg|png)$/,
loader: 'file!image-webpack'
}, {
test: /\.(ttf|eot|woff|woff2)$/,
loader: 'url?limit=100000'
}]
},
plugins: [injectHtmlPlugin, copyAssets],
resolve: {
extensions: ['', '.js']
}
}
// array config syntax: runs webpack multiple times
module.exports = [
prerenderConfig,
renderConfig
]