diff --git a/src/index.js b/src/index.js index ea7d5324..57cafc6b 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,5 @@ import path from 'path'; +import Module from 'module'; import { getOptions } from 'loader-utils'; import validateOptions from 'schema-utils'; @@ -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** * @@ -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') { @@ -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') { diff --git a/test/options/__snapshots__/exec.test.js.snap b/test/options/__snapshots__/exec.test.js.snap index f75c9bbf..2fa06468 100644 --- a/test/options/__snapshots__/exec.test.js.snap +++ b/test/options/__snapshots__/exec.test.js.snap @@ -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' } } " diff --git a/test/options/exec.test.js b/test/options/exec.test.js index 60f46385..d48c1475 100644 --- a/test/options/exec.test.js +++ b/test/options/exec.test.js @@ -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(