Skip to content

Commit

Permalink
Webpack 4 (#267)
Browse files Browse the repository at this point in the history
* upgrade url-join to 4.0.0, account for changes

* webpack 4 support
  • Loading branch information
shellscape authored Feb 28, 2018
1 parent 0d57c32 commit fbad3a9
Show file tree
Hide file tree
Showing 20 changed files with 7,131 additions and 4,025 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.nyc_output
node_modules
coverage
*.lcov
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ script:
npm run ci

after_success:
- cat ./coverage/coverage.json | node_modules/codecov.io/bin/codecov.io.js
- rm -rf ./coverage
npm run cover
17 changes: 0 additions & 17 deletions codecov.yml

This file was deleted.

5 changes: 5 additions & 0 deletions lib/DevMiddlewareError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = class DevMiddlewareError extends Error {

};
31 changes: 7 additions & 24 deletions lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ module.exports = function ctx(compiler, options) {
}
}

function invalid(...args) {
function invalid(callback) {
if (context.state) {
context.options.reporter(context.options, {
log,
Expand All @@ -70,9 +70,7 @@ module.exports = function ctx(compiler, options) {

// We are now in invalid state
context.state = false;
// resolve async
if (args.length === 2 && typeof args[1] === 'function') {
const [, callback] = args;
if (callback && typeof callback === 'function') {
callback();
}
}
Expand All @@ -94,26 +92,11 @@ module.exports = function ctx(compiler, options) {
}

context.rebuild = rebuild;
context.compiler.plugin('invalid', invalid);
context.compiler.plugin('run', invalid);

context.compiler.plugin('done', (stats) => {
// clean up the time offset
if (options.watchOffset > 0) {
stats.startTime -= options.watchOffset;
}

done(stats);
});

context.compiler.plugin('watch-run', (watcher, callback) => {
// apply a fix for compiler.watch, if watchOffset is greater than 0:
// ff0000-ad-tech/wp-plugin-watch-offset
// offset start-time
if (options.watchOffset > 0) {
watcher.startTime += options.watchOffset;
}
invalid(watcher, callback);
context.compiler.hooks.invalid.tap('WebpackDevMiddleware', invalid);
context.compiler.hooks.run.tap('WebpackDevMiddleware', invalid);
context.compiler.hooks.done.tap('WebpackDevMiddleware', done);
context.compiler.hooks.watchRun.tap('WebpackDevMiddleware', (comp, callback) => {
invalid(callback);
});

return context;
Expand Down
13 changes: 7 additions & 6 deletions lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const mime = require('mime');
const urlJoin = require('url-join');
const DevMiddlewareError = require('./DevMiddlewareError');
const { getFilenameFromUrl, handleRangeHeaders, handleRequest, ready } = require('./util');

module.exports = function wrapper(context) {
Expand Down Expand Up @@ -34,26 +35,24 @@ module.exports = function wrapper(context) {
function processRequest() {
try {
let stat = context.fs.statSync(filename);

if (!stat.isFile()) {
if (stat.isDirectory()) {
let { index } = context.options;

if (index === undefined || index === true) {
index = 'index.html';
} else if (!index) {
// TODO throw a proper error
throw new Error('next');
throw new DevMiddlewareError('next');
}

filename = urlJoin(filename, index);
stat = context.fs.statSync(filename);
if (!stat.isFile()) {
// TODO throw a proper error
throw new Error('next');
throw new DevMiddlewareError('next');
}
} else {
// TODO throw a proper error
throw new Error('next');
throw new DevMiddlewareError('next');
}
}
} catch (e) {
Expand All @@ -63,7 +62,9 @@ module.exports = function wrapper(context) {
// server content
let content = context.fs.readFileSync(filename);
content = handleRangeHeaders(content, req, res);

let contentType = mime.getType(filename);

// do not add charset to WebAssembly files, otherwise compileStreaming will fail in the client
if (!/\.wasm$/.test(filename)) {
contentType += '; charset=UTF-8';
Expand Down
9 changes: 7 additions & 2 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const MemoryFileSystem = require('memory-fs');
const pathabs = require('path-is-absolute');
const parseRange = require('range-parser');
const urlJoin = require('url-join');
const DevMiddlewareError = require('./DevMiddlewareError');

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

Expand Down Expand Up @@ -90,7 +91,11 @@ module.exports = {
let uri = outputPath;

if (filename) {
uri = urlJoin((outputPath || '').replace(/\/$/, ''), filename);
uri = urlJoin((outputPath || ''), filename);

if (!uri.startsWith('/')) {
uri = `/${uri}`;
}
}

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

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

let fs;
Expand Down
Loading

0 comments on commit fbad3a9

Please sign in to comment.