Skip to content

Commit

Permalink
feat: Add support for webpack 5
Browse files Browse the repository at this point in the history
  • Loading branch information
danez committed Jul 20, 2020
1 parent e40dbe9 commit 920c6d9
Show file tree
Hide file tree
Showing 16 changed files with 68 additions and 64 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"global-require": "off",
"class-methods-use-this": "off",
"no-param-reassign": "off",
"no-underscore-dangle": "off"
"no-underscore-dangle": "off",
"no-undefined": "off"
},
"overrides": [
{ "files": "**/*.test.js", "parserOptions": { "sourceType": "module" } }
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
coverage
node_modules
npm-debug.log
tests/tmp
tests/integration/tmp
yarn-error.log
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ node_js:
- "10"
cache:
yarn: true
before_script: 'if [[ "$WEBPACK" != "default" ]]; then yarn add --dev webpack@^${WEBPACK}; fi'
script: yarn test
env:
- WEBPACK=default
- WEBPACK=5.0.0-0
matrix:
include:
- node_js: "lts/*"
script: yarn lint
script: yarn test
26 changes: 9 additions & 17 deletions src/plugins/CachePluginFactory.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
"use strict";

const CachePlugin = require("webpack/lib/CachePlugin");
let CachePlugin;
try {
// webpack 4
CachePlugin = require("webpack/lib/CachePlugin");
} catch (e) {
// webpack 5
// eslint-disable-next-line import/no-unresolved
CachePlugin = require("webpack/lib/cache/MemoryCachePlugin");
}

class CachePluginFactory {
constructor() {
this.plugins = {};
this.dependencies = {};
}

addPlugin(target, compiler) {
if (!this.plugins[target]) {
this.plugins[target] = new CachePlugin();
}
this.plugins[target].apply(compiler);
if (this.dependencies[target]) {
compiler._lastCompilationFileDependencies = this.dependencies[
target
].file;
compiler._lastCompilationContextDependencies = this.dependencies[
target
].context;
}
}

updateDependencies(target, compiler) {
this.dependencies[target] = {
file: compiler._lastCompilationFileDependencies,
context: compiler._lastCompilationContextDependencies,
};
}
}

Expand Down
3 changes: 0 additions & 3 deletions tasks/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ module.exports = (grunt) => {
}

const handler = (err, stats) => {
if (opts.cache) {
cachePluginFactory.updateDependencies(target, compiler);
}
if (err) {
done(err);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = function (grunt) {
},
webpack: {
test: (config) =>({
mode: "none",
entry: path.join(__dirname, "entry"),
output: {
path: __dirname,
Expand Down
1 change: 1 addition & 0 deletions tests/integration/fixtures/webpack/basic/Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = function (grunt) {
grunt.initConfig({
webpack: {
test: {
mode: "none",
entry: path.join(__dirname, "entry"),
output: {
path: __dirname,
Expand Down
1 change: 1 addition & 0 deletions tests/integration/fixtures/webpack/cache/Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = function (grunt) {
grunt.initConfig({
webpack: {
test: {
mode: "none",
cache: true,
entry: path.join(__dirname, "entry"),
output: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require('path');
const webpack = require('webpack');
const loadGruntWebpackTasks = require('../../../../utils/loadGruntWebpackTasks');
const path = require("path");
const webpack = require("webpack");
const loadGruntWebpackTasks = require("../../../../utils/loadGruntWebpackTasks");

const plugin = new webpack.DefinePlugin({ test: JSON.stringify("test") });
plugin.circle = plugin;
Expand All @@ -9,15 +9,14 @@ module.exports = function (grunt) {
grunt.initConfig({
webpack: {
test: {
entry: path.join(__dirname, "entry"),
output: {
path: __dirname,
filename: "output.js",
},
plugins: [
plugin,
]
}
mode: "none",
entry: path.join(__dirname, "entry"),
output: {
path: __dirname,
filename: "output.js",
},
plugins: [plugin],
},
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ module.exports = function (grunt) {
grunt.initConfig({
webpack: {
test: {
mode: "none",
stats: false,
entry: path.join(__dirname, "entry"),
output: {
path: __dirname,
filename: "output.js",
},
stats:{
colors: false
}
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
assertGrunt.failed();

expect(stdout).toMatch(/Can't resolve '\.\/b'/);
expect(stdout).toMatch(/@ \.\/entry\.js 1:8-22/);
expect(stdout).toMatch(/\.\/entry\.js 1:8-22/);
4 changes: 4 additions & 0 deletions tests/integration/fixtures/webpack/error/Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ module.exports = function (grunt) {
grunt.initConfig({
webpack: {
test: {
mode: "none",
entry: path.join(__dirname, "entry"),
output: {
path: __dirname,
filename: "output.js",
},
stats:{
colors: false
}
},
},
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/fixtures/webpack/error/exec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
assertGrunt.failed();

expect(stdout).toMatch(/Can't resolve '\.\/b'/);
expect(stdout).toMatch(/@ \.\/entry\.js 1:8-22/);
expect(stdout).toMatch(/\.\/entry\.js 1:8-22/);
26 changes: 13 additions & 13 deletions tests/integration/fixtures/webpack/loader-path/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
const path = require('path');
const path = require("path");

const config = {
entry: {
'main': './main.js',
},
module: {
rules: [
{
test: /\.js/,
use: ['babel-loader'],
},
]
},
mode: "none",
entry: {
main: "./main.js",
},
module: {
rules: [
{
test: /\.js/,
use: ["babel-loader"],
},
],
},
};

module.exports = config;

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require('path');
const webpack = require('webpack');
const loadGruntWebpackTasks = require('../../../../utils/loadGruntWebpackTasks');
const path = require("path");
const webpack = require("webpack");
const loadGruntWebpackTasks = require("../../../../utils/loadGruntWebpackTasks");

module.exports = function (grunt) {
grunt.initConfig({
Expand All @@ -12,21 +12,21 @@ module.exports = function (grunt) {
},
test: [
{
mode: "none",
entry: path.join(__dirname, "entry"),
output: {
path: __dirname,
filename: "bundle.js",
},
plugins: [
new webpack.BannerPlugin("this is a banner"),
],
plugins: [new webpack.BannerPlugin("this is a banner")],
},
{
mode: "none",
entry: path.join(__dirname, "entry2"),
output: {
path: __dirname,
filename: "bundle2.js",
}
},
},
],
},
Expand Down
14 changes: 7 additions & 7 deletions tests/integration/fixtures/webpack/multi-compiler/Gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require('path');
const webpack = require('webpack');
const loadGruntWebpackTasks = require('../../../../utils/loadGruntWebpackTasks');
const path = require("path");
const webpack = require("webpack");
const loadGruntWebpackTasks = require("../../../../utils/loadGruntWebpackTasks");

module.exports = function (grunt) {
grunt.initConfig({
Expand All @@ -12,21 +12,21 @@ module.exports = function (grunt) {
},
test: [
{
mode: "none",
entry: path.join(__dirname, "entry"),
output: {
path: __dirname,
filename: "bundle.js",
},
plugins: [
new webpack.BannerPlugin("this is a banner"),
],
plugins: [new webpack.BannerPlugin("this is a banner")],
},
{
mode: "none",
entry: path.join(__dirname, "entry2"),
output: {
path: __dirname,
filename: "bundle2.js",
}
},
},
],
},
Expand Down

0 comments on commit 920c6d9

Please sign in to comment.