Skip to content

Commit

Permalink
Fix Chunk.modules DeprecationWarning in Webpack 3.x
Browse files Browse the repository at this point in the history
Updates to Chunk.mapModules is not backwards compatible with Webpack 2.x due to breaking changes in webpack/webpack#4764

Following should be added to `README`:

### Install
```
# for webpack 3
npm install --save-dev extract-text-webpack-plugin
# for webpack 1, 2
npm install --save-dev extract-text-webpack-plugin@0.1.0
```
  • Loading branch information
steven-prybylynskyi committed Jul 20, 2017
1 parent 5e04244 commit 1c8a04f
Showing 1 changed file with 41 additions and 34 deletions.
75 changes: 41 additions & 34 deletions Plugin.js
@@ -1,44 +1,51 @@
'use strict';

class ExtractFilePlugin {
apply(compiler) {
compiler.plugin('this-compilation', (compilation) => {
compilation.plugin('normal-module-loader', (loaderContext, module) => {
loaderContext[__dirname] = () => {
module.meta[__dirname] = true;
};
});

compilation.plugin('additional-assets', callback => {
compilation.chunks.forEach(chunk => {
chunk.modules.forEach(module => processModule(chunk, module));
});

callback();
});
});
}

apply(compiler) {

compiler.plugin('this-compilation', (compilation) => {

compilation.plugin('normal-module-loader', (loaderContext, module) => {
loaderContext[__dirname] = () => {
module.meta[__dirname] = true;
};
});

compilation.plugin('additional-assets', callback => {
compilation.chunks.forEach(chunk => {
chunk.forEachModule(module => processModule(chunk, module));
});

callback();
});
});
}
}

function processModule(chunk, ourModule) {
if (ourModule.meta && ourModule.meta[__dirname]) {
// let's find module, which was issued by ours (proxied module)
chunk.modules.some((module) => {
if (module.reasons.some(reason => reason.module === ourModule)) {
// add assets from that module
addAssets(chunk, module);
// break cycle
return true;
}
});
}

if (ourModule.meta && ourModule.meta[__dirname]) {

let moduleFound = false;

// let's find module, which was issued by ours (proxied module)
chunk.forEachModule((module) => {

if (!moduleFound && module.reasons.some(reason => reason.module === ourModule)) {
// add assets from that module
addAssets(chunk, module);
// break cycle
moduleFound = true;
}
});
}
}

function addAssets(chunk, module) {
// add any emitted assets via proxied module to this chunk
for (var file in module.assets) {
chunk.files.push(file);
}

// add any emitted assets via proxied module to this chunk
for (let file in module.assets) {
chunk.files.push(file);
}
}

module.exports = ExtractFilePlugin;

0 comments on commit 1c8a04f

Please sign in to comment.