Skip to content

Commit

Permalink
fix: esModule option issue fix
Browse files Browse the repository at this point in the history
  • Loading branch information
dangreen committed Aug 5, 2020
1 parent ae7d211 commit 6237b59
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
44 changes: 38 additions & 6 deletions test/esModule-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand All @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand All @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/simple.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import './style.css';
import './style-other.css';
import a from './style.css';
import b from './style-other.css';
2 changes: 1 addition & 1 deletion test/helpers/runInJsDom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 6237b59

Please sign in to comment.