forked from openstf/stf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace webpack-dev-middleware with our own improved version.
- Loading branch information
Showing
3 changed files
with
115 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,112 @@ | ||
var webpackMiddleware = require('webpack-dev-middleware') | ||
var path = require('path') | ||
var url = require('url') | ||
|
||
var webpack = require('webpack') | ||
var mime = require('mime') | ||
var Promise = require('bluebird') | ||
var _ = require('lodash') | ||
var MemoryOutputFileSystem = require('webpack/lib/MemoryOutputFileSystem') | ||
var MemoryInputFileSystem = | ||
require('webpack/node_modules/enhanced-resolve/lib/MemoryInputFileSystem') | ||
|
||
var webpackOptions = require('../../webpack.config.js') | ||
var logger = require('../util/logger') | ||
var lifecycle = require('../util/lifecycle') | ||
var globalOptions = require('../../webpack.config') | ||
|
||
var overrideOptions = { | ||
debug: true, | ||
devtool: 'eval' | ||
} | ||
// Similar to webpack-dev-middleware, but integrates with our custom | ||
// lifecycle, behaves more like normal express middleware, and removes | ||
// all unnecessary features. | ||
module.exports = function(options) { | ||
var log = logger.createLogger('middleware:webpack') | ||
options = _.defaults(options || {}, globalOptions) | ||
|
||
var storage = Object.create(null) | ||
var fs = new MemoryInputFileSystem(storage) | ||
|
||
var compiler = webpack(options) | ||
compiler.outputFileSystem = new MemoryOutputFileSystem(storage) | ||
|
||
var valid = false | ||
var queue = [] | ||
|
||
var finalOptions = _.assign(webpackOptions, overrideOptions) | ||
log.info('Creating bundle') | ||
var watching = compiler.watch(options.watchDelay, function(err) { | ||
if (err) { | ||
log.fatal('Webpack had an error', err.stack) | ||
lifecycle.fatal() | ||
} | ||
}) | ||
|
||
module.exports = webpackMiddleware(webpack(finalOptions), { | ||
noInfo: false, | ||
quiet: false, | ||
lazy: false, | ||
publicPath: '/static/build/', | ||
stats: { | ||
colors: true | ||
lifecycle.observe(function() { | ||
watching.watcher.close() | ||
}) | ||
|
||
function doneListener(stats) { | ||
process.nextTick(function() { | ||
if (valid) { | ||
log.info(stats.toString(options.stats)) | ||
|
||
if (stats.hasErrors()) { | ||
log.error('Bundle has errors') | ||
} | ||
else if (stats.hasWarnings()) { | ||
log.warn('Bundle has warnings') | ||
} | ||
else { | ||
log.info('Bundle is now valid') | ||
} | ||
|
||
queue.forEach(function(resolver) { | ||
resolver.resolve() | ||
}) | ||
} | ||
}) | ||
|
||
valid = true | ||
} | ||
}) | ||
|
||
function invalidate() { | ||
if (valid) { | ||
log.info('Bundle is now invalid') | ||
valid = false | ||
} | ||
} | ||
|
||
compiler.plugin('done', doneListener) | ||
compiler.plugin('invalid', invalidate) | ||
compiler.plugin('compile', invalidate) | ||
|
||
function bundle() { | ||
if (valid) { | ||
return Promise.resolve() | ||
} | ||
else { | ||
log.info('Waiting for bundle to finish') | ||
var resolver = Promise.defer() | ||
queue.push(resolver) | ||
return resolver.promise | ||
} | ||
} | ||
|
||
return function(req, res, next) { | ||
var parsedUrl = url.parse(req.url) | ||
|
||
var target = path.join( | ||
compiler.outputPath | ||
, parsedUrl.pathname | ||
) | ||
|
||
bundle() | ||
.then(function() { | ||
try { | ||
var body = fs.readFileSync(target) | ||
res.set('Content-Type', mime.lookup(target)) | ||
res.end(body) | ||
} | ||
catch (err) { | ||
return next() | ||
} | ||
}) | ||
.catch(next) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters