Skip to content

fix: exec option for @webpack5 #437

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

Merged
merged 1 commit into from
Aug 10, 2020
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
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