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: noop in environment without DOM API #597

Merged
merged 1 commit into from
Mar 13, 2023
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
4 changes: 4 additions & 0 deletions src/runtime/injectStylesIntoLinkTag.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
module.exports = (url, options) => {
if (typeof document === "undefined") {
return () => {};
}

options = options || {};
options.attributes =
typeof options.attributes === "object" ? options.attributes : {};
Expand Down
7 changes: 6 additions & 1 deletion src/runtime/isOldIE.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions src/runtime/singletonStyleDomAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
7 changes: 7 additions & 0 deletions src/runtime/styleDomAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
28 changes: 28 additions & 0 deletions test/__snapshots__/injectType-option.test.js.snap
Original file line number Diff line number Diff line change
@@ -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`] = `
"<!DOCTYPE html><html><head>
<title>style-loader test</title>
Expand All @@ -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`] = `
"<!DOCTYPE html><html><head>
<title>style-loader test</title>
Expand All @@ -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`] = `
"<!DOCTYPE html><html><head>
<title>style-loader test</title>
Expand All @@ -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`] = `
"<!DOCTYPE html><html><head>
<title>style-loader test</title>
Expand All @@ -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`] = `
"<!DOCTYPE html><html><head>
<title>style-loader test</title>
Expand All @@ -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`] = `
"<!DOCTYPE html><html><head>
<title>style-loader test</title>
Expand All @@ -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`] = `
"<!DOCTYPE html><html><head>
<title>style-loader test</title>
Expand Down
23 changes: 23 additions & 0 deletions test/injectType-option.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
/* eslint-env browser */

import vm from "vm";

import {
compile,
getCompiler,
getEntryByInjectType,
getErrors,
getWarnings,
readAsset,
runInJsDom,
} from "./helpers/index";

Expand Down Expand Up @@ -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");
});
});
});