Skip to content

Commit

Permalink
test(v6): reproduce custom environment preload injection
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Apr 27, 2024
1 parent a8adcac commit ffe4dd1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
20 changes: 20 additions & 0 deletions packages/vite/src/node/__tests__/build.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,26 @@ describe('resolveBuildOutputs', () => {
],
})
})

test('custom environment ssr', async () => {
const builder = await createBuilder({
root: resolve(__dirname, 'fixtures/dynamic-import'),
environments: {
custom: {
build: {
ssr: true,
rollupOptions: {
input: {
index: '/entry',
},
},
},
},
},
})
const result = await builder.build(builder.environments.custom)
expect((result as RollupOutput).output[0].code).not.toContain('preload')

Check failure on line 658 in packages/vite/src/node/__tests__/build.spec.ts

View workflow job for this annotation

GitHub Actions / Build&Test: node-18, ubuntu-latest

packages/vite/src/node/__tests__/build.spec.ts > resolveBuildOutputs > custom environment ssr

AssertionError: expected 'const scriptRel = "modulepreload";\nc…' not to contain 'preload' - Expected + Received - preload + const scriptRel = "modulepreload"; + const assetsURL = function(dep) { + return "/" + dep; + }; + const seen = {}; + const __vitePreload = function preload(baseModule, deps, importerUrl) { + let promise = Promise.resolve(); + if (deps && deps.length > 0) { + const links = document.getElementsByTagName("link"); + const cspNonceMeta = document.querySelector( + "meta[property=csp-nonce]" + ); + const cspNonce = (cspNonceMeta == null ? void 0 : cspNonceMeta.nonce) || (cspNonceMeta == null ? void 0 : cspNonceMeta.getAttribute("nonce")); + promise = Promise.all( + deps.map((dep) => { + dep = assetsURL(dep); + if (dep in seen) + return; + seen[dep] = true; + const isCss = dep.endsWith(".css"); + const cssSelector = isCss ? '[rel="stylesheet"]' : ""; + const isBaseRelative = !!importerUrl; + if (isBaseRelative) { + for (let i = links.length - 1; i >= 0; i--) { + const link2 = links[i]; + if (link2.href === dep && (!isCss || link2.rel === "stylesheet")) { + return; + } + } + } else if (document.querySelector(`link[href="${dep}"]${cssSelector}`)) { + return; + } + const link = document.createElement("link"); + link.rel = isCss ? "stylesheet" : scriptRel; + if (!isCss) { + link.as = "script"; + link.crossOrigin = ""; + } + link.href = dep; + if (cspNonce) { + link.setAttribute("nonce", cspNonce); + } + document.head.appendChild(link); + if (isCss) { + return new Promise((res, rej) => { + link.addEventListener("load", res); + link.addEventListener( + "error", + () => rej(new Error(`Unable to preload CSS for ${dep}`)) + ); + }); + } + }) + ); + } + return promise.then(() => baseModule()).catch((err) => { + const e = new Event("vite:preloadError", { cancelable: true }); + e.payload = err; + window.dispatchEvent(e); + if (!e.defaultPrevented) { + throw err; + } + }); + }; + async function main() { + const mod = await __vitePreload(() => import("./assets/dep-BSaPgEGk.mjs"), true ? [] : void 0); + console.log(mod); + } + export { + main + }; + ❯ packages/vite/src/node/__tests__/build.spec.ts:658:57

Check failure on line 658 in packages/vite/src/node/__tests__/build.spec.ts

View workflow job for this annotation

GitHub Actions / Build&Test: node-20, ubuntu-latest

packages/vite/src/node/__tests__/build.spec.ts > resolveBuildOutputs > custom environment ssr

AssertionError: expected 'const scriptRel = "modulepreload";\nc…' not to contain 'preload' - Expected + Received - preload + const scriptRel = "modulepreload"; + const assetsURL = function(dep) { + return "/" + dep; + }; + const seen = {}; + const __vitePreload = function preload(baseModule, deps, importerUrl) { + let promise = Promise.resolve(); + if (deps && deps.length > 0) { + const links = document.getElementsByTagName("link"); + const cspNonceMeta = document.querySelector( + "meta[property=csp-nonce]" + ); + const cspNonce = (cspNonceMeta == null ? void 0 : cspNonceMeta.nonce) || (cspNonceMeta == null ? void 0 : cspNonceMeta.getAttribute("nonce")); + promise = Promise.all( + deps.map((dep) => { + dep = assetsURL(dep); + if (dep in seen) + return; + seen[dep] = true; + const isCss = dep.endsWith(".css"); + const cssSelector = isCss ? '[rel="stylesheet"]' : ""; + const isBaseRelative = !!importerUrl; + if (isBaseRelative) { + for (let i = links.length - 1; i >= 0; i--) { + const link2 = links[i]; + if (link2.href === dep && (!isCss || link2.rel === "stylesheet")) { + return; + } + } + } else if (document.querySelector(`link[href="${dep}"]${cssSelector}`)) { + return; + } + const link = document.createElement("link"); + link.rel = isCss ? "stylesheet" : scriptRel; + if (!isCss) { + link.as = "script"; + link.crossOrigin = ""; + } + link.href = dep; + if (cspNonce) { + link.setAttribute("nonce", cspNonce); + } + document.head.appendChild(link); + if (isCss) { + return new Promise((res, rej) => { + link.addEventListener("load", res); + link.addEventListener( + "error", + () => rej(new Error(`Unable to preload CSS for ${dep}`)) + ); + }); + } + }) + ); + } + return promise.then(() => baseModule()).catch((err) => { + const e = new Event("vite:preloadError", { cancelable: true }); + e.payload = err; + window.dispatchEvent(e); + if (!e.defaultPrevented) { + throw err; + } + }); + }; + async function main() { + const mod = await __vitePreload(() => import("./assets/dep-BSaPgEGk.mjs"), true ? [] : void 0); + console.log(mod); + } + export { + main + }; + ❯ packages/vite/src/node/__tests__/build.spec.ts:658:57

Check failure on line 658 in packages/vite/src/node/__tests__/build.spec.ts

View workflow job for this annotation

GitHub Actions / Build&Test: node-20, macos-latest

packages/vite/src/node/__tests__/build.spec.ts > resolveBuildOutputs > custom environment ssr

AssertionError: expected 'const scriptRel = "modulepreload";\nc…' not to contain 'preload' - Expected + Received - preload + const scriptRel = "modulepreload"; + const assetsURL = function(dep) { + return "/" + dep; + }; + const seen = {}; + const __vitePreload = function preload(baseModule, deps, importerUrl) { + let promise = Promise.resolve(); + if (deps && deps.length > 0) { + const links = document.getElementsByTagName("link"); + const cspNonceMeta = document.querySelector( + "meta[property=csp-nonce]" + ); + const cspNonce = (cspNonceMeta == null ? void 0 : cspNonceMeta.nonce) || (cspNonceMeta == null ? void 0 : cspNonceMeta.getAttribute("nonce")); + promise = Promise.all( + deps.map((dep) => { + dep = assetsURL(dep); + if (dep in seen) + return; + seen[dep] = true; + const isCss = dep.endsWith(".css"); + const cssSelector = isCss ? '[rel="stylesheet"]' : ""; + const isBaseRelative = !!importerUrl; + if (isBaseRelative) { + for (let i = links.length - 1; i >= 0; i--) { + const link2 = links[i]; + if (link2.href === dep && (!isCss || link2.rel === "stylesheet")) { + return; + } + } + } else if (document.querySelector(`link[href="${dep}"]${cssSelector}`)) { + return; + } + const link = document.createElement("link"); + link.rel = isCss ? "stylesheet" : scriptRel; + if (!isCss) { + link.as = "script"; + link.crossOrigin = ""; + } + link.href = dep; + if (cspNonce) { + link.setAttribute("nonce", cspNonce); + } + document.head.appendChild(link); + if (isCss) { + return new Promise((res, rej) => { + link.addEventListener("load", res); + link.addEventListener( + "error", + () => rej(new Error(`Unable to preload CSS for ${dep}`)) + ); + }); + } + }) + ); + } + return promise.then(() => baseModule()).catch((err) => { + const e = new Event("vite:preloadError", { cancelable: true }); + e.payload = err; + window.dispatchEvent(e); + if (!e.defaultPrevented) { + throw err; + } + }); + }; + async function main() { + const mod = await __vitePreload(() => import("./assets/dep-BSaPgEGk.mjs"), true ? [] : void 0); + console.log(mod); + } + export { + main + }; + ❯ packages/vite/src/node/__tests__/build.spec.ts:658:57

Check failure on line 658 in packages/vite/src/node/__tests__/build.spec.ts

View workflow job for this annotation

GitHub Actions / Build&Test: node-20, windows-latest

packages/vite/src/node/__tests__/build.spec.ts > resolveBuildOutputs > custom environment ssr

AssertionError: expected 'const scriptRel = "modulepreload";\nc…' not to contain 'preload' - Expected + Received - preload + const scriptRel = "modulepreload"; + const assetsURL = function(dep) { + return "/" + dep; + }; + const seen = {}; + const __vitePreload = function preload(baseModule, deps, importerUrl) { + let promise = Promise.resolve(); + if (deps && deps.length > 0) { + const links = document.getElementsByTagName("link"); + const cspNonceMeta = document.querySelector( + "meta[property=csp-nonce]" + ); + const cspNonce = (cspNonceMeta == null ? void 0 : cspNonceMeta.nonce) || (cspNonceMeta == null ? void 0 : cspNonceMeta.getAttribute("nonce")); + promise = Promise.all( + deps.map((dep) => { + dep = assetsURL(dep); + if (dep in seen) + return; + seen[dep] = true; + const isCss = dep.endsWith(".css"); + const cssSelector = isCss ? '[rel="stylesheet"]' : ""; + const isBaseRelative = !!importerUrl; + if (isBaseRelative) { + for (let i = links.length - 1; i >= 0; i--) { + const link2 = links[i]; + if (link2.href === dep && (!isCss || link2.rel === "stylesheet")) { + return; + } + } + } else if (document.querySelector(`link[href="${dep}"]${cssSelector}`)) { + return; + } + const link = document.createElement("link"); + link.rel = isCss ? "stylesheet" : scriptRel; + if (!isCss) { + link.as = "script"; + link.crossOrigin = ""; + } + link.href = dep; + if (cspNonce) { + link.setAttribute("nonce", cspNonce); + } + document.head.appendChild(link); + if (isCss) { + return new Promise((res, rej) => { + link.addEventListener("load", res); + link.addEventListener( + "error", + () => rej(new Error(`Unable to preload CSS for ${dep}`)) + ); + }); + } + }) + ); + } + return promise.then(() => baseModule()).catch((err) => { + const e = new Event("vite:preloadError", { cancelable: true }); + e.payload = err; + window.dispatchEvent(e); + if (!e.defaultPrevented) { + throw err; + } + }); + }; + async function main() { + const mod = await __vitePreload(() => import("./assets/dep-BSaPgEGk.mjs"), true ? [] : void 0); + console.log(mod); + } + export { + main + }; + ❯ packages/vite/src/node/__tests__/build.spec.ts:658:57
})
})

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const hello = 'hello'
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export async function main() {
const mod = await import('./dep.mjs')
console.log(mod)
}

0 comments on commit ffe4dd1

Please sign in to comment.