Skip to content

Commit ede7a52

Browse files
author
Stanislav Germanovskii
authored
Merge pull request #440 from JetBrains/Ticket-437
fix: get rid of webpack--version
2 parents 4ed89bf + 54a0c6b commit ede7a52

File tree

7 files changed

+23
-55
lines changed

7 files changed

+23
-55
lines changed

examples/base-webpack.config.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
const path = require('path');
22
const packageName = require('../package.json').name;
3-
const { getWebpackVersion } = require('../lib/utils');
4-
5-
const webpackVersion = getWebpackVersion();
63

74
const config = {
85
output: {
@@ -24,8 +21,4 @@ const config = {
2421
}
2522
};
2623

27-
if (webpackVersion >= 4) {
28-
config.mode = 'development';
29-
}
30-
3124
module.exports = config;

lib/plugin.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ const {
99
MappedList,
1010
replaceInModuleSource,
1111
replaceSpritePlaceholder,
12-
getMatchedRule,
13-
getWebpackVersion
12+
getMatchedRule
1413
} = require('./utils');
1514

1615
const defaultConfig = {
@@ -77,11 +76,11 @@ class SVGSpritePlugin {
7776
compilation.hooks
7877
.afterOptimizeChunks
7978
.tap(NAMESPACE, () => this.afterOptimizeChunks(compilation));
80-
if (!getWebpackVersion.IS_5) {
81-
compilation.hooks
82-
.optimizeExtractedChunks
83-
.tap(NAMESPACE, chunks => this.optimizeExtractedChunks(chunks));
84-
}
79+
80+
compilation.hooks
81+
.optimizeExtractedChunks
82+
.tap(NAMESPACE, chunks => this.optimizeExtractedChunks(chunks));
83+
8584
compilation.hooks
8685
.additionalAssets
8786
.tapPromise(NAMESPACE, () => {

lib/utils/get-matched-rule.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
/* eslint-disable global-require */
2-
const getWebpackVersion = require('./get-webpack-version');
32

43
let getMatchedRule = null;
54

6-
if (getWebpackVersion.IS_5) {
7-
// webpack5 and upper
8-
getMatchedRule = require('./get-matched-rule-5');
9-
} else {
10-
// webpack4 and lower
5+
try {
116
getMatchedRule = require('./get-matched-rule-4');
7+
} catch (e) {
8+
getMatchedRule = require('./get-matched-rule-5');
129
}
1310

1411
module.exports = getMatchedRule;

lib/utils/get-webpack-version.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

lib/utils/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ module.exports.generateSpritePlaceholder = require('./generate-sprite-placeholde
44
module.exports.getAllModules = require('./get-all-modules');
55
// module.exports.getLoaderOptions = require('./get-loader-options');
66
module.exports.getModuleChunk = require('./get-module-chunk');
7-
module.exports.getWebpackVersion = require('./get-webpack-version');
87
module.exports.getMatchedRule = require('./get-matched-rule');
98
module.exports.isModuleShouldBeExtracted = require('./is-module-should-be-extracted');
109
module.exports.interpolate = require('./interpolate');

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@
100100
"release:dry-run": "standard-version --no-verify",
101101
"test": "mocha test/*.test.js",
102102
"test:all": "yarn test:webpack-2 && yarn test:webpack-3 && yarn test:webpack-4",
103-
"test:webpack-2": "yarn env webpack-2 && yarn test",
104-
"test:webpack-3": "yarn env webpack-3 && yarn test",
105-
"test:webpack-4": "yarn env webpack-4 && yarn test",
103+
"test:webpack-2": "yarn env webpack-2 && env WEBPACK_VERSION=2 yarn test",
104+
"test:webpack-3": "yarn env webpack-3 && env WEBPACK_VERSION=3 yarn test",
105+
"test:webpack-4": "yarn env webpack-4 && env WEBPACK_VERSION=4 yarn test",
106106
"upload-coverage": "codeclimate-test-reporter < coverage/lcov.info"
107107
}
108108
}

test/loader.test.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ const webpack = require('webpack');
33
const HtmlPlugin = require('html-webpack-plugin');
44

55
const { isWebpack1 } = require('../lib/utils');
6-
const webpackVersion = require('../lib/utils/get-webpack-version');
76
const { loaderPath, fixturesPath } = require('./_config');
87
const {
98
rule,
@@ -75,7 +74,7 @@ describe('loader and plugin', () => {
7574

7675
it('should warn if there is remaining loaders in extract mode', async () => {
7776
const v4Config = {};
78-
if (webpackVersion.IS_4) {
77+
if (process.env.WEBPACK_VERSION === '4') {
7978
v4Config.mode = 'development';
8079
v4Config.devtool = false;
8180
}
@@ -212,7 +211,7 @@ describe('loader and plugin', () => {
212211
const extractor = extractPlugin('[name].css', { allChunks: true });
213212

214213
const v4Config = {};
215-
if (webpackVersion.IS_4) {
214+
if (process.env.WEBPACK_VERSION === '4') {
216215
v4Config.mode = 'development';
217216
v4Config.devtool = false;
218217
}
@@ -244,8 +243,8 @@ describe('loader and plugin', () => {
244243
assets['entry2.css'].source().should.contain('entry2.svg');
245244
});
246245

247-
if (!webpackVersion.IS_4) {
248-
it('should work in combination with CommonsChunkPlugin', async () => {
246+
it('should work in combination with CommonsChunkPlugin', async () => {
247+
try {
249248
const extractor = extractPlugin('[name].css');
250249
const { assets } = await compile({
251250
context: path.resolve(fixturesPath, 'extract-text-webpack-plugin/with-commons-chunk-plugin'),
@@ -268,8 +267,10 @@ describe('loader and plugin', () => {
268267
});
269268

270269
assets['common.css'].source().should.contain('common.svg');
271-
});
272-
}
270+
} catch (e) {
271+
e.message.should.contain('webpack.optimize.CommonsChunkPlugin has been removed');
272+
}
273+
});
273274
});
274275

275276
describe('html-loader interop', () => {
@@ -318,7 +319,7 @@ describe('loader and plugin', () => {
318319
});
319320

320321
// webpack 3 scope hoisting interop
321-
if (webpackVersion.IS_3) {
322+
if (process.env.WEBPACK_VERSION === '3') {
322323
// eslint-disable-next-line global-require,import/no-unresolved
323324
const ModuleConcatenationPlugin = require('webpack/lib/optimize/ModuleConcatenationPlugin');
324325

@@ -429,7 +430,7 @@ describe('loader and plugin', () => {
429430
const spriteFilename = defaultSpriteFilename;
430431

431432
const v4Config = {};
432-
if (webpackVersion.IS_4) {
433+
if (process.env.WEBPACK_VERSION === '4') {
433434
v4Config.mode = 'development';
434435
v4Config.devtool = false;
435436
}
@@ -451,7 +452,7 @@ describe('loader and plugin', () => {
451452
const spriteFilename = defaultSpriteFilename;
452453

453454
const v4Config = {};
454-
if (webpackVersion.IS_4) {
455+
if (process.env.WEBPACK_VERSION === '4') {
455456
v4Config.mode = 'development';
456457
v4Config.devtool = false;
457458
}

0 commit comments

Comments
 (0)