Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: get rid of webpack--version #440

Merged
merged 1 commit into from
Mar 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions examples/base-webpack.config.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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