Skip to content

Support webpack@4 and a few other things. #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
1 change: 1 addition & 0 deletions ExtractedModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ function ExtractedModule(identifier, originalModule, source, sourceMap, addtitio
this._sourceMap = sourceMap;
this._prevModules = prevModules;
this.addtitionalInformation = addtitionalInformation;
this.type = "css";
this.chunks = [];
}
module.exports = ExtractedModule;
Expand Down
98 changes: 55 additions & 43 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,14 @@ function ExtractTextPlugin(options) {
" allChunks: boolean\n" +
" disable: boolean\n");
}
if(isString(options)) {
if(typeof options === "string") {
options = { filename: options };
} else {
schemaTester(pluginSchema, options);
}
this.filename = options.filename || (process.env.NODE_ENV === 'development' ? '[name].css' : '[name].[contenthash].css');
this.filename = options.filename || (
!process.env.NODE_ENV || (process.env.NODE_ENV === 'development') ? '[name].css' : '[name].[contenthash].css'
);
this.id = options.id != null ? options.id : ++nextId;
this.options = {};
mergeOptions(this.options, options);
Expand All @@ -120,7 +122,7 @@ function ExtractTextPlugin(options) {
module.exports = ExtractTextPlugin;

function getLoaderObject(loader) {
if (isString(loader)) {
if (typeof loader === "string") {
return {loader: loader};
}
return loader;
Expand All @@ -134,18 +136,6 @@ function mergeOptions(a, b) {
return a;
}

function isString(a) {
return typeof a === "string";
}

function isFunction(a) {
return isType('Function', a);
}

function isType(type, obj) {
return Object.prototype.toString.call(obj) === '[object ' + type + ']';
}

ExtractTextPlugin.loader = function(options) {
return { loader: require.resolve("./loader"), options: options };
};
Expand Down Expand Up @@ -184,22 +174,26 @@ ExtractTextPlugin.prototype.extract = function(options) {
if(options.loader) {
console.warn('loader option has been deprecated - replace with "use"');
}
if(Array.isArray(options) || isString(options) || typeof options.options === "object" || typeof options.query === 'object') {
if(Array.isArray(options) || (typeof options === "string") || typeof options.options === "object" || typeof options.query === 'object') {
options = { loader: options };
} else {
schemaTester(loaderSchema, options);
}
var loader = options.use ||  options.loader;
var before = options.fallback || options.fallbackLoader || [];
if(isString(loader)) {
if(typeof loader === "string") {
loader = loader.split("!");
}
if(isString(before)) {
if(typeof before === "string") {
before = before.split("!");
} else if(!Array.isArray(before)) {
before = [before];
}
options = mergeOptions({omit: before.length, remove: true}, options);
options = mergeOptions({
omit: before.length,
remove: true,
hot: !process.env.NODE_ENV || (process.env.NODE_ENV === "development")
}, options);
delete options.loader;
delete options.use;
delete options.fallback;
Expand Down Expand Up @@ -232,21 +226,14 @@ ExtractTextPlugin.prototype.apply = function(compiler) {
var id = this.id;
var extractedChunks, entryChunks, initialChunks;
compilation.plugin("optimize-tree", function(chunks, modules, callback) {
extractedChunks = chunks.map(function() {
return new Chunk();
extractedChunks = chunks.map(function(chunk) {
return new Chunk(chunk.name);
});
chunks.forEach(function(chunk, i) {
var extractedChunk = extractedChunks[i];
extractedChunk.index = i;
extractedChunk.originalChunk = chunk;
extractedChunk.name = chunk.name;
extractedChunk.entrypoints = chunk.entrypoints;
chunk.chunks.forEach(function(c) {
extractedChunk.addChunk(extractedChunks[chunks.indexOf(c)]);
});
chunk.parents.forEach(function(c) {
extractedChunk.addParent(extractedChunks[chunks.indexOf(c)]);
});
extractedChunk.originalChunk = chunk;
splitChunk(chunk, extractedChunk, extractedChunks);
});
async.forEach(chunks, function(chunk, callback) {
var extractedChunk = extractedChunks[chunks.indexOf(chunk)];
Expand Down Expand Up @@ -295,7 +282,7 @@ ExtractTextPlugin.prototype.apply = function(compiler) {

// HMR: inject file name into corresponding javascript modules in order to trigger
// appropriate hot module reloading of CSS
if (process.env.NODE_ENV === 'development') {
if (options.hot) {
compilation.plugin("before-chunk-assets", function() {
extractedChunks.forEach(function(extractedChunk) {
forEachChunkModule(extractedChunk, function(module) {
Expand Down Expand Up @@ -355,7 +342,11 @@ function getPath(compilation, source, chunk) {
}

function isChunk(chunk, error) {
if (!(chunk instanceof Chunk)) {
if (!chunk || (
!chunk.modulesIterable &&
!chunk.forEachModule &&
!chunk.modules
)) {
throw new Error(typeof error === 'string' ? error : 'chunk is not an instance of Chunk');
}

Expand All @@ -365,32 +356,53 @@ function isChunk(chunk, error) {
function forEachChunkModule(chunk, cb) {
isChunk(chunk);

// webpack >= 4.x.x
if (chunk.modulesIterable) {
Array.from(chunk.modulesIterable, (x) => {
cb(x);
return x;
})
}
// webpack >= 3.x.x
if (typeof chunk.forEachModule === 'function') {
else if (typeof chunk.forEachModule === 'function') {
chunk.forEachModule(cb);
}
// webpack < 3.x.x
else {
// webpack < 3.x.x
chunk.modules.forEach(cb);
}

// Nothing better to return...
return chunk;
}

function getChunkModulesArray(chunk) {
isChunk(chunk);

var arr = [];

// webpack >= 4.x.x
if (chunk.modulesIterable) {
return Array.from(chunk.modulesIterable);
}
// webpack >= 3.x.x
if ( typeof chunk.mapModules === 'function' ) {
arr = chunk.mapModules();
else if ( typeof chunk.mapModules === 'function' ) {
return chunk.mapModules();
}
else {
// webpack < 3.x.x
arr = chunk.modules.slice();
return chunk.modules.slice();
}
}

return arr;
function splitChunk(chunk, extractedChunk, extractedChunks) {
// webpack >= 4.x.x
if (typeof chunk.split === 'function') {
chunk.split(extractedChunk);
}
// webpack < 4.x.x
else {
extractedChunk.entrypoints = chunk.entrypoints;
chunk.chunks.forEach(function(c) {
extractedChunk.addChunk(extractedChunks[chunks.indexOf(c)]);
});
chunk.parents.forEach(function(c) {
extractedChunk.addParent(extractedChunks[chunks.indexOf(c)]);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll need to pass down extractedChunks to this function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PAkerstrand Good catch. It would be nice if there was a way to smoke test this against older versions. Thanks!

});
}
}
17 changes: 11 additions & 6 deletions loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ var LimitChunkCountPlugin = require("webpack/lib/optimize/LimitChunkCountPlugin"
var NS = fs.realpathSync(__dirname);

module.exports = function(source) {
if (process.env.NODE_ENV !== 'development') return source
var query = loaderUtils.getOptions(this) || {};
var hot = "hot" in query ? query.hot : (!process.env.NODE_ENV || process.env.NODE_ENV === "development");
if (!hot) return source

// We need to always require hotModuleReplacement.js for HMR to work in a wierd scenario
// where only one css file is imported. Otherwise HMR breaks when modules are disposed.
Expand Down Expand Up @@ -54,13 +56,14 @@ module.exports.pitch = function(request) {
}

var childFilename = "extract-text-webpack-plugin-output-filename"; // eslint-disable-line no-path-concat
var publicPath = typeof query.publicPath === "string" ? query.publicPath : this._compilation.outputOptions.publicPath;
var publicPath = typeof query.publicPath === "string" ? query.publicPath : null;
var outputOptions = {
filename: childFilename,
publicPath: publicPath
publicPath: publicPath || this._compilation.outputOptions.publicPath,
};
var hot = "hot" in query ? query.hot : (!process.env.NODE_ENV || process.env.NODE_ENV === "development");
var childCompiler = this._compilation.createChildCompiler("extract-text-webpack-plugin", outputOptions);
childCompiler.apply(new NodeTemplatePlugin(outputOptions));
childCompiler.apply(new NodeTemplatePlugin());
childCompiler.apply(new LibraryTemplatePlugin(null, "commonjs2"));
childCompiler.apply(new NodeTargetPlugin());
childCompiler.apply(new SingleEntryPlugin(this.context, "!!" + request));
Expand Down Expand Up @@ -139,13 +142,15 @@ module.exports.pitch = function(request) {
//
// All we need is a date that changes during dev, to trigger a reload since
// hashes generated based on the file contents are what trigger HMR.
if (process.env.NODE_ENV === 'development') {
if (hot) {
const pathVar = publicPath ?
`"${publicPath}"` : "__wepback_public_path__";
resultSource += `
if (module.hot) {
module.hot.accept();
if (module.hot.data) {
var neverUsed = ${+new Date()}
require(${loaderUtils.stringifyRequest(this, path.join(__dirname, "hotModuleReplacement.js"))})("${publicPath}", "%%extracted-file%%");
require(${loaderUtils.stringifyRequest(this, path.join(__dirname, "hotModuleReplacement.js"))})(${pathVar}, "%%extracted-file%%");
}
}`;
}
Expand Down
1 change: 1 addition & 0 deletions schema/loader-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = {
"filename": { "type": "string" },
"use": { "type": ["string", "array", "object"] },
"publicPath": { "type": "string" },
"hot": { "type": "boolean" },

// deprecated
"fallbackLoader": { "type": ["string", "array", "object"] },
Expand Down
4 changes: 4 additions & 0 deletions schema/plugin-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
"publicPath": {
"description": "",
"type": "string"
},
"hot": {
"description": "Enable HMR",
"type": "boolean"
}
}
}