Skip to content

Commit ed50491

Browse files
fix: compatibility with webpack@5 (#437)
1 parent 2ea3cf0 commit ed50491

File tree

3 files changed

+54
-31
lines changed

3 files changed

+54
-31
lines changed

src/index.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from 'path';
2+
import Module from 'module';
23

34
import { getOptions } from 'loader-utils';
45
import validateOptions from 'schema-utils';
@@ -11,6 +12,23 @@ import SyntaxError from './Error';
1112
import parseOptions from './options';
1213
import schema from './options.json';
1314

15+
const parentModule = module;
16+
17+
function exec(code, loaderContext) {
18+
const { resource, context } = loaderContext;
19+
20+
const module = new Module(resource, parentModule);
21+
22+
// eslint-disable-next-line no-underscore-dangle
23+
module.paths = Module._nodeModulePaths(context);
24+
module.filename = resource;
25+
26+
// eslint-disable-next-line no-underscore-dangle
27+
module._compile(code, resource);
28+
29+
return module.exports;
30+
}
31+
1432
/**
1533
* **PostCSS Loader**
1634
*
@@ -113,7 +131,7 @@ export default function loader(content, sourceMap, meta = {}) {
113131
// https://webpack.js.org/api/loaders/#deprecated-context-properties
114132
if (postcssOptions.parser === 'postcss-js') {
115133
// eslint-disable-next-line no-param-reassign
116-
content = this.exec(content, this.resource);
134+
content = exec(content, this);
117135
}
118136

119137
if (typeof postcssOptions.parser === 'string') {
@@ -135,7 +153,7 @@ export default function loader(content, sourceMap, meta = {}) {
135153
// https://webpack.js.org/api/loaders/#deprecated-context-properties
136154
if (config.exec) {
137155
// eslint-disable-next-line no-param-reassign
138-
content = this.exec(content, this.resource);
156+
content = exec(content, this);
139157
}
140158

141159
if (options.sourceMap && typeof sourceMap === 'string') {

test/options/__snapshots__/exec.test.js.snap

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`Options Exec should work Exec - {Boolean}: css 1`] = `"module.exports = \\"a {\\\\n color: green\\\\n}\\""`;
4+
5+
exports[`Options Exec should work Exec - {Boolean}: errors 1`] = `Array []`;
6+
7+
exports[`Options Exec should work Exec - {Boolean}: warnings 1`] = `Array []`;
8+
39
exports[`Options Exec should work JSS - {String}: css 1`] = `
410
"module.exports = { a: { color: 'yellow' } }
511
"

test/options/exec.test.js

+28-29
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,34 @@ import {
99
} from '../helpers/index';
1010

1111
describe('Options Exec', () => {
12-
// Todo loaderContext.exec is deprecated and removed in webpack 5
13-
// it('should work Exec - {Boolean}', async () => {
14-
// const compiler = getCompiler(
15-
// './jss/exec/index.js',
16-
// {},
17-
// {
18-
// module: {
19-
// rules: [
20-
// {
21-
// test: /style\.(exec\.js|js)$/i,
22-
// use: [
23-
// {
24-
// loader: path.resolve(__dirname, '../../src'),
25-
// options: { exec: true },
26-
// },
27-
// ],
28-
// },
29-
// ],
30-
// },
31-
// }
32-
// );
33-
// const stats = await compile(compiler);
34-
//
35-
// const codeFromBundle = getCodeFromBundle('style.exec.js', stats);
36-
//
37-
// expect(codeFromBundle.css).toMatchSnapshot('css');
38-
// expect(getWarnings(stats)).toMatchSnapshot('warnings');
39-
// expect(getErrors(stats)).toMatchSnapshot('errors');
40-
// });
12+
it('should work Exec - {Boolean}', async () => {
13+
const compiler = getCompiler(
14+
'./jss/exec/index.js',
15+
{},
16+
{
17+
module: {
18+
rules: [
19+
{
20+
test: /style\.(exec\.js|js)$/i,
21+
use: [
22+
{
23+
loader: path.resolve(__dirname, '../../src'),
24+
options: { exec: true },
25+
},
26+
],
27+
},
28+
],
29+
},
30+
}
31+
);
32+
const stats = await compile(compiler);
33+
34+
const codeFromBundle = getCodeFromBundle('style.exec.js', stats);
35+
36+
expect(codeFromBundle.css).toMatchSnapshot('css');
37+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
38+
expect(getErrors(stats)).toMatchSnapshot('errors');
39+
});
4140

4241
it('should work JSS - {String}', async () => {
4342
const compiler = getCompiler(

0 commit comments

Comments
 (0)