Skip to content

Commit

Permalink
fix: esModule option issue (#476)
Browse files Browse the repository at this point in the history
  • Loading branch information
dangreen authored Aug 5, 2020
1 parent ae7d211 commit c623f27
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 14 deletions.
14 changes: 9 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ loaderApi.pitch = function loader(request) {
const injectType = options.injectType || 'styleTag';
const esModule =
typeof options.esModule !== 'undefined' ? options.esModule : false;

delete options.esModule;
const runtimeOptions = {
injectType: options.injectType,
attributes: options.attributes,
insert: options.insert,
base: options.base,
};

switch (injectType) {
case 'linkTag': {
Expand Down Expand Up @@ -80,7 +84,7 @@ if (module.hot) {
content = content.__esModule ? content.default : content;`
}
var options = ${JSON.stringify(options)};
var options = ${JSON.stringify(runtimeOptions)};
options.insert = ${insert};
Expand Down Expand Up @@ -177,7 +181,7 @@ if (module.hot) {
var refs = 0;
var update;
var options = ${JSON.stringify(options)};
var options = ${JSON.stringify(runtimeOptions)};
options.insert = ${insert};
options.singleton = ${isSingleton};
Expand Down Expand Up @@ -287,7 +291,7 @@ if (module.hot) {
}`
}
var options = ${JSON.stringify(options)};
var options = ${JSON.stringify(runtimeOptions)};
options.insert = ${insert};
options.singleton = ${isSingleton};
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 c623f27

Please sign in to comment.