Skip to content

Commit fbad3a9

Browse files
authored
Webpack 4 (#267)
* upgrade url-join to 4.0.0, account for changes * webpack 4 support
1 parent 0d57c32 commit fbad3a9

20 files changed

+7131
-4025
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
.nyc_output
12
node_modules
23
coverage
4+
*.lcov

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@ script:
1010
npm run ci
1111

1212
after_success:
13-
- cat ./coverage/coverage.json | node_modules/codecov.io/bin/codecov.io.js
14-
- rm -rf ./coverage
13+
npm run cover

codecov.yml

Lines changed: 0 additions & 17 deletions
This file was deleted.

lib/DevMiddlewareError.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
module.exports = class DevMiddlewareError extends Error {
4+
5+
};

lib/context.js

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ module.exports = function ctx(compiler, options) {
6060
}
6161
}
6262

63-
function invalid(...args) {
63+
function invalid(callback) {
6464
if (context.state) {
6565
context.options.reporter(context.options, {
6666
log,
@@ -70,9 +70,7 @@ module.exports = function ctx(compiler, options) {
7070

7171
// We are now in invalid state
7272
context.state = false;
73-
// resolve async
74-
if (args.length === 2 && typeof args[1] === 'function') {
75-
const [, callback] = args;
73+
if (callback && typeof callback === 'function') {
7674
callback();
7775
}
7876
}
@@ -94,26 +92,11 @@ module.exports = function ctx(compiler, options) {
9492
}
9593

9694
context.rebuild = rebuild;
97-
context.compiler.plugin('invalid', invalid);
98-
context.compiler.plugin('run', invalid);
99-
100-
context.compiler.plugin('done', (stats) => {
101-
// clean up the time offset
102-
if (options.watchOffset > 0) {
103-
stats.startTime -= options.watchOffset;
104-
}
105-
106-
done(stats);
107-
});
108-
109-
context.compiler.plugin('watch-run', (watcher, callback) => {
110-
// apply a fix for compiler.watch, if watchOffset is greater than 0:
111-
// ff0000-ad-tech/wp-plugin-watch-offset
112-
// offset start-time
113-
if (options.watchOffset > 0) {
114-
watcher.startTime += options.watchOffset;
115-
}
116-
invalid(watcher, callback);
95+
context.compiler.hooks.invalid.tap('WebpackDevMiddleware', invalid);
96+
context.compiler.hooks.run.tap('WebpackDevMiddleware', invalid);
97+
context.compiler.hooks.done.tap('WebpackDevMiddleware', done);
98+
context.compiler.hooks.watchRun.tap('WebpackDevMiddleware', (comp, callback) => {
99+
invalid(callback);
117100
});
118101

119102
return context;

lib/middleware.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const mime = require('mime');
44
const urlJoin = require('url-join');
5+
const DevMiddlewareError = require('./DevMiddlewareError');
56
const { getFilenameFromUrl, handleRangeHeaders, handleRequest, ready } = require('./util');
67

78
module.exports = function wrapper(context) {
@@ -34,26 +35,24 @@ module.exports = function wrapper(context) {
3435
function processRequest() {
3536
try {
3637
let stat = context.fs.statSync(filename);
38+
3739
if (!stat.isFile()) {
3840
if (stat.isDirectory()) {
3941
let { index } = context.options;
4042

4143
if (index === undefined || index === true) {
4244
index = 'index.html';
4345
} else if (!index) {
44-
// TODO throw a proper error
45-
throw new Error('next');
46+
throw new DevMiddlewareError('next');
4647
}
4748

4849
filename = urlJoin(filename, index);
4950
stat = context.fs.statSync(filename);
5051
if (!stat.isFile()) {
51-
// TODO throw a proper error
52-
throw new Error('next');
52+
throw new DevMiddlewareError('next');
5353
}
5454
} else {
55-
// TODO throw a proper error
56-
throw new Error('next');
55+
throw new DevMiddlewareError('next');
5756
}
5857
}
5958
} catch (e) {
@@ -63,7 +62,9 @@ module.exports = function wrapper(context) {
6362
// server content
6463
let content = context.fs.readFileSync(filename);
6564
content = handleRangeHeaders(content, req, res);
65+
6666
let contentType = mime.getType(filename);
67+
6768
// do not add charset to WebAssembly files, otherwise compileStreaming will fail in the client
6869
if (!/\.wasm$/.test(filename)) {
6970
contentType += '; charset=UTF-8';

lib/util.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const MemoryFileSystem = require('memory-fs');
66
const pathabs = require('path-is-absolute');
77
const parseRange = require('range-parser');
88
const urlJoin = require('url-join');
9+
const DevMiddlewareError = require('./DevMiddlewareError');
910

1011
const HASH_REGEXP = /[0-9a-f]{10,}/;
1112

@@ -90,7 +91,11 @@ module.exports = {
9091
let uri = outputPath;
9192

9293
if (filename) {
93-
uri = urlJoin((outputPath || '').replace(/\/$/, ''), filename);
94+
uri = urlJoin((outputPath || ''), filename);
95+
96+
if (!uri.startsWith('/')) {
97+
uri = `/${uri}`;
98+
}
9499
}
95100

96101
// if no matches, use outputPath as filename
@@ -156,7 +161,7 @@ module.exports = {
156161

157162
setFs(context, compiler) {
158163
if (typeof compiler.outputPath === 'string' && !pathabs.posix(compiler.outputPath) && !pathabs.win32(compiler.outputPath)) {
159-
throw new Error('`output.path` needs to be an absolute path or `/`.');
164+
throw new DevMiddlewareError('`output.path` needs to be an absolute path or `/`.');
160165
}
161166

162167
let fs;

0 commit comments

Comments
 (0)