Skip to content
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

fix: esModule option issue fix #476

Merged
merged 2 commits into from
Aug 5, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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