From 54a0c6bcf7dab8ccbbd4db713dccab8b7665473a Mon Sep 17 00:00:00 2001 From: Stanislav Germanovskii Date: Sat, 6 Mar 2021 21:57:10 +0300 Subject: [PATCH] fix(loader): get rid of webpack--version Webpack versions util cause some troubles, so this fix should cover them BREAKING CHANGE: delete get-webpack-version.js ISSUES CLOSED: #437, #417 --- examples/base-webpack.config.js | 7 ------- lib/plugin.js | 13 ++++++------- lib/utils/get-matched-rule.js | 9 +++------ lib/utils/get-webpack-version.js | 21 --------------------- lib/utils/index.js | 1 - package.json | 6 +++--- test/loader.test.js | 21 +++++++++++---------- 7 files changed, 23 insertions(+), 55 deletions(-) delete mode 100644 lib/utils/get-webpack-version.js diff --git a/examples/base-webpack.config.js b/examples/base-webpack.config.js index 67ef82a..b6bc2d2 100644 --- a/examples/base-webpack.config.js +++ b/examples/base-webpack.config.js @@ -1,8 +1,5 @@ const path = require('path'); const packageName = require('../package.json').name; -const { getWebpackVersion } = require('../lib/utils'); - -const webpackVersion = getWebpackVersion(); const config = { output: { @@ -24,8 +21,4 @@ const config = { } }; -if (webpackVersion >= 4) { - config.mode = 'development'; -} - module.exports = config; diff --git a/lib/plugin.js b/lib/plugin.js index 5dc97c7..4e1f5b7 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -9,8 +9,7 @@ const { MappedList, replaceInModuleSource, replaceSpritePlaceholder, - getMatchedRule, - getWebpackVersion + getMatchedRule } = require('./utils'); const defaultConfig = { @@ -77,11 +76,11 @@ class SVGSpritePlugin { compilation.hooks .afterOptimizeChunks .tap(NAMESPACE, () => this.afterOptimizeChunks(compilation)); - if (!getWebpackVersion.IS_5) { - compilation.hooks - .optimizeExtractedChunks - .tap(NAMESPACE, chunks => this.optimizeExtractedChunks(chunks)); - } + + compilation.hooks + .optimizeExtractedChunks + .tap(NAMESPACE, chunks => this.optimizeExtractedChunks(chunks)); + compilation.hooks .additionalAssets .tapPromise(NAMESPACE, () => { diff --git a/lib/utils/get-matched-rule.js b/lib/utils/get-matched-rule.js index dbc567c..e4e58f3 100644 --- a/lib/utils/get-matched-rule.js +++ b/lib/utils/get-matched-rule.js @@ -1,14 +1,11 @@ /* eslint-disable global-require */ -const getWebpackVersion = require('./get-webpack-version'); let getMatchedRule = null; -if (getWebpackVersion.IS_5) { - // webpack5 and upper - getMatchedRule = require('./get-matched-rule-5'); -} else { - // webpack4 and lower +try { getMatchedRule = require('./get-matched-rule-4'); +} catch (e) { + getMatchedRule = require('./get-matched-rule-5'); } module.exports = getMatchedRule; diff --git a/lib/utils/get-webpack-version.js b/lib/utils/get-webpack-version.js deleted file mode 100644 index e300ef1..0000000 --- a/lib/utils/get-webpack-version.js +++ /dev/null @@ -1,21 +0,0 @@ -// eslint-disable-next-line import/no-extraneous-dependencies -// require.main is undefined while running WebStorm code assistance analyzer of webpack.config.js -if (!require.main) { - require.main = { require }; -} -const webpackVersion = require.main.require('webpack/package.json').version; - -/** - * @param {boolean} [onlyMajor=true] - * @return {string} - */ -function getWebpackVersion(onlyMajor = true) { - return onlyMajor ? webpackVersion.split('.')[0] : webpackVersion; -} - -getWebpackVersion.IS_1 = getWebpackVersion() === '1'; -getWebpackVersion.IS_2 = getWebpackVersion() === '2'; -getWebpackVersion.IS_3 = getWebpackVersion() === '3'; -getWebpackVersion.IS_4 = getWebpackVersion() === '4'; -getWebpackVersion.IS_5 = getWebpackVersion() === '5'; -module.exports = getWebpackVersion; diff --git a/lib/utils/index.js b/lib/utils/index.js index 36fba83..5d82af6 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -4,7 +4,6 @@ module.exports.generateSpritePlaceholder = require('./generate-sprite-placeholde module.exports.getAllModules = require('./get-all-modules'); // module.exports.getLoaderOptions = require('./get-loader-options'); module.exports.getModuleChunk = require('./get-module-chunk'); -module.exports.getWebpackVersion = require('./get-webpack-version'); module.exports.getMatchedRule = require('./get-matched-rule'); module.exports.isModuleShouldBeExtracted = require('./is-module-should-be-extracted'); module.exports.interpolate = require('./interpolate'); diff --git a/package.json b/package.json index 5ffb72a..6b1189c 100644 --- a/package.json +++ b/package.json @@ -100,9 +100,9 @@ "release:dry-run": "standard-version --no-verify", "test": "mocha test/*.test.js", "test:all": "yarn test:webpack-2 && yarn test:webpack-3 && yarn test:webpack-4", - "test:webpack-2": "yarn env webpack-2 && yarn test", - "test:webpack-3": "yarn env webpack-3 && yarn test", - "test:webpack-4": "yarn env webpack-4 && yarn test", + "test:webpack-2": "yarn env webpack-2 && env WEBPACK_VERSION=2 yarn test", + "test:webpack-3": "yarn env webpack-3 && env WEBPACK_VERSION=3 yarn test", + "test:webpack-4": "yarn env webpack-4 && env WEBPACK_VERSION=4 yarn test", "upload-coverage": "codeclimate-test-reporter < coverage/lcov.info" } } diff --git a/test/loader.test.js b/test/loader.test.js index ecb38e6..694ec7b 100644 --- a/test/loader.test.js +++ b/test/loader.test.js @@ -3,7 +3,6 @@ const webpack = require('webpack'); const HtmlPlugin = require('html-webpack-plugin'); const { isWebpack1 } = require('../lib/utils'); -const webpackVersion = require('../lib/utils/get-webpack-version'); const { loaderPath, fixturesPath } = require('./_config'); const { rule, @@ -75,7 +74,7 @@ describe('loader and plugin', () => { it('should warn if there is remaining loaders in extract mode', async () => { const v4Config = {}; - if (webpackVersion.IS_4) { + if (process.env.WEBPACK_VERSION === '4') { v4Config.mode = 'development'; v4Config.devtool = false; } @@ -212,7 +211,7 @@ describe('loader and plugin', () => { const extractor = extractPlugin('[name].css', { allChunks: true }); const v4Config = {}; - if (webpackVersion.IS_4) { + if (process.env.WEBPACK_VERSION === '4') { v4Config.mode = 'development'; v4Config.devtool = false; } @@ -244,8 +243,8 @@ describe('loader and plugin', () => { assets['entry2.css'].source().should.contain('entry2.svg'); }); - if (!webpackVersion.IS_4) { - it('should work in combination with CommonsChunkPlugin', async () => { + it('should work in combination with CommonsChunkPlugin', async () => { + try { const extractor = extractPlugin('[name].css'); const { assets } = await compile({ context: path.resolve(fixturesPath, 'extract-text-webpack-plugin/with-commons-chunk-plugin'), @@ -268,8 +267,10 @@ describe('loader and plugin', () => { }); assets['common.css'].source().should.contain('common.svg'); - }); - } + } catch (e) { + e.message.should.contain('webpack.optimize.CommonsChunkPlugin has been removed'); + } + }); }); describe('html-loader interop', () => { @@ -318,7 +319,7 @@ describe('loader and plugin', () => { }); // webpack 3 scope hoisting interop - if (webpackVersion.IS_3) { + if (process.env.WEBPACK_VERSION === '3') { // eslint-disable-next-line global-require,import/no-unresolved const ModuleConcatenationPlugin = require('webpack/lib/optimize/ModuleConcatenationPlugin'); @@ -429,7 +430,7 @@ describe('loader and plugin', () => { const spriteFilename = defaultSpriteFilename; const v4Config = {}; - if (webpackVersion.IS_4) { + if (process.env.WEBPACK_VERSION === '4') { v4Config.mode = 'development'; v4Config.devtool = false; } @@ -451,7 +452,7 @@ describe('loader and plugin', () => { const spriteFilename = defaultSpriteFilename; const v4Config = {}; - if (webpackVersion.IS_4) { + if (process.env.WEBPACK_VERSION === '4') { v4Config.mode = 'development'; v4Config.devtool = false; }