Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…eware

* 'master' of https://github.com/webpack/webpack-dev-middleware:
  committed too quickly...
  Add not-functional test for server side render
  More tests
  fix typos
  Start with some tests!
  Fix inconsistencies with semi colons
  Use valid license for npm
  Document `reporter` option
  Add changelog for 1.7.0
  1.7.0
  Removing watching/watching.running check (webpack#100)
  Improve eslint linting and remove js-beautify
  Support for server-side rendering  (webpack#118)
  • Loading branch information
wuct committed Sep 12, 2016
2 parents c8b7ba7 + ca63555 commit 02b1e36
Show file tree
Hide file tree
Showing 12 changed files with 346 additions and 70 deletions.
63 changes: 44 additions & 19 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,25 +1,50 @@
{
"env": {
"node": true
"node": true,
"mocha": true
},
"rules": {
"strict": 0,
"camelcase": 0,
"curly": 0,
"indent": [2, "tab"],
"eol-last": 2,
"no-shadow": 0,
"no-redeclare": 2,
"no-extra-bind": 2,
"no-empty": 0,
"no-process-exit": 2,
"no-underscore-dangle": 0,
"no-use-before-define": 0,
"no-unused-vars": 2,
"consistent-return": 0,
"no-inner-declarations": 2,
"no-loop-func": 2,
"no-undef": 2,
"space-before-function-paren": [2, "never"]
"indent": [2, "tab", { "SwitchCase": 1 }],
"brace-style": ["error", "1tbs"],
"semi": "error",
"no-eval": "error",
"eol-last": "error",
"no-redeclare": "error",
"no-extra-bind": "error",
"no-process-exit": "error",
"no-inner-declarations": "warn",
"no-loop-func": "warn",
"no-undef": "error",
"no-trailing-spaces": "error",
"space-before-function-paren": ["error", "never"],
"no-multi-spaces": "error",
"space-in-parens": "error",
"space-before-blocks": "error",
"no-unused-vars": "error",
"no-dupe-keys": "error",
"valid-typeof": "error",
"space-infix-ops": "error",
"no-negated-in-lhs": "error",
"no-octal": "error",
"no-regex-spaces": "error",
"no-self-assign": "error",
"no-sparse-arrays": "error",
"no-unexpected-multiline": "error",
"no-unreachable": "error",
"no-extra-semi": "error",
"no-func-assign": "error",
"no-invalid-regexp": "error",
"keyword-spacing": ["error", {
"after": false,
"overrides": {
"try": {"after": true},
"else": {"after": true},
"throw": {"after": true},
"case": {"after": true},
"return": {"after": true},
"finally": {"after": true},
"do": {"after": true}
}
}]
}
}
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# 1.7.0 (2016-09-10)

- Add `reporter` option for custom logs ([#91](https://github.com/webpack/webpack-dev-middleware/pull/91)).
- Add `serverSideRender` option to help support server-side rendering ([#118](https://github.com/webpack/webpack-dev-middleware/pull/118)).
- Set `statusCode` to 200 for served files ([#94](https://github.com/webpack/webpack-dev-middleware/pull/94)).
- Fix `waitUntilValid` callback always being called immediately when using MultiCompiler ([#100](https://github.com/webpack/webpack-dev-middleware/pull/100)).
- Fix range support not working ([#116](https://github.com/webpack/webpack-dev-middleware/pull/116)).
- Only use middleware for GET requests ([#96](https://github.com/webpack/webpack-dev-middleware/pull/96)).
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ app.use(webpackMiddleware(webpack({
}
// options for formating the statistics

reporter: null,
// Provide a custom reporter to change the way how logs are shown.

serverSideRender: false,
// Turn off the server-side rendering mode. See Server-Side Rendering part for more info.
}));
Expand Down
21 changes: 21 additions & 0 deletions lib/GetFilenameFromUrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var pathJoin = require("./PathJoin");

function getFilenameFromUrl(publicPath, outputPath, url) {
// publicPrefix is the folder our bundle should be in
var localPrefix = publicPath || "/";
if(url.indexOf(localPrefix) !== 0) {
if(/^(https?:)?\/\//.test(localPrefix)) {
localPrefix = "/" + localPrefix.replace(/^(https?:)?\/\/[^\/]+\//, "");
// fast exit if another directory requested
if(url.indexOf(localPrefix) !== 0) return false;
} else return false;
}
// get filename from request
var filename = url.substr(localPrefix.length);
if(filename.indexOf("?") >= 0) {
filename = filename.substr(0, filename.indexOf("?"));
}
return filename ? pathJoin(outputPath, filename) : outputPath;
}

module.exports = getFilenameFromUrl;
5 changes: 5 additions & 0 deletions lib/PathJoin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function pathJoin(a, b) {
return a == "/" ? "/" + b : (a || "") + "/" + b;
}

module.exports = pathJoin;
53 changes: 16 additions & 37 deletions middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
var MemoryFileSystem = require("memory-fs");
var mime = require("mime");
var parseRange = require("range-parser");
var getFilenameFromUrl = require("./lib/GetFilenameFromUrl");
var pathJoin = require("./lib/PathJoin");

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

Expand All @@ -20,15 +22,15 @@ var defaultReporter = function(reporterOptions) {
options.noInfo)
displayStats = false;
if(displayStats) {
console.log(stats.toString(options.stats))
console.log(stats.toString(options.stats));
}
if(!options.noInfo && !options.quiet) {
console.info("webpack: bundle is now VALID.");
}
} else {
console.info("webpack: bundle is now INVALID.");
}
}
};

// constructor for the middleware
module.exports = function(compiler, options) {
Expand Down Expand Up @@ -58,7 +60,7 @@ module.exports = function(compiler, options) {
compiler.plugin("done", function(stats) {
// We are now on valid state
state = true;
webpackStats = stats
webpackStats = stats;

// Do the stuff in nextTick, because bundle may be invalidated
// if a change happened while compiling
Expand All @@ -75,7 +77,7 @@ module.exports = function(compiler, options) {
// execute callback that are delayed
var cbs = callbacks;
callbacks = [];
cbs.forEach(function continueBecauseBundleAvailible(cb) {
cbs.forEach(function continueBecauseBundleAvailable(cb) {
cb();
});
});
Expand All @@ -93,7 +95,7 @@ module.exports = function(compiler, options) {
options.reporter({
state: false,
options: options
})
});

// We are now in invalid state
state = false;
Expand Down Expand Up @@ -146,28 +148,6 @@ module.exports = function(compiler, options) {
}
}

function pathJoin(a, b) {
return a == "/" ? "/" + b : (a || "") + "/" + b
}

function getFilenameFromUrl(url) {
// publicPrefix is the folder our bundle should be in
var localPrefix = options.publicPath || "/";
if(url.indexOf(localPrefix) !== 0) {
if(/^(https?:)?\/\//.test(localPrefix)) {
localPrefix = "/" + localPrefix.replace(/^(https?:)?\/\/[^\/]+\//, "");
// fast exit if another directory requested
if(url.indexOf(localPrefix) !== 0) return false;
} else return false;
}
// get filename from request
var filename = url.substr(localPrefix.length);
if(filename.indexOf("?") >= 0) {
filename = filename.substr(0, filename.indexOf("?"));
}
return filename ? pathJoin(compiler.outputPath, filename) : compiler.outputPath;
}

function handleRangeHeaders(content, req, res) {
res.setHeader('Accept-Ranges', 'bytes');
if(req.headers.range) {
Expand Down Expand Up @@ -199,18 +179,18 @@ module.exports = function(compiler, options) {
// The middleware function
function webpackDevMiddleware(req, res, next) {
function goNext() {
if(!options.serverSideRender) return next()
if(!options.serverSideRender) return next();
ready(function() {
res.locals.webpackStats = webpackStats
next()
}, req)
res.locals.webpackStats = webpackStats;
next();
}, req);
}

if(req.method !== 'GET') {
return goNext();
}

var filename = getFilenameFromUrl(req.url);
var filename = getFilenameFromUrl(options.publicPath, compiler.outputPath, req.url);
if(filename === false) return goNext();

// in lazy mode, rebuild on bundle request
Expand All @@ -225,7 +205,7 @@ module.exports = function(compiler, options) {
}
} catch(e) {}
}
// delay the request until we have a vaild bundle
// delay the request until we have a valid bundle
ready(processRequest, req);

function processRequest() {
Expand Down Expand Up @@ -261,12 +241,11 @@ module.exports = function(compiler, options) {
}
}

webpackDevMiddleware.getFilenameFromUrl = getFilenameFromUrl;
webpackDevMiddleware.getFilenameFromUrl = getFilenameFromUrl.bind(this, options.publicPath, compiler.outputPath);

webpackDevMiddleware.waitUntilValid = function(callback) {
callback = callback || function() {};
if(!watching || !watching.running) callback();
else ready(callback, {});
ready(callback, {});
};

webpackDevMiddleware.invalidate = function(callback) {
Expand All @@ -288,4 +267,4 @@ module.exports = function(compiler, options) {
webpackDevMiddleware.fileSystem = fs;

return webpackDevMiddleware;
}
};
32 changes: 18 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webpack-dev-middleware",
"version": "1.6.1",
"version": "1.7.0",
"author": "Tobias Koppers @sokra",
"description": "Offers a dev middleware for webpack, which arguments a live bundle to a directory",
"peerDependencies": {
Expand All @@ -12,16 +12,15 @@
"range-parser": "^1.0.3"
},
"devDependencies": {
"eslint": "^2.10.1",
"beautify-lint": "^1.0.4",
"js-beautify": "^1.6.3"
"eslint": "^3.4.0",
"express": "^4.14.0",
"file-loader": "^0.9.0",
"mocha": "^3.0.2",
"should": "^11.1.0",
"supertest": "^2.0.0",
"webpack": "^2.1.0-beta.22"
},
"licenses": [
{
"type": "MIT",
"url": "http://www.opensource.org/licenses/mit-license.php"
}
],
"license": "MIT",
"engines": {
"node": ">=0.6"
},
Expand All @@ -31,10 +30,15 @@
"type": "git",
"url": "https://github.com/webpack/webpack-dev-middleware.git"
},
"files": [
"middleware.js",
"lib/"
],
"scripts": {
"lint": "eslint *.js",
"beautify-lint": "beautify-lint *.js",
"beautify": "beautify-rewrite *.js",
"travis": "npm run lint && npm run beautify-lint && node middleware.js"
"lint": "eslint *.js test",
"pretest": "npm run -s lint",
"test": "mocha --full-trace --check-leaks",
"beautify": "npm run lint -- --fix",
"travis": "npm test && node middleware.js"
}
}
61 changes: 61 additions & 0 deletions test/GetFilenameFromUrl.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
var should = require("should");
var getFilenameFromUrl = require("../lib/GetFilenameFromUrl");

function testUrl(options) {
var url = getFilenameFromUrl(options.publicPath, options.outputPath, options.url);
should.strictEqual(url, options.expected);
}

describe("GetFilenameFromUrl", function() {
it("should handle urls", function() {
var results = [
{
url: "/foo.js",
outputPath: "/",
publicPath: "/",
expected: "/foo.js"
}, {
url: "/0.19dc5d417382d73dd190.hot-update.js",
outputPath: "/",
publicPath: "http://localhost:8080/",
expected: "/0.19dc5d417382d73dd190.hot-update.js"
}, {
url: "/bar.js",
outputPath: "/",
publicPath: "https://localhost:8080/",
expected: "/bar.js"
}, {
url: "/test.html?foo=bar",
outputPath: "/",
publicPath: "/",
expected: "/test.html",
}, {
url: "/a.js",
outputPath: "/dist",
publicPath: "/",
expected: "/dist/a.js",
}, {
url: "/b.js",
outputPath: "/",
publicPath: undefined,
expected: "/b.js",
}, {
url: "/c.js",
outputPath: undefined,
publicPath: undefined,
expected: "/c.js",
}, {
url: "/more/complex/path.js",
outputPath: "/a",
publicPath: "/",
expected: "/a/more/complex/path.js",
}, {
url: "c.js",
outputPath: "/dist",
publicPath: "/",
expected: false, // publicPath is not in url, so it should fail
}
];
results.forEach(testUrl);
});
});
Loading

0 comments on commit 02b1e36

Please sign in to comment.