diff --git a/lib/plugin.js b/lib/plugin.js index c1908b1..e5cec93 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -4,7 +4,14 @@ const Chunk = require('webpack/lib/Chunk'); const SVGCompiler = require('svg-baker'); const Sprite = require('svg-baker/lib/sprite'); const { NAMESPACE } = require('./config'); -const { MappedList, replaceInModuleSource, replaceSpritePlaceholder } = require('./utils'); +const { + MappedList, + replaceInModuleSource, + replaceSpritePlaceholder, + getWebpackVersion +} = require('./utils'); + +const webpackVersion = parseInt(getWebpackVersion(), 10); class SVGSpritePlugin { constructor() { @@ -44,7 +51,9 @@ class SVGSpritePlugin { const replacements = map.groupItemsBySymbolFile((acc, item) => acc[item.resource] = item.useUrl); chunks.forEach((chunk) => { - chunk.modules + const modules = webpackVersion < 3 ? chunk.modules : chunk.mapModules(); + + modules // dirty hack to identify modules extracted by extract-text-webpack-plugin // TODO refactor .filter(module => '_originalModule' in module) diff --git a/lib/utils/get-module-chunk.js b/lib/utils/get-module-chunk.js index 19d3d99..95d6142 100644 --- a/lib/utils/get-module-chunk.js +++ b/lib/utils/get-module-chunk.js @@ -1,3 +1,5 @@ +const webpackVersion = require('./get-webpack-version'); + /** * Find nearest module chunk (not sure that is reliable method, but who cares). * @see http://stackoverflow.com/questions/43202761/how-to-determine-all-module-chunks-in-webpack @@ -6,7 +8,13 @@ * @return {Chunk|null} */ function getModuleChunk(module, modules) { - const { chunks } = module; + let chunks; + + if (parseInt(webpackVersion(), 10) >= 3) { + chunks = module.mapChunks(); + } else { + chunks = module.chunks; + } // webpack 1 compat const issuer = typeof module.issuer === 'string'