Skip to content

Align with webpack api. #29

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

Merged
merged 1 commit into from
Jul 27, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 56 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,51 @@ var pluginSchema = require('./schema/plugin-schema.json');
var NS = fs.realpathSync(__dirname);
var nextId = 0;

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

return true;
}

function forEachChunkModule( chunk, cb ) {
isChunk( chunk );

// webpack >= 3.x.x
if ( typeof chunk.forEachModule === 'function' ) {
cb.forEachModule( cb );
}
else {
// webpack < 3.x.x
chunk.modules.forEach( cb );
}

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

function getChunkModulesArray( chunk ) {
isChunk( chunk );

var arr = [];

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

return arr;
}

function ExtractTextPluginCompilation() {
this.modulesByIdentifier = {};
}


ExtractTextPluginCompilation.prototype.addModule = function(identifier, originalModule, source, additionalInformation, sourceMap, prevModules) {
var m;
if(!this.modulesByIdentifier[identifier]) {
Expand Down Expand Up @@ -53,11 +93,15 @@ ExtractTextPluginCompilation.prototype.addResultToChunk = function(identifier, r
};

ExtractTextPlugin.prototype.renderExtractedChunk = function(chunk) {
var that = this;
var source = new ConcatSource();
chunk.modules.forEach(function(module) {

forEachChunkModule( chunk, function ( module ) {
var moduleSource = module.source();
source.add(this.applyAdditionalInformation(moduleSource, module.additionalInformation));
}, this);

source.add( that.applyAdditionalInformation( moduleSource, module.additionalInformation ) );
} );

return source;
};

Expand Down Expand Up @@ -251,7 +295,7 @@ ExtractTextPlugin.prototype.apply = function(compiler) {
// SETTING THIS TO TRUE INSURES ALL CHUNKS ARE HANDLED:
var shouldExtract = true; //!!(options.allChunks || chunk.isInitial());

async.forEach(chunk.modules.slice(), function(module, callback) {
async.forEach(getChunkModulesArray(chunk), function(module, callback) {
var meta = module[NS];
if(meta && (!meta.options.id || meta.options.id === id)) {
var wasExtracted = Array.isArray(meta.content);
Expand Down Expand Up @@ -294,7 +338,7 @@ ExtractTextPlugin.prototype.apply = function(compiler) {
if (process.env.NODE_ENV === 'development') {
compilation.plugin("before-chunk-assets", function() {
extractedChunks.forEach(function(extractedChunk) {
extractedChunk.modules.forEach(function(module) {
forEachChunkModule(extractedChunk, function(module) {
if(module.__fileInjected) return;
module.__fileInjected = true;

Expand All @@ -308,9 +352,11 @@ ExtractTextPlugin.prototype.apply = function(compiler) {

// add the css files to assets and the files array corresponding to its chunks
compilation.plugin("additional-assets", function(callback) {
extractedChunks.forEach(function(extractedChunk) {
if(extractedChunk.modules.length) {
extractedChunk.modules.sort(function(a, b) {
extractedChunks.forEach(function(extractedChunk) {
var extractedChunkModules = getChunkModulesArray(extractedChunk);

if ( extractedChunkModules.length ) {
extractedChunkModules.sort(function(a, b) {
if(!options.ignoreOrder && isInvalidOrder(a, b)) {
compilation.errors.push(new OrderUndefinedError(a.getOriginalModule()));
compilation.errors.push(new OrderUndefinedError(b.getOriginalModule()));
Expand All @@ -320,7 +366,7 @@ ExtractTextPlugin.prototype.apply = function(compiler) {

var stylesheet = this.renderExtractedChunk(extractedChunk);
var chunk = extractedChunk.originalChunk;
var file = getFile(compilation, filename, stylesheet, chunk)
var file = getFile(compilation, filename, stylesheet, chunk);

compilation.assets[file] = stylesheet;
chunk.files.push(file);
Expand Down