Skip to content

Commit

Permalink
fix(loader): get rid of webpack--version
Browse files Browse the repository at this point in the history
Webpack versions util cause some troubles, so this fix should cover them

BREAKING CHANGE:
delete get-webpack-version.js

ISSUES CLOSED: #437, #417
  • Loading branch information
Stanislav Germanovskii committed Mar 6, 2021
1 parent 4ed89bf commit 54a0c6b
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 55 deletions.
7 changes: 0 additions & 7 deletions 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: {
Expand All @@ -24,8 +21,4 @@ const config = {
}
};

if (webpackVersion >= 4) {
config.mode = 'development';
}

module.exports = config;
13 changes: 6 additions & 7 deletions lib/plugin.js
Expand Up @@ -9,8 +9,7 @@ const {
MappedList,
replaceInModuleSource,
replaceSpritePlaceholder,
getMatchedRule,
getWebpackVersion
getMatchedRule
} = require('./utils');

const defaultConfig = {
Expand Down Expand Up @@ -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, () => {
Expand Down
9 changes: 3 additions & 6 deletions 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;
21 changes: 0 additions & 21 deletions lib/utils/get-webpack-version.js

This file was deleted.

1 change: 0 additions & 1 deletion lib/utils/index.js
Expand Up @@ -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');
Expand Down
6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -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"
}
}
21 changes: 11 additions & 10 deletions test/loader.test.js
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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'),
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down

0 comments on commit 54a0c6b

Please sign in to comment.