From 6237b59baeb4d703f3a8820be234b48ed1d6f116 Mon Sep 17 00:00:00 2001 From: dangreen Date: Wed, 5 Aug 2020 23:01:40 +0700 Subject: [PATCH] fix: esModule option issue fix --- src/index.js | 2 +- test/esModule-option.test.js | 44 +++++++++++++++++++++++++++++++----- test/fixtures/simple.js | 4 ++-- test/helpers/runInJsDom.js | 2 +- 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/src/index.js b/src/index.js index 13dfedeb..d3a25cfb 100644 --- a/src/index.js +++ b/src/index.js @@ -10,7 +10,7 @@ import schema from './options.json'; const loaderApi = () => {}; loaderApi.pitch = function loader(request) { - const options = loaderUtils.getOptions(this); + const options = { ...loaderUtils.getOptions(this) }; validateOptions(schema, options, { name: 'Style Loader', diff --git a/test/esModule-option.test.js b/test/esModule-option.test.js index 2e9e7c6f..75cbb5a1 100644 --- a/test/esModule-option.test.js +++ b/test/esModule-option.test.js @@ -17,8 +17,16 @@ describe('"esModule" option', () => { 'lazySingletonStyleTag', 'linkTag', ]; + const commonjsExports = { + styleTag: 'module.exports = content.locals || {}', + singletonStyleTag: 'module.exports = content.locals || {}', + lazyStyleTag: 'module.exports = exported', + lazySingletonStyleTag: 'module.exports = exported', + }; injectTypes.forEach((injectType) => { + const commonjsExport = commonjsExports[injectType]; + it(`should work when not specified and when the "injectType" option is "${injectType}"`, async () => { const entry = getEntryByInjectType('simple.js', injectType); const compiler = getCompiler(entry, { injectType }); @@ -37,8 +45,12 @@ describe('"esModule" option', () => { const compiler = getCompiler(entry, { injectType, esModule: true }); const stats = await compile(compiler); - runInJsDom('main.bundle.js', compiler, stats, (dom) => { + runInJsDom('main.bundle.js', compiler, stats, (dom, bundle) => { expect(dom.serialize()).toMatchSnapshot('DOM'); + + if (commonjsExport) { + expect(bundle).not.toEqual(expect.stringContaining(commonjsExport)); + } }); expect(getWarnings(stats)).toMatchSnapshot('warnings'); @@ -74,8 +86,12 @@ describe('"esModule" option', () => { ); const stats = await compile(compiler); - runInJsDom('main.bundle.js', compiler, stats, (dom) => { + runInJsDom('main.bundle.js', compiler, stats, (dom, bundle) => { expect(dom.serialize()).toMatchSnapshot('DOM'); + + if (commonjsExport) { + expect(bundle).not.toEqual(expect.stringContaining(commonjsExport)); + } }); expect(getWarnings(stats)).toMatchSnapshot('warnings'); @@ -117,8 +133,12 @@ describe('"esModule" option', () => { ); const stats = await compile(compiler); - runInJsDom('main.bundle.js', compiler, stats, (dom) => { + runInJsDom('main.bundle.js', compiler, stats, (dom, bundle) => { expect(dom.serialize()).toMatchSnapshot('DOM'); + + if (commonjsExport) { + expect(bundle).not.toEqual(expect.stringContaining(commonjsExport)); + } }); expect(getWarnings(stats)).toMatchSnapshot('warnings'); @@ -130,8 +150,12 @@ describe('"esModule" option', () => { const compiler = getCompiler(entry, { injectType, esModule: false }); const stats = await compile(compiler); - runInJsDom('main.bundle.js', compiler, stats, (dom) => { + runInJsDom('main.bundle.js', compiler, stats, (dom, bundle) => { expect(dom.serialize()).toMatchSnapshot('DOM'); + + if (commonjsExport) { + expect(bundle).toEqual(expect.stringContaining(commonjsExport)); + } }); expect(getWarnings(stats)).toMatchSnapshot('warnings'); @@ -167,8 +191,12 @@ describe('"esModule" option', () => { ); const stats = await compile(compiler); - runInJsDom('main.bundle.js', compiler, stats, (dom) => { + runInJsDom('main.bundle.js', compiler, stats, (dom, bundle) => { expect(dom.serialize()).toMatchSnapshot('DOM'); + + if (commonjsExport) { + expect(bundle).toEqual(expect.stringContaining(commonjsExport)); + } }); expect(getWarnings(stats)).toMatchSnapshot('warnings'); @@ -210,8 +238,12 @@ describe('"esModule" option', () => { ); const stats = await compile(compiler); - runInJsDom('main.bundle.js', compiler, stats, (dom) => { + runInJsDom('main.bundle.js', compiler, stats, (dom, bundle) => { expect(dom.serialize()).toMatchSnapshot('DOM'); + + if (commonjsExport) { + expect(bundle).toEqual(expect.stringContaining(commonjsExport)); + } }); expect(getWarnings(stats)).toMatchSnapshot('warnings'); diff --git a/test/fixtures/simple.js b/test/fixtures/simple.js index 9784c57c..490afd69 100644 --- a/test/fixtures/simple.js +++ b/test/fixtures/simple.js @@ -1,2 +1,2 @@ -import './style.css'; -import './style-other.css'; +import a from './style.css'; +import b from './style-other.css'; diff --git a/test/helpers/runInJsDom.js b/test/helpers/runInJsDom.js index 088751fb..343a06cc 100644 --- a/test/helpers/runInJsDom.js +++ b/test/helpers/runInJsDom.js @@ -32,7 +32,7 @@ function runInJsDom(assetName, compiler, stats, testFn) { dom.window.eval(bundle); - testFn(dom); + testFn(dom, bundle); // free memory associated with the window dom.window.close();