Skip to content

Commit c623f27

Browse files
authored
fix: esModule option issue (#476)
1 parent ae7d211 commit c623f27

File tree

4 files changed

+50
-14
lines changed

4 files changed

+50
-14
lines changed

src/index.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ loaderApi.pitch = function loader(request) {
2626
const injectType = options.injectType || 'styleTag';
2727
const esModule =
2828
typeof options.esModule !== 'undefined' ? options.esModule : false;
29-
30-
delete options.esModule;
29+
const runtimeOptions = {
30+
injectType: options.injectType,
31+
attributes: options.attributes,
32+
insert: options.insert,
33+
base: options.base,
34+
};
3135

3236
switch (injectType) {
3337
case 'linkTag': {
@@ -80,7 +84,7 @@ if (module.hot) {
8084
content = content.__esModule ? content.default : content;`
8185
}
8286
83-
var options = ${JSON.stringify(options)};
87+
var options = ${JSON.stringify(runtimeOptions)};
8488
8589
options.insert = ${insert};
8690
@@ -177,7 +181,7 @@ if (module.hot) {
177181
178182
var refs = 0;
179183
var update;
180-
var options = ${JSON.stringify(options)};
184+
var options = ${JSON.stringify(runtimeOptions)};
181185
182186
options.insert = ${insert};
183187
options.singleton = ${isSingleton};
@@ -287,7 +291,7 @@ if (module.hot) {
287291
}`
288292
}
289293
290-
var options = ${JSON.stringify(options)};
294+
var options = ${JSON.stringify(runtimeOptions)};
291295
292296
options.insert = ${insert};
293297
options.singleton = ${isSingleton};

test/esModule-option.test.js

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,16 @@ describe('"esModule" option', () => {
1717
'lazySingletonStyleTag',
1818
'linkTag',
1919
];
20+
const commonjsExports = {
21+
styleTag: 'module.exports = content.locals || {}',
22+
singletonStyleTag: 'module.exports = content.locals || {}',
23+
lazyStyleTag: 'module.exports = exported',
24+
lazySingletonStyleTag: 'module.exports = exported',
25+
};
2026

2127
injectTypes.forEach((injectType) => {
28+
const commonjsExport = commonjsExports[injectType];
29+
2230
it(`should work when not specified and when the "injectType" option is "${injectType}"`, async () => {
2331
const entry = getEntryByInjectType('simple.js', injectType);
2432
const compiler = getCompiler(entry, { injectType });
@@ -37,8 +45,12 @@ describe('"esModule" option', () => {
3745
const compiler = getCompiler(entry, { injectType, esModule: true });
3846
const stats = await compile(compiler);
3947

40-
runInJsDom('main.bundle.js', compiler, stats, (dom) => {
48+
runInJsDom('main.bundle.js', compiler, stats, (dom, bundle) => {
4149
expect(dom.serialize()).toMatchSnapshot('DOM');
50+
51+
if (commonjsExport) {
52+
expect(bundle).not.toEqual(expect.stringContaining(commonjsExport));
53+
}
4254
});
4355

4456
expect(getWarnings(stats)).toMatchSnapshot('warnings');
@@ -74,8 +86,12 @@ describe('"esModule" option', () => {
7486
);
7587
const stats = await compile(compiler);
7688

77-
runInJsDom('main.bundle.js', compiler, stats, (dom) => {
89+
runInJsDom('main.bundle.js', compiler, stats, (dom, bundle) => {
7890
expect(dom.serialize()).toMatchSnapshot('DOM');
91+
92+
if (commonjsExport) {
93+
expect(bundle).not.toEqual(expect.stringContaining(commonjsExport));
94+
}
7995
});
8096

8197
expect(getWarnings(stats)).toMatchSnapshot('warnings');
@@ -117,8 +133,12 @@ describe('"esModule" option', () => {
117133
);
118134
const stats = await compile(compiler);
119135

120-
runInJsDom('main.bundle.js', compiler, stats, (dom) => {
136+
runInJsDom('main.bundle.js', compiler, stats, (dom, bundle) => {
121137
expect(dom.serialize()).toMatchSnapshot('DOM');
138+
139+
if (commonjsExport) {
140+
expect(bundle).not.toEqual(expect.stringContaining(commonjsExport));
141+
}
122142
});
123143

124144
expect(getWarnings(stats)).toMatchSnapshot('warnings');
@@ -130,8 +150,12 @@ describe('"esModule" option', () => {
130150
const compiler = getCompiler(entry, { injectType, esModule: false });
131151
const stats = await compile(compiler);
132152

133-
runInJsDom('main.bundle.js', compiler, stats, (dom) => {
153+
runInJsDom('main.bundle.js', compiler, stats, (dom, bundle) => {
134154
expect(dom.serialize()).toMatchSnapshot('DOM');
155+
156+
if (commonjsExport) {
157+
expect(bundle).toEqual(expect.stringContaining(commonjsExport));
158+
}
135159
});
136160

137161
expect(getWarnings(stats)).toMatchSnapshot('warnings');
@@ -167,8 +191,12 @@ describe('"esModule" option', () => {
167191
);
168192
const stats = await compile(compiler);
169193

170-
runInJsDom('main.bundle.js', compiler, stats, (dom) => {
194+
runInJsDom('main.bundle.js', compiler, stats, (dom, bundle) => {
171195
expect(dom.serialize()).toMatchSnapshot('DOM');
196+
197+
if (commonjsExport) {
198+
expect(bundle).toEqual(expect.stringContaining(commonjsExport));
199+
}
172200
});
173201

174202
expect(getWarnings(stats)).toMatchSnapshot('warnings');
@@ -210,8 +238,12 @@ describe('"esModule" option', () => {
210238
);
211239
const stats = await compile(compiler);
212240

213-
runInJsDom('main.bundle.js', compiler, stats, (dom) => {
241+
runInJsDom('main.bundle.js', compiler, stats, (dom, bundle) => {
214242
expect(dom.serialize()).toMatchSnapshot('DOM');
243+
244+
if (commonjsExport) {
245+
expect(bundle).toEqual(expect.stringContaining(commonjsExport));
246+
}
215247
});
216248

217249
expect(getWarnings(stats)).toMatchSnapshot('warnings');

test/fixtures/simple.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
import './style.css';
2-
import './style-other.css';
1+
import a from './style.css';
2+
import b from './style-other.css';

test/helpers/runInJsDom.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function runInJsDom(assetName, compiler, stats, testFn) {
3232

3333
dom.window.eval(bundle);
3434

35-
testFn(dom);
35+
testFn(dom, bundle);
3636

3737
// free memory associated with the window
3838
dom.window.close();

0 commit comments

Comments
 (0)