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 compatibility with webpack 5 #698

Merged
merged 20 commits into from
Feb 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix: webpack 5 support
  • Loading branch information
alexander-akait committed Feb 15, 2021
commit cf882d2c0a71810ca67db0cebc5d0ed371a7e716
18 changes: 6 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 12 additions & 4 deletions src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export function pitch(request) {

const callback = this.async();

childCompiler.runAsChild((err, entries, compilation) => {
childCompiler.runAsChild((error, entries, compilation) => {
const assets = Object.create(null);
const assetsInfo = new Map();

Expand Down Expand Up @@ -199,7 +199,15 @@ export function pitch(request) {
}

const count = identifierCountMap.get(dependency.identifier) || 0;
const { CssDependency } = shared(this._compiler.webpack);
const { CssDependency } = shared(webpack, () => {
return {};
});

if (!CssDependency) {
throw new Error(
"You forgot to add 'mini-css-extract-plugin' plugin (i.e. `{ plugins: [new MiniCssExtractPlugin()] }`), please read https://github.com/webpack-contrib/mini-css-extract-plugin#getting-started"
);
}

this._module.addDependency(
(lastDep = new CssDependency(dependency, dependency.context, count))
Expand All @@ -213,8 +221,8 @@ export function pitch(request) {
}
};

if (err) {
return callback(err);
if (error) {
return callback(error);
}

if (compilation.errors.length > 0) {
Expand Down
34 changes: 29 additions & 5 deletions test/TestCases.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,18 +127,42 @@ describe('TestCases', () => {
);
}

webpack(webpackConfig, (err, stats) => {
if (err) {
done(err);
webpack(webpackConfig, (error, stats) => {
if (error) {
done(error);

return;
}

if (stats.hasErrors()) {
const errorsPath = path.join(directoryForCase, './errors.test.js');

if (fs.existsSync(errorsPath)) {
const { errors } = stats.compilation;
// eslint-disable-next-line global-require, import/no-dynamic-require
const errorFilters = require(errorsPath);
const filteredErrors = errors.filter(
// eslint-disable-next-line no-shadow
(error) =>
!errorFilters.some((errorFilter) => errorFilter.test(error))
);

if (filteredErrors.length > 0) {
done(new Error(`Errors:\n${filteredErrors.join(',\n')}`));

return;
}

done();

return;
}

done(new Error(stats.toString()));

return;
}

done();

if (stats.hasErrors() && stats.hasWarnings()) {
done(
new Error(
Expand Down
1 change: 1 addition & 0 deletions test/cases/no-loader/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './style.css';
3 changes: 3 additions & 0 deletions test/cases/no-loader/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: red;
}
18 changes: 18 additions & 0 deletions test/cases/no-loader/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Self from '../../../src';

module.exports = {
entry: './index.js',
module: {
rules: [
{
test: /\.css$/,
use: ['css-loader'],
},
],
},
plugins: [
new Self({
filename: '[name].css',
}),
],
};