Skip to content

Commit

Permalink
fix: compatibility with webpack@5 (#437)
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito committed Aug 10, 2020
1 parent 2ea3cf0 commit ed50491
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 31 deletions.
22 changes: 20 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'path';
import Module from 'module';

import { getOptions } from 'loader-utils';
import validateOptions from 'schema-utils';
Expand All @@ -11,6 +12,23 @@ import SyntaxError from './Error';
import parseOptions from './options';
import schema from './options.json';

const parentModule = module;

function exec(code, loaderContext) {
const { resource, context } = loaderContext;

const module = new Module(resource, parentModule);

// eslint-disable-next-line no-underscore-dangle
module.paths = Module._nodeModulePaths(context);
module.filename = resource;

// eslint-disable-next-line no-underscore-dangle
module._compile(code, resource);

return module.exports;
}

/**
* **PostCSS Loader**
*
Expand Down Expand Up @@ -113,7 +131,7 @@ export default function loader(content, sourceMap, meta = {}) {
// https://webpack.js.org/api/loaders/#deprecated-context-properties
if (postcssOptions.parser === 'postcss-js') {
// eslint-disable-next-line no-param-reassign
content = this.exec(content, this.resource);
content = exec(content, this);
}

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

if (options.sourceMap && typeof sourceMap === 'string') {
Expand Down
6 changes: 6 additions & 0 deletions test/options/__snapshots__/exec.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Options Exec should work Exec - {Boolean}: css 1`] = `"module.exports = \\"a {\\\\n color: green\\\\n}\\""`;

exports[`Options Exec should work Exec - {Boolean}: errors 1`] = `Array []`;

exports[`Options Exec should work Exec - {Boolean}: warnings 1`] = `Array []`;

exports[`Options Exec should work JSS - {String}: css 1`] = `
"module.exports = { a: { color: 'yellow' } }
"
Expand Down
57 changes: 28 additions & 29 deletions test/options/exec.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,34 @@ import {
} from '../helpers/index';

describe('Options Exec', () => {
// Todo loaderContext.exec is deprecated and removed in webpack 5
// it('should work Exec - {Boolean}', async () => {
// const compiler = getCompiler(
// './jss/exec/index.js',
// {},
// {
// module: {
// rules: [
// {
// test: /style\.(exec\.js|js)$/i,
// use: [
// {
// loader: path.resolve(__dirname, '../../src'),
// options: { exec: true },
// },
// ],
// },
// ],
// },
// }
// );
// const stats = await compile(compiler);
//
// const codeFromBundle = getCodeFromBundle('style.exec.js', stats);
//
// expect(codeFromBundle.css).toMatchSnapshot('css');
// expect(getWarnings(stats)).toMatchSnapshot('warnings');
// expect(getErrors(stats)).toMatchSnapshot('errors');
// });
it('should work Exec - {Boolean}', async () => {
const compiler = getCompiler(
'./jss/exec/index.js',
{},
{
module: {
rules: [
{
test: /style\.(exec\.js|js)$/i,
use: [
{
loader: path.resolve(__dirname, '../../src'),
options: { exec: true },
},
],
},
],
},
}
);
const stats = await compile(compiler);

const codeFromBundle = getCodeFromBundle('style.exec.js', stats);

expect(codeFromBundle.css).toMatchSnapshot('css');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});

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

0 comments on commit ed50491

Please sign in to comment.