mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
Webpack 2 (#449)
* upgrade webpack * fix WatchRemoveEventPlugin for webpack2 * use webpack2 for building for files * bump webpack * bump webpack * monkeypatch watchpack
This commit is contained in:
parent
917a96a1cd
commit
ff117b7ed6
24
gulpfile.js
24
gulpfile.js
|
@ -5,7 +5,8 @@ const cache = require('gulp-cached')
|
||||||
const notify_ = require('gulp-notify')
|
const notify_ = require('gulp-notify')
|
||||||
const benchmark = require('gulp-benchmark')
|
const benchmark = require('gulp-benchmark')
|
||||||
const sequence = require('run-sequence')
|
const sequence = require('run-sequence')
|
||||||
const webpack = require('webpack-stream')
|
const webpack = require('webpack')
|
||||||
|
const webpackStream = require('webpack-stream')
|
||||||
const del = require('del')
|
const del = require('del')
|
||||||
const jest = require('gulp-jest')
|
const jest = require('gulp-jest')
|
||||||
|
|
||||||
|
@ -88,29 +89,28 @@ gulp.task('build', [
|
||||||
gulp.task('build-prefetcher', ['compile-lib', 'compile-client'], () => {
|
gulp.task('build-prefetcher', ['compile-lib', 'compile-client'], () => {
|
||||||
return gulp
|
return gulp
|
||||||
.src('client/next-prefetcher.js')
|
.src('client/next-prefetcher.js')
|
||||||
.pipe(webpack({
|
.pipe(webpackStream({
|
||||||
quiet: true,
|
|
||||||
output: { filename: 'next-prefetcher-bundle.js' },
|
output: { filename: 'next-prefetcher-bundle.js' },
|
||||||
plugins: [
|
plugins: [
|
||||||
new webpack.webpack.DefinePlugin({
|
new webpack.DefinePlugin({
|
||||||
'process.env': {
|
'process.env': {
|
||||||
NODE_ENV: JSON.stringify('production')
|
NODE_ENV: JSON.stringify('production')
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
],
|
],
|
||||||
module: {
|
module: {
|
||||||
loaders: [
|
rules: [
|
||||||
{
|
{
|
||||||
test: /\.js$/,
|
test: /\.js$/,
|
||||||
exclude: /node_modules/,
|
exclude: /node_modules/,
|
||||||
loader: 'babel',
|
loader: 'babel-loader',
|
||||||
query: {
|
options: {
|
||||||
'babelrc': false,
|
babelrc: false,
|
||||||
'presets': [
|
presets: [
|
||||||
['env', {
|
['env', {
|
||||||
'targets': {
|
targets: {
|
||||||
// All browsers which supports service workers
|
// All browsers which supports service workers
|
||||||
'browsers': ['chrome 49', 'firefox 49', 'opera 41']
|
browsers: ['chrome 49', 'firefox 49', 'opera 41']
|
||||||
}
|
}
|
||||||
}]
|
}]
|
||||||
]
|
]
|
||||||
|
@ -118,7 +118,7 @@ gulp.task('build-prefetcher', ['compile-lib', 'compile-client'], () => {
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}))
|
}, webpack))
|
||||||
.pipe(gulp.dest('dist/client'))
|
.pipe(gulp.dest('dist/client'))
|
||||||
.pipe(notify('Built release prefetcher'))
|
.pipe(notify('Built release prefetcher'))
|
||||||
})
|
})
|
||||||
|
|
|
@ -71,7 +71,7 @@
|
||||||
"strip-ansi": "3.0.1",
|
"strip-ansi": "3.0.1",
|
||||||
"styled-jsx": "0.3.0",
|
"styled-jsx": "0.3.0",
|
||||||
"url": "0.11.0",
|
"url": "0.11.0",
|
||||||
"webpack": "1.14.0",
|
"webpack": "2.2.0-rc.3",
|
||||||
"webpack-dev-middleware": "1.9.0",
|
"webpack-dev-middleware": "1.9.0",
|
||||||
"webpack-hot-middleware": "2.14.0",
|
"webpack-hot-middleware": "2.14.0",
|
||||||
"write-file-webpack-plugin": "3.4.2"
|
"write-file-webpack-plugin": "3.4.2"
|
||||||
|
|
|
@ -10,26 +10,24 @@ export default class WatchRemoveEventPlugin {
|
||||||
apply (compiler) {
|
apply (compiler) {
|
||||||
compiler.removedFiles = []
|
compiler.removedFiles = []
|
||||||
|
|
||||||
compiler.plugin('environment', () => {
|
if (!compiler.watchFileSystem) return
|
||||||
if (!compiler.watchFileSystem) return
|
|
||||||
|
|
||||||
const { watchFileSystem } = compiler
|
const { watchFileSystem } = compiler
|
||||||
const { watch } = watchFileSystem
|
const { watch } = watchFileSystem
|
||||||
|
|
||||||
watchFileSystem.watch = (files, dirs, missing, startTime, options, callback, callbackUndelayed) => {
|
watchFileSystem.watch = (files, dirs, missing, startTime, options, callback, callbackUndelayed) => {
|
||||||
const result = watch.call(watchFileSystem, files, dirs, missing, startTime, options, (...args) => {
|
const result = watch.call(watchFileSystem, files, dirs, missing, startTime, options, (...args) => {
|
||||||
compiler.removedFiles = this.removedFiles
|
compiler.removedFiles = this.removedFiles
|
||||||
this.removedFiles = []
|
this.removedFiles = []
|
||||||
callback(...args)
|
callback(...args)
|
||||||
}, callbackUndelayed)
|
}, callbackUndelayed)
|
||||||
|
|
||||||
const watchpack = watchFileSystem.watcher
|
const watchpack = watchFileSystem.watcher
|
||||||
watchpack.fileWatchers.forEach((w) => {
|
watchpack.fileWatchers.forEach((w) => {
|
||||||
w.on('remove', this.onRemove.bind(this, watchpack, w.path))
|
w.on('remove', this.onRemove.bind(this, watchpack, w.path))
|
||||||
})
|
})
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onRemove (watchpack, file) {
|
onRemove (watchpack, file) {
|
||||||
|
@ -38,3 +36,64 @@ export default class WatchRemoveEventPlugin {
|
||||||
watchpack._onChange(file)
|
watchpack._onChange(file)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// monkeypatching watchpack module to fix
|
||||||
|
// https://github.com/webpack/watchpack/pull/33
|
||||||
|
|
||||||
|
let DirectoryWatcher
|
||||||
|
try {
|
||||||
|
DirectoryWatcher = require('webpack/node_modules/watchpack/lib/DirectoryWatcher')
|
||||||
|
} catch (err) {
|
||||||
|
DirectoryWatcher = require('watchpack/lib/DirectoryWatcher')
|
||||||
|
}
|
||||||
|
|
||||||
|
/* eslint-disable */
|
||||||
|
var FS_ACCURENCY = 10000;
|
||||||
|
|
||||||
|
function withoutCase(str) {
|
||||||
|
return str.toLowerCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
DirectoryWatcher.prototype.setFileTime = function setFileTime(filePath, mtime, initial, type) {
|
||||||
|
var now = Date.now();
|
||||||
|
var old = this.files[filePath];
|
||||||
|
|
||||||
|
this.files[filePath] = [initial ? Math.min(now, mtime) : now, mtime];
|
||||||
|
|
||||||
|
// we add the fs accurency to reach the maximum possible mtime
|
||||||
|
if(mtime)
|
||||||
|
mtime = mtime + FS_ACCURENCY;
|
||||||
|
|
||||||
|
if(!old) {
|
||||||
|
if(mtime) {
|
||||||
|
if(this.watchers[withoutCase(filePath)]) {
|
||||||
|
this.watchers[withoutCase(filePath)].forEach(function(w) {
|
||||||
|
if(!initial || w.checkStartTime(mtime, initial)) {
|
||||||
|
w.emit("change", mtime);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if(!initial && mtime && type !== "add") {
|
||||||
|
if(this.watchers[withoutCase(filePath)]) {
|
||||||
|
this.watchers[withoutCase(filePath)].forEach(function(w) {
|
||||||
|
w.emit("change", mtime);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else if(!initial && !mtime) {
|
||||||
|
delete this.files[filePath];
|
||||||
|
if(this.watchers[withoutCase(filePath)]) {
|
||||||
|
this.watchers[withoutCase(filePath)].forEach(function(w) {
|
||||||
|
w.emit("remove");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(this.watchers[withoutCase(this.path)]) {
|
||||||
|
this.watchers[withoutCase(this.path)].forEach(function(w) {
|
||||||
|
if(!initial || w.checkStartTime(mtime, initial)) {
|
||||||
|
w.emit("change", filePath, mtime);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
/* eslint-enable */
|
||||||
|
|
|
@ -51,6 +51,14 @@ export default async function createCompiler (dir, { dev = false, quiet = false
|
||||||
const minChunks = pages.filter((p) => p !== documentPage).length
|
const minChunks = pages.filter((p) => p !== documentPage).length
|
||||||
|
|
||||||
const plugins = [
|
const plugins = [
|
||||||
|
new webpack.LoaderOptionsPlugin({
|
||||||
|
options: {
|
||||||
|
context: dir,
|
||||||
|
customInterpolateName (url, name, opts) {
|
||||||
|
return interpolateNames.get(this.resourcePath) || url
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}),
|
||||||
new WriteFilePlugin({
|
new WriteFilePlugin({
|
||||||
exitOnErrors: false,
|
exitOnErrors: false,
|
||||||
log: false,
|
log: false,
|
||||||
|
@ -66,7 +74,6 @@ export default async function createCompiler (dir, { dev = false, quiet = false
|
||||||
|
|
||||||
if (dev) {
|
if (dev) {
|
||||||
plugins.push(
|
plugins.push(
|
||||||
new webpack.optimize.OccurrenceOrderPlugin(),
|
|
||||||
new webpack.HotModuleReplacementPlugin(),
|
new webpack.HotModuleReplacementPlugin(),
|
||||||
new webpack.NoErrorsPlugin(),
|
new webpack.NoErrorsPlugin(),
|
||||||
new DetachPlugin(),
|
new DetachPlugin(),
|
||||||
|
@ -104,7 +111,7 @@ export default async function createCompiler (dir, { dev = false, quiet = false
|
||||||
mainBabelOptions.presets.push(require.resolve('./babel/preset'))
|
mainBabelOptions.presets.push(require.resolve('./babel/preset'))
|
||||||
}
|
}
|
||||||
|
|
||||||
const loaders = (dev ? [{
|
const rules = (dev ? [{
|
||||||
test: /\.js(\?[^?]*)?$/,
|
test: /\.js(\?[^?]*)?$/,
|
||||||
loader: 'hot-self-accept-loader',
|
loader: 'hot-self-accept-loader',
|
||||||
include: [
|
include: [
|
||||||
|
@ -126,13 +133,13 @@ export default async function createCompiler (dir, { dev = false, quiet = false
|
||||||
exclude (str) {
|
exclude (str) {
|
||||||
return /node_modules/.test(str) && str.indexOf(nextPagesDir) !== 0
|
return /node_modules/.test(str) && str.indexOf(nextPagesDir) !== 0
|
||||||
},
|
},
|
||||||
query: {
|
options: {
|
||||||
name: 'dist/[path][name].[ext]'
|
name: 'dist/[path][name].[ext]'
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
loader: 'babel',
|
loader: 'babel-loader',
|
||||||
include: nextPagesDir,
|
include: nextPagesDir,
|
||||||
query: {
|
options: {
|
||||||
babelrc: false,
|
babelrc: false,
|
||||||
cacheDirectory: true,
|
cacheDirectory: true,
|
||||||
sourceMaps: dev ? 'both' : false,
|
sourceMaps: dev ? 'both' : false,
|
||||||
|
@ -150,7 +157,7 @@ export default async function createCompiler (dir, { dev = false, quiet = false
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
test: /\.js(\?[^?]*)?$/,
|
test: /\.js(\?[^?]*)?$/,
|
||||||
loader: 'babel',
|
loader: 'babel-loader',
|
||||||
include: [dir, nextPagesDir],
|
include: [dir, nextPagesDir],
|
||||||
exclude (str) {
|
exclude (str) {
|
||||||
return /node_modules/.test(str) && str.indexOf(nextPagesDir) !== 0
|
return /node_modules/.test(str) && str.indexOf(nextPagesDir) !== 0
|
||||||
|
@ -165,7 +172,7 @@ export default async function createCompiler (dir, { dev = false, quiet = false
|
||||||
path: join(dir, '.next'),
|
path: join(dir, '.next'),
|
||||||
filename: '[name]',
|
filename: '[name]',
|
||||||
libraryTarget: 'commonjs2',
|
libraryTarget: 'commonjs2',
|
||||||
publicPath: dev ? '/_webpack/' : null,
|
publicPath: '/_webpack/',
|
||||||
devtoolModuleFilenameTemplate ({ resourcePath }) {
|
devtoolModuleFilenameTemplate ({ resourcePath }) {
|
||||||
const hash = createHash('sha1')
|
const hash = createHash('sha1')
|
||||||
hash.update(Date.now() + '')
|
hash.update(Date.now() + '')
|
||||||
|
@ -176,28 +183,27 @@ export default async function createCompiler (dir, { dev = false, quiet = false
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
resolve: {
|
resolve: {
|
||||||
root: [
|
modules: [
|
||||||
nodeModulesDir,
|
nodeModulesDir,
|
||||||
join(dir, 'node_modules')
|
join(dir, 'node_modules')
|
||||||
].concat(
|
].concat(
|
||||||
(process.env.NODE_PATH || '')
|
(process.env.NODE_PATH || '')
|
||||||
.split(process.platform === 'win32' ? ';' : ':')
|
.split(process.platform === 'win32' ? ';' : ':')
|
||||||
|
.filter((p) => !!p)
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
resolveLoader: {
|
resolveLoader: {
|
||||||
root: [
|
modules: [
|
||||||
nodeModulesDir,
|
nodeModulesDir,
|
||||||
join(__dirname, 'loaders')
|
join(__dirname, 'loaders')
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
plugins,
|
plugins,
|
||||||
module: {
|
module: {
|
||||||
loaders
|
rules
|
||||||
},
|
},
|
||||||
devtool: dev ? 'inline-source-map' : false,
|
devtool: dev ? 'inline-source-map' : false,
|
||||||
customInterpolateName: function (url, name, opts) {
|
performance: { hints: false }
|
||||||
return interpolateNames.get(this.resourcePath) || url
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config.webpack) {
|
if (config.webpack) {
|
||||||
|
|
Loading…
Reference in a new issue