Skip to content

Commit

Permalink
Merge pull request #37 from mzgoddard/latest-webpack-beta
Browse files Browse the repository at this point in the history
Support webpack 2's move to multiple parsers
  • Loading branch information
mzgoddard authored Sep 27, 2016
2 parents e798ecc + 268cdff commit 61f7838
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
33 changes: 33 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ var fsReadFile = Promise.promisify(fs.readFile, {context: fs});
var fsStat = Promise.promisify(fs.stat, {context: fs});
var fsWriteFile = Promise.promisify(fs.writeFile, {context: fs});

var NS, extractTextNS;

NS = fs.realpathSync(__dirname);

try {
extractTextNS = path.dirname(require.resolve('extract-text-webpack-plugin'));
}
catch (_) {}

function serializeDependencies(deps) {
return deps
.map(function(dep) {
Expand Down Expand Up @@ -449,6 +458,14 @@ HardSourceWebpackPlugin.prototype.apply = function(compiler) {
}
});

// Webpack 2 can use different parsers based on config rule sets.
params.normalModuleFactory.plugin('parser', function(parser, options) {
// Store the options somewhere that can not conflict with another plugin
// on the parser so we can look it up and store those options with a
// cached module resolution.
parser[NS + '/parser-options'] = options;
});

params.normalModuleFactory.plugin('resolver', function(fn) {
return function(request, cb) {
var cacheId = JSON.stringify([request.context, request.request]);
Expand All @@ -462,6 +479,7 @@ HardSourceWebpackPlugin.prototype.apply = function(compiler) {
if (!request.source) {
resolveCache[cacheId] = Object.assign({}, request, {
parser: null,
parserOptions: request.parser[NS + '/parser-options'],
dependencies: null,
});
}
Expand All @@ -473,6 +491,9 @@ HardSourceWebpackPlugin.prototype.apply = function(compiler) {
var result = Object.assign({}, resolveCache[cacheId]);
result.dependencies = request.dependencies;
result.parser = compilation.compiler.parser;
if (!result.parser || !result.parser.parse) {
result.parser = params.normalModuleFactory.getParser(result.parserOptions);
}
return cb(null, result);
};

Expand Down Expand Up @@ -520,6 +541,13 @@ HardSourceWebpackPlugin.prototype.apply = function(compiler) {
compiler.contextTimestamps
)) {
var module = new HardModule(cacheItem);

// Custom plugin handling for common plugins.
// This will be moved in a pluginified HardSourcePlugin.
if (cacheItem.extractTextPluginMeta) {
module[extractTextNS] = cacheItem.extractTextPluginMeta;
}

return cb(null, module);
}
}
Expand Down Expand Up @@ -640,6 +668,11 @@ HardSourceWebpackPlugin.prototype.apply = function(compiler) {
contextDependencies: module.contextDependencies,
};

// Custom plugin handling for common plugins.
// This will be moved in a pluginified HardSourcePlugin.
moduleCache[module.request].extractTextPluginMeta =
module[extractTextNS];

moduleOps.push({
key: module.request,
value: JSON.stringify(moduleCache[module.request]),
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/plugin-extract-text/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ module.exports = {
new ExtractTextPlugin('style.css'),
new HardSourceWebpackPlugin({
cacheDirectory: 'cache',
environmentPaths: {
root: __dirname + '/../../..',
},
}),
],
};

0 comments on commit 61f7838

Please sign in to comment.