Skip to content

Commit 03d3df3

Browse files
fix: noop in environment without DOM API (#597)
1 parent 382485f commit 03d3df3

6 files changed

+74
-1
lines changed

src/runtime/injectStylesIntoLinkTag.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
module.exports = (url, options) => {
2+
if (typeof document === "undefined") {
3+
return () => {};
4+
}
5+
26
options = options || {};
37
options.attributes =
48
typeof options.attributes === "object" ? options.attributes : {};

src/runtime/isOldIE.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ function isOldIE() {
88
// Tests for existence of standard globals is to allow style-loader
99
// to operate correctly into non-standard environments
1010
// @see https://github.com/webpack-contrib/style-loader/issues/177
11-
memo = Boolean(window && document && document.all && !window.atob);
11+
memo = Boolean(
12+
typeof window !== "undefined" &&
13+
typeof document !== "undefined" &&
14+
document.all &&
15+
!window.atob
16+
);
1217
}
1318

1419
return memo;

src/runtime/singletonStyleDomAPI.js

+6
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ const singletonData = {
7474

7575
/* istanbul ignore next */
7676
function domAPI(options) {
77+
if (typeof document === "undefined")
78+
return {
79+
update: () => {},
80+
remove: () => {},
81+
};
82+
7783
// eslint-disable-next-line no-undef,no-use-before-define
7884
const styleIndex = singletonData.singletonCounter++;
7985
const styleElement =

src/runtime/styleDomAPI.js

+7
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ function removeStyleElement(styleElement) {
5454

5555
/* istanbul ignore next */
5656
function domAPI(options) {
57+
if (typeof document === "undefined") {
58+
return {
59+
update: () => {},
60+
remove: () => {},
61+
};
62+
}
63+
5764
const styleElement = options.insertStyleElement(options);
5865

5966
return {

test/__snapshots__/injectType-option.test.js.snap

+28
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`"injectType" option should work when the "injectType" option is "autoStyleTag" with non DOM environment: errors 1`] = `Array []`;
4+
5+
exports[`"injectType" option should work when the "injectType" option is "autoStyleTag" with non DOM environment: warnings 1`] = `Array []`;
6+
37
exports[`"injectType" option should work when the "injectType" option is "autoStyleTag": DOM 1`] = `
48
"<!DOCTYPE html><html><head>
59
<title>style-loader test</title>
@@ -24,6 +28,10 @@ exports[`"injectType" option should work when the "injectType" option is "autoSt
2428
2529
exports[`"injectType" option should work when the "injectType" option is "autoStyleTag": warnings 1`] = `Array []`;
2630
31+
exports[`"injectType" option should work when the "injectType" option is "lazyAutoStyleTag" with non DOM environment: errors 1`] = `Array []`;
32+
33+
exports[`"injectType" option should work when the "injectType" option is "lazyAutoStyleTag" with non DOM environment: warnings 1`] = `Array []`;
34+
2735
exports[`"injectType" option should work when the "injectType" option is "lazyAutoStyleTag": DOM 1`] = `
2836
"<!DOCTYPE html><html><head>
2937
<title>style-loader test</title>
@@ -48,6 +56,10 @@ exports[`"injectType" option should work when the "injectType" option is "lazyAu
4856
4957
exports[`"injectType" option should work when the "injectType" option is "lazyAutoStyleTag": warnings 1`] = `Array []`;
5058
59+
exports[`"injectType" option should work when the "injectType" option is "lazySingletonStyleTag" with non DOM environment: errors 1`] = `Array []`;
60+
61+
exports[`"injectType" option should work when the "injectType" option is "lazySingletonStyleTag" with non DOM environment: warnings 1`] = `Array []`;
62+
5163
exports[`"injectType" option should work when the "injectType" option is "lazySingletonStyleTag": DOM 1`] = `
5264
"<!DOCTYPE html><html><head>
5365
<title>style-loader test</title>
@@ -72,6 +84,10 @@ exports[`"injectType" option should work when the "injectType" option is "lazySi
7284
7385
exports[`"injectType" option should work when the "injectType" option is "lazySingletonStyleTag": warnings 1`] = `Array []`;
7486
87+
exports[`"injectType" option should work when the "injectType" option is "lazyStyleTag" with non DOM environment: errors 1`] = `Array []`;
88+
89+
exports[`"injectType" option should work when the "injectType" option is "lazyStyleTag" with non DOM environment: warnings 1`] = `Array []`;
90+
7591
exports[`"injectType" option should work when the "injectType" option is "lazyStyleTag": DOM 1`] = `
7692
"<!DOCTYPE html><html><head>
7793
<title>style-loader test</title>
@@ -96,6 +112,10 @@ exports[`"injectType" option should work when the "injectType" option is "lazySt
96112
97113
exports[`"injectType" option should work when the "injectType" option is "lazyStyleTag": warnings 1`] = `Array []`;
98114
115+
exports[`"injectType" option should work when the "injectType" option is "linkTag" with non DOM environment: errors 1`] = `Array []`;
116+
117+
exports[`"injectType" option should work when the "injectType" option is "linkTag" with non DOM environment: warnings 1`] = `Array []`;
118+
99119
exports[`"injectType" option should work when the "injectType" option is "linkTag": DOM 1`] = `
100120
"<!DOCTYPE html><html><head>
101121
<title>style-loader test</title>
@@ -114,6 +134,10 @@ exports[`"injectType" option should work when the "injectType" option is "linkTa
114134
115135
exports[`"injectType" option should work when the "injectType" option is "linkTag": warnings 1`] = `Array []`;
116136
137+
exports[`"injectType" option should work when the "injectType" option is "singletonStyleTag" with non DOM environment: errors 1`] = `Array []`;
138+
139+
exports[`"injectType" option should work when the "injectType" option is "singletonStyleTag" with non DOM environment: warnings 1`] = `Array []`;
140+
117141
exports[`"injectType" option should work when the "injectType" option is "singletonStyleTag": DOM 1`] = `
118142
"<!DOCTYPE html><html><head>
119143
<title>style-loader test</title>
@@ -138,6 +162,10 @@ exports[`"injectType" option should work when the "injectType" option is "single
138162
139163
exports[`"injectType" option should work when the "injectType" option is "singletonStyleTag": warnings 1`] = `Array []`;
140164
165+
exports[`"injectType" option should work when the "injectType" option is "styleTag" with non DOM environment: errors 1`] = `Array []`;
166+
167+
exports[`"injectType" option should work when the "injectType" option is "styleTag" with non DOM environment: warnings 1`] = `Array []`;
168+
141169
exports[`"injectType" option should work when the "injectType" option is "styleTag": DOM 1`] = `
142170
"<!DOCTYPE html><html><head>
143171
<title>style-loader test</title>

test/injectType-option.test.js

+23
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
/* eslint-env browser */
22

3+
import vm from "vm";
4+
35
import {
46
compile,
57
getCompiler,
68
getEntryByInjectType,
79
getErrors,
810
getWarnings,
11+
readAsset,
912
runInJsDom,
1013
} from "./helpers/index";
1114

@@ -35,5 +38,25 @@ describe('"injectType" option', () => {
3538
expect(getWarnings(stats)).toMatchSnapshot("warnings");
3639
expect(getErrors(stats)).toMatchSnapshot("errors");
3740
});
41+
42+
it(`should work when the "injectType" option is "${injectType}" with non DOM environment`, async () => {
43+
const entry = getEntryByInjectType("simple.js", injectType);
44+
const compiler = getCompiler(entry, { injectType });
45+
const stats = await compile(compiler);
46+
const code = readAsset("main.bundle.js", compiler, stats);
47+
const script = new vm.Script(code);
48+
49+
let errored;
50+
51+
try {
52+
script.runInContext(vm.createContext({ console }));
53+
} catch (error) {
54+
errored = error;
55+
}
56+
57+
expect(errored).toBeUndefined();
58+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
59+
expect(getErrors(stats)).toMatchSnapshot("errors");
60+
});
3861
});
3962
});

0 commit comments

Comments
 (0)