From 1c8a04f692fc84ef888c176b236fdc1d5247305e Mon Sep 17 00:00:00 2001 From: Steven Pribilinskiy Date: Thu, 20 Jul 2017 21:08:41 +0300 Subject: [PATCH] Fix `Chunk.modules` DeprecationWarning in Webpack 3.x Updates to Chunk.mapModules is not backwards compatible with Webpack 2.x due to breaking changes in https://github.com/webpack/webpack/pull/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 ``` --- Plugin.js | 75 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 41 insertions(+), 34 deletions(-) diff --git a/Plugin.js b/Plugin.js index faccef1..dcd3a74 100644 --- a/Plugin.js +++ b/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;