Skip to content

Commit ff117b7

Browse files
nkzawarauchg
authored andcommitted
Webpack 2 (#449)
* upgrade webpack * fix WatchRemoveEventPlugin for webpack2 * use webpack2 for building for files * bump webpack * bump webpack * monkeypatch watchpack
1 parent 917a96a commit ff117b7

File tree

4 files changed

+111
-46
lines changed

4 files changed

+111
-46
lines changed

gulpfile.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ const cache = require('gulp-cached')
55
const notify_ = require('gulp-notify')
66
const benchmark = require('gulp-benchmark')
77
const sequence = require('run-sequence')
8-
const webpack = require('webpack-stream')
8+
const webpack = require('webpack')
9+
const webpackStream = require('webpack-stream')
910
const del = require('del')
1011
const jest = require('gulp-jest')
1112

@@ -88,37 +89,36 @@ gulp.task('build', [
8889
gulp.task('build-prefetcher', ['compile-lib', 'compile-client'], () => {
8990
return gulp
9091
.src('client/next-prefetcher.js')
91-
.pipe(webpack({
92-
quiet: true,
92+
.pipe(webpackStream({
9393
output: { filename: 'next-prefetcher-bundle.js' },
9494
plugins: [
95-
new webpack.webpack.DefinePlugin({
95+
new webpack.DefinePlugin({
9696
'process.env': {
9797
NODE_ENV: JSON.stringify('production')
9898
}
9999
})
100100
],
101101
module: {
102-
loaders: [
102+
rules: [
103103
{
104104
test: /\.js$/,
105105
exclude: /node_modules/,
106-
loader: 'babel',
107-
query: {
108-
'babelrc': false,
109-
'presets': [
106+
loader: 'babel-loader',
107+
options: {
108+
babelrc: false,
109+
presets: [
110110
['env', {
111-
'targets': {
111+
targets: {
112112
// All browsers which supports service workers
113-
'browsers': ['chrome 49', 'firefox 49', 'opera 41']
113+
browsers: ['chrome 49', 'firefox 49', 'opera 41']
114114
}
115115
}]
116116
]
117117
}
118118
}
119119
]
120120
}
121-
}))
121+
}, webpack))
122122
.pipe(gulp.dest('dist/client'))
123123
.pipe(notify('Built release prefetcher'))
124124
})

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
"strip-ansi": "3.0.1",
7272
"styled-jsx": "0.3.0",
7373
"url": "0.11.0",
74-
"webpack": "1.14.0",
74+
"webpack": "2.2.0-rc.3",
7575
"webpack-dev-middleware": "1.9.0",
7676
"webpack-hot-middleware": "2.14.0",
7777
"write-file-webpack-plugin": "3.4.2"

server/build/plugins/watch-remove-event-plugin.js

Lines changed: 79 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,24 @@ export default class WatchRemoveEventPlugin {
1010
apply (compiler) {
1111
compiler.removedFiles = []
1212

13-
compiler.plugin('environment', () => {
14-
if (!compiler.watchFileSystem) return
15-
16-
const { watchFileSystem } = compiler
17-
const { watch } = watchFileSystem
18-
19-
watchFileSystem.watch = (files, dirs, missing, startTime, options, callback, callbackUndelayed) => {
20-
const result = watch.call(watchFileSystem, files, dirs, missing, startTime, options, (...args) => {
21-
compiler.removedFiles = this.removedFiles
22-
this.removedFiles = []
23-
callback(...args)
24-
}, callbackUndelayed)
25-
26-
const watchpack = watchFileSystem.watcher
27-
watchpack.fileWatchers.forEach((w) => {
28-
w.on('remove', this.onRemove.bind(this, watchpack, w.path))
29-
})
30-
return result
31-
}
32-
})
13+
if (!compiler.watchFileSystem) return
14+
15+
const { watchFileSystem } = compiler
16+
const { watch } = watchFileSystem
17+
18+
watchFileSystem.watch = (files, dirs, missing, startTime, options, callback, callbackUndelayed) => {
19+
const result = watch.call(watchFileSystem, files, dirs, missing, startTime, options, (...args) => {
20+
compiler.removedFiles = this.removedFiles
21+
this.removedFiles = []
22+
callback(...args)
23+
}, callbackUndelayed)
24+
25+
const watchpack = watchFileSystem.watcher
26+
watchpack.fileWatchers.forEach((w) => {
27+
w.on('remove', this.onRemove.bind(this, watchpack, w.path))
28+
})
29+
return result
30+
}
3331
}
3432

3533
onRemove (watchpack, file) {
@@ -38,3 +36,64 @@ export default class WatchRemoveEventPlugin {
3836
watchpack._onChange(file)
3937
}
4038
}
39+
40+
// monkeypatching watchpack module to fix
41+
// https://github.com/webpack/watchpack/pull/33
42+
43+
let DirectoryWatcher
44+
try {
45+
DirectoryWatcher = require('webpack/node_modules/watchpack/lib/DirectoryWatcher')
46+
} catch (err) {
47+
DirectoryWatcher = require('watchpack/lib/DirectoryWatcher')
48+
}
49+
50+
/* eslint-disable */
51+
var FS_ACCURENCY = 10000;
52+
53+
function withoutCase(str) {
54+
return str.toLowerCase();
55+
}
56+
57+
DirectoryWatcher.prototype.setFileTime = function setFileTime(filePath, mtime, initial, type) {
58+
var now = Date.now();
59+
var old = this.files[filePath];
60+
61+
this.files[filePath] = [initial ? Math.min(now, mtime) : now, mtime];
62+
63+
// we add the fs accurency to reach the maximum possible mtime
64+
if(mtime)
65+
mtime = mtime + FS_ACCURENCY;
66+
67+
if(!old) {
68+
if(mtime) {
69+
if(this.watchers[withoutCase(filePath)]) {
70+
this.watchers[withoutCase(filePath)].forEach(function(w) {
71+
if(!initial || w.checkStartTime(mtime, initial)) {
72+
w.emit("change", mtime);
73+
}
74+
});
75+
}
76+
}
77+
} else if(!initial && mtime && type !== "add") {
78+
if(this.watchers[withoutCase(filePath)]) {
79+
this.watchers[withoutCase(filePath)].forEach(function(w) {
80+
w.emit("change", mtime);
81+
});
82+
}
83+
} else if(!initial && !mtime) {
84+
delete this.files[filePath];
85+
if(this.watchers[withoutCase(filePath)]) {
86+
this.watchers[withoutCase(filePath)].forEach(function(w) {
87+
w.emit("remove");
88+
});
89+
}
90+
}
91+
if(this.watchers[withoutCase(this.path)]) {
92+
this.watchers[withoutCase(this.path)].forEach(function(w) {
93+
if(!initial || w.checkStartTime(mtime, initial)) {
94+
w.emit("change", filePath, mtime);
95+
}
96+
});
97+
}
98+
};
99+
/* eslint-enable */

server/build/webpack.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ export default async function createCompiler (dir, { dev = false, quiet = false
5151
const minChunks = pages.filter((p) => p !== documentPage).length
5252

5353
const plugins = [
54+
new webpack.LoaderOptionsPlugin({
55+
options: {
56+
context: dir,
57+
customInterpolateName (url, name, opts) {
58+
return interpolateNames.get(this.resourcePath) || url
59+
}
60+
}
61+
}),
5462
new WriteFilePlugin({
5563
exitOnErrors: false,
5664
log: false,
@@ -66,7 +74,6 @@ export default async function createCompiler (dir, { dev = false, quiet = false
6674

6775
if (dev) {
6876
plugins.push(
69-
new webpack.optimize.OccurrenceOrderPlugin(),
7077
new webpack.HotModuleReplacementPlugin(),
7178
new webpack.NoErrorsPlugin(),
7279
new DetachPlugin(),
@@ -104,7 +111,7 @@ export default async function createCompiler (dir, { dev = false, quiet = false
104111
mainBabelOptions.presets.push(require.resolve('./babel/preset'))
105112
}
106113

107-
const loaders = (dev ? [{
114+
const rules = (dev ? [{
108115
test: /\.js(\?[^?]*)?$/,
109116
loader: 'hot-self-accept-loader',
110117
include: [
@@ -126,13 +133,13 @@ export default async function createCompiler (dir, { dev = false, quiet = false
126133
exclude (str) {
127134
return /node_modules/.test(str) && str.indexOf(nextPagesDir) !== 0
128135
},
129-
query: {
136+
options: {
130137
name: 'dist/[path][name].[ext]'
131138
}
132139
}, {
133-
loader: 'babel',
140+
loader: 'babel-loader',
134141
include: nextPagesDir,
135-
query: {
142+
options: {
136143
babelrc: false,
137144
cacheDirectory: true,
138145
sourceMaps: dev ? 'both' : false,
@@ -150,7 +157,7 @@ export default async function createCompiler (dir, { dev = false, quiet = false
150157
}
151158
}, {
152159
test: /\.js(\?[^?]*)?$/,
153-
loader: 'babel',
160+
loader: 'babel-loader',
154161
include: [dir, nextPagesDir],
155162
exclude (str) {
156163
return /node_modules/.test(str) && str.indexOf(nextPagesDir) !== 0
@@ -165,7 +172,7 @@ export default async function createCompiler (dir, { dev = false, quiet = false
165172
path: join(dir, '.next'),
166173
filename: '[name]',
167174
libraryTarget: 'commonjs2',
168-
publicPath: dev ? '/_webpack/' : null,
175+
publicPath: '/_webpack/',
169176
devtoolModuleFilenameTemplate ({ resourcePath }) {
170177
const hash = createHash('sha1')
171178
hash.update(Date.now() + '')
@@ -176,28 +183,27 @@ export default async function createCompiler (dir, { dev = false, quiet = false
176183
}
177184
},
178185
resolve: {
179-
root: [
186+
modules: [
180187
nodeModulesDir,
181188
join(dir, 'node_modules')
182189
].concat(
183190
(process.env.NODE_PATH || '')
184191
.split(process.platform === 'win32' ? ';' : ':')
192+
.filter((p) => !!p)
185193
)
186194
},
187195
resolveLoader: {
188-
root: [
196+
modules: [
189197
nodeModulesDir,
190198
join(__dirname, 'loaders')
191199
]
192200
},
193201
plugins,
194202
module: {
195-
loaders
203+
rules
196204
},
197205
devtool: dev ? 'inline-source-map' : false,
198-
customInterpolateName: function (url, name, opts) {
199-
return interpolateNames.get(this.resourcePath) || url
200-
}
206+
performance: { hints: false }
201207
}
202208

203209
if (config.webpack) {

0 commit comments

Comments
 (0)