Skip to content

Commit 4b3c8cd

Browse files
committed
fix array handling, modify defaults
1 parent 3eced57 commit 4b3c8cd

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

index.js

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ export default function ({
1919
fallback,
2020
precompress = false,
2121
minify = false,
22-
injectTo,
23-
targetExtension = '.php',
24-
replace
22+
injectTo = {},
23+
targetExtension = '.html',
24+
replace = []
2525
} = {}) {
2626
return {
2727
name: 'sveltekit-adapter-html-like',
@@ -66,41 +66,43 @@ export default function ({
6666
filesOnly: true
6767
});
6868

69-
await Promise.allSettled(
69+
await Promise.all(
7070
htmlFiles.map(async (htmlFile) => {
7171
const htmlContents = await readFile(htmlFile, 'utf8');
7272
const dom = new JSDOM(htmlContents);
7373

74-
if (injectTo?.length) {
75-
const targetElements = Object.keys(injectTo);
74+
const targetElements = Object.keys(injectTo);
7675

77-
targetElements.map((targetElement) => {
78-
if (!['head', 'body'].includes(targetElement)) {
79-
builder.log.warn(`Skipping unsupported injection element: ${targetElement}`);
76+
targetElements.map((targetElement) => {
77+
if (!['head', 'body'].includes(targetElement)) {
78+
builder.log.warn(`Skipping unsupported injection element: ${targetElement}`);
79+
return;
80+
}
81+
82+
const injectToPositions = Object.keys(injectTo[targetElement]);
83+
84+
injectToPositions.map((injectToPosition) => {
85+
if (
86+
!['beforebegin', 'afterbegin', 'beforeend', 'afterend'].includes(injectToPosition)
87+
) {
88+
builder.log.warn(
89+
`Skipping unsupported insertAdjacentHTML position: ${injectToPosition}`
90+
);
8091
return;
8192
}
8293

83-
const injectToPositions = Object.keys(injectTo[targetElement]);
94+
const injectToPositions = Array.isArray(injectTo[targetElement][injectToPosition])
95+
? injectTo[targetElement][injectToPosition]
96+
: Array(injectTo[targetElement][injectToPosition]);
8497

85-
injectToPositions.map((injectToPosition) => {
86-
if (
87-
!['beforebegin', 'afterbegin', 'beforeend', 'afterend'].includes(injectToPosition)
88-
) {
89-
builder.log.warn(
90-
`Skipping unsupported insertAdjacentHTML position: ${injectToPosition}`
91-
);
92-
return;
93-
}
94-
95-
const injectToText = String(injectTo[targetElement][injectToPosition]);
98+
injectToPositions.map((injectToText) => {
9699
const injectToHash = crypto.createHash('sha256').update(injectToText).digest('hex');
97100
const injectToTag = `<!-- inject:${sessionUUID}:${injectToHash} -->`;
98101

99102
if (!replace.some((item) => item.from === injectToTag)) {
100103
replace.push({
101104
from: injectToTag,
102-
to: injectToText,
103-
many: true
105+
to: injectToText
104106
});
105107
}
106108

@@ -111,7 +113,7 @@ export default function ({
111113
);
112114
});
113115
});
114-
}
116+
});
115117

116118
let outputHTML;
117119

0 commit comments

Comments
 (0)