From 03d3df3c363484c18a1e9a5e468a7600ea1322f3 Mon Sep 17 00:00:00 2001
From: Alexander Akait <4567934+alexander-akait@users.noreply.github.com>
Date: Mon, 13 Mar 2023 23:50:50 +0300
Subject: [PATCH] fix: noop in environment without DOM API (#597)
---
src/runtime/injectStylesIntoLinkTag.js | 4 +++
src/runtime/isOldIE.js | 7 ++++-
src/runtime/singletonStyleDomAPI.js | 6 ++++
src/runtime/styleDomAPI.js | 7 +++++
.../injectType-option.test.js.snap | 28 +++++++++++++++++++
test/injectType-option.test.js | 23 +++++++++++++++
6 files changed, 74 insertions(+), 1 deletion(-)
diff --git a/src/runtime/injectStylesIntoLinkTag.js b/src/runtime/injectStylesIntoLinkTag.js
index 975bba33..198b4c01 100644
--- a/src/runtime/injectStylesIntoLinkTag.js
+++ b/src/runtime/injectStylesIntoLinkTag.js
@@ -1,4 +1,8 @@
module.exports = (url, options) => {
+ if (typeof document === "undefined") {
+ return () => {};
+ }
+
options = options || {};
options.attributes =
typeof options.attributes === "object" ? options.attributes : {};
diff --git a/src/runtime/isOldIE.js b/src/runtime/isOldIE.js
index 72d7d379..d9060b15 100644
--- a/src/runtime/isOldIE.js
+++ b/src/runtime/isOldIE.js
@@ -8,7 +8,12 @@ function isOldIE() {
// Tests for existence of standard globals is to allow style-loader
// to operate correctly into non-standard environments
// @see https://github.com/webpack-contrib/style-loader/issues/177
- memo = Boolean(window && document && document.all && !window.atob);
+ memo = Boolean(
+ typeof window !== "undefined" &&
+ typeof document !== "undefined" &&
+ document.all &&
+ !window.atob
+ );
}
return memo;
diff --git a/src/runtime/singletonStyleDomAPI.js b/src/runtime/singletonStyleDomAPI.js
index 54058a28..41d2e012 100644
--- a/src/runtime/singletonStyleDomAPI.js
+++ b/src/runtime/singletonStyleDomAPI.js
@@ -74,6 +74,12 @@ const singletonData = {
/* istanbul ignore next */
function domAPI(options) {
+ if (typeof document === "undefined")
+ return {
+ update: () => {},
+ remove: () => {},
+ };
+
// eslint-disable-next-line no-undef,no-use-before-define
const styleIndex = singletonData.singletonCounter++;
const styleElement =
diff --git a/src/runtime/styleDomAPI.js b/src/runtime/styleDomAPI.js
index 3cbf7609..dd460ce6 100644
--- a/src/runtime/styleDomAPI.js
+++ b/src/runtime/styleDomAPI.js
@@ -54,6 +54,13 @@ function removeStyleElement(styleElement) {
/* istanbul ignore next */
function domAPI(options) {
+ if (typeof document === "undefined") {
+ return {
+ update: () => {},
+ remove: () => {},
+ };
+ }
+
const styleElement = options.insertStyleElement(options);
return {
diff --git a/test/__snapshots__/injectType-option.test.js.snap b/test/__snapshots__/injectType-option.test.js.snap
index b75876f2..c0a4f851 100644
--- a/test/__snapshots__/injectType-option.test.js.snap
+++ b/test/__snapshots__/injectType-option.test.js.snap
@@ -1,5 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
+exports[`"injectType" option should work when the "injectType" option is "autoStyleTag" with non DOM environment: errors 1`] = `Array []`;
+
+exports[`"injectType" option should work when the "injectType" option is "autoStyleTag" with non DOM environment: warnings 1`] = `Array []`;
+
exports[`"injectType" option should work when the "injectType" option is "autoStyleTag": DOM 1`] = `
"
style-loader test
@@ -24,6 +28,10 @@ exports[`"injectType" option should work when the "injectType" option is "autoSt
exports[`"injectType" option should work when the "injectType" option is "autoStyleTag": warnings 1`] = `Array []`;
+exports[`"injectType" option should work when the "injectType" option is "lazyAutoStyleTag" with non DOM environment: errors 1`] = `Array []`;
+
+exports[`"injectType" option should work when the "injectType" option is "lazyAutoStyleTag" with non DOM environment: warnings 1`] = `Array []`;
+
exports[`"injectType" option should work when the "injectType" option is "lazyAutoStyleTag": DOM 1`] = `
"
style-loader test
@@ -48,6 +56,10 @@ exports[`"injectType" option should work when the "injectType" option is "lazyAu
exports[`"injectType" option should work when the "injectType" option is "lazyAutoStyleTag": warnings 1`] = `Array []`;
+exports[`"injectType" option should work when the "injectType" option is "lazySingletonStyleTag" with non DOM environment: errors 1`] = `Array []`;
+
+exports[`"injectType" option should work when the "injectType" option is "lazySingletonStyleTag" with non DOM environment: warnings 1`] = `Array []`;
+
exports[`"injectType" option should work when the "injectType" option is "lazySingletonStyleTag": DOM 1`] = `
"
style-loader test
@@ -72,6 +84,10 @@ exports[`"injectType" option should work when the "injectType" option is "lazySi
exports[`"injectType" option should work when the "injectType" option is "lazySingletonStyleTag": warnings 1`] = `Array []`;
+exports[`"injectType" option should work when the "injectType" option is "lazyStyleTag" with non DOM environment: errors 1`] = `Array []`;
+
+exports[`"injectType" option should work when the "injectType" option is "lazyStyleTag" with non DOM environment: warnings 1`] = `Array []`;
+
exports[`"injectType" option should work when the "injectType" option is "lazyStyleTag": DOM 1`] = `
"
style-loader test
@@ -96,6 +112,10 @@ exports[`"injectType" option should work when the "injectType" option is "lazySt
exports[`"injectType" option should work when the "injectType" option is "lazyStyleTag": warnings 1`] = `Array []`;
+exports[`"injectType" option should work when the "injectType" option is "linkTag" with non DOM environment: errors 1`] = `Array []`;
+
+exports[`"injectType" option should work when the "injectType" option is "linkTag" with non DOM environment: warnings 1`] = `Array []`;
+
exports[`"injectType" option should work when the "injectType" option is "linkTag": DOM 1`] = `
"
style-loader test
@@ -114,6 +134,10 @@ exports[`"injectType" option should work when the "injectType" option is "linkTa
exports[`"injectType" option should work when the "injectType" option is "linkTag": warnings 1`] = `Array []`;
+exports[`"injectType" option should work when the "injectType" option is "singletonStyleTag" with non DOM environment: errors 1`] = `Array []`;
+
+exports[`"injectType" option should work when the "injectType" option is "singletonStyleTag" with non DOM environment: warnings 1`] = `Array []`;
+
exports[`"injectType" option should work when the "injectType" option is "singletonStyleTag": DOM 1`] = `
"
style-loader test
@@ -138,6 +162,10 @@ exports[`"injectType" option should work when the "injectType" option is "single
exports[`"injectType" option should work when the "injectType" option is "singletonStyleTag": warnings 1`] = `Array []`;
+exports[`"injectType" option should work when the "injectType" option is "styleTag" with non DOM environment: errors 1`] = `Array []`;
+
+exports[`"injectType" option should work when the "injectType" option is "styleTag" with non DOM environment: warnings 1`] = `Array []`;
+
exports[`"injectType" option should work when the "injectType" option is "styleTag": DOM 1`] = `
"
style-loader test
diff --git a/test/injectType-option.test.js b/test/injectType-option.test.js
index 861e9fb9..1deb2b99 100644
--- a/test/injectType-option.test.js
+++ b/test/injectType-option.test.js
@@ -1,11 +1,14 @@
/* eslint-env browser */
+import vm from "vm";
+
import {
compile,
getCompiler,
getEntryByInjectType,
getErrors,
getWarnings,
+ readAsset,
runInJsDom,
} from "./helpers/index";
@@ -35,5 +38,25 @@ describe('"injectType" option', () => {
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
});
+
+ it(`should work when the "injectType" option is "${injectType}" with non DOM environment`, async () => {
+ const entry = getEntryByInjectType("simple.js", injectType);
+ const compiler = getCompiler(entry, { injectType });
+ const stats = await compile(compiler);
+ const code = readAsset("main.bundle.js", compiler, stats);
+ const script = new vm.Script(code);
+
+ let errored;
+
+ try {
+ script.runInContext(vm.createContext({ console }));
+ } catch (error) {
+ errored = error;
+ }
+
+ expect(errored).toBeUndefined();
+ expect(getWarnings(stats)).toMatchSnapshot("warnings");
+ expect(getErrors(stats)).toMatchSnapshot("errors");
+ });
});
});