Skip to content

Commit 3cb7dc8

Browse files
authored
refactor!: avoid import.meta polyfill (#3213)
1 parent 9d46b8a commit 3cb7dc8

File tree

6 files changed

+20
-64
lines changed

6 files changed

+20
-64
lines changed

src/build/rollup/base.ts

+3-9
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { rollup as unwasm } from "unwasm/plugin";
1818
import { database } from "./plugins/database";
1919
import { handlers } from "./plugins/handlers";
2020
import { handlersMeta } from "./plugins/handlers-meta";
21-
import { importMeta } from "./plugins/import-meta";
21+
import { serverMain } from "./plugins/server-main";
2222
import { publicAssets } from "./plugins/public-assets";
2323
import { raw } from "./plugins/raw";
2424
import { serverAssets } from "./plugins/server-assets";
@@ -46,8 +46,8 @@ export function baseRollupPlugins(
4646
plugins.push(unwasm(nitro.options.wasm || {}));
4747
}
4848

49-
// Universal import.meta
50-
plugins.push(importMeta(nitro));
49+
// Inject gloalThis.__server_main__
50+
plugins.push(serverMain(nitro));
5151

5252
// Nitro Plugins
5353
const nitroPlugins = [...new Set(nitro.options.plugins)];
@@ -251,12 +251,6 @@ export function baseRollupConfig(nitro: Nitro) {
251251
"globalThis.process.": "process.",
252252
"process.env.RUNTIME_CONFIG": () =>
253253
JSON.stringify(nitro.options.runtimeConfig, null, 2),
254-
...Object.fromEntries(
255-
[".", ";", ")", "[", "]", "}", " "].map((d) => [
256-
`import.meta${d}`,
257-
`globalThis._importMeta_${d}`,
258-
])
259-
),
260254
...Object.fromEntries(
261255
[";", "(", "{", "}", " ", "\t", "\n"].map((d) => [
262256
`${d}global.`,

src/build/rollup/plugins/import-meta.ts

-35
This file was deleted.

src/build/rollup/plugins/public-assets.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ import { fileURLToPath } from 'node:url'
7575
import { resolve, dirname } from 'pathe'
7676
import assets from '#nitro-internal-virtual/public-assets-data'
7777
export function readAsset (id) {
78-
const serverDir = dirname(fileURLToPath(import.meta.url))
78+
const serverDir = dirname(fileURLToPath(globalThis.__nitro_main__))
7979
return fsp.readFile(resolve(serverDir, assets[id].path))
8080
}`;
8181
},
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type { Nitro } from "nitro/types";
2+
import type { Plugin } from "rollup";
3+
4+
export function serverMain(nitro: Nitro): Plugin {
5+
return {
6+
name: "nitro:server-main",
7+
renderChunk(code, chunk) {
8+
if (chunk.isEntry) {
9+
return {
10+
code: `globalThis.__nitro_main__ = import.meta.url; ${code}`,
11+
map: null,
12+
};
13+
}
14+
},
15+
};
16+
}

test/fixture/api/import-meta.ts

-9
This file was deleted.

test/tests.ts

-10
Original file line numberDiff line numberDiff line change
@@ -401,16 +401,6 @@ export function testNitro(
401401
expect(data.json.error).toBe(true);
402402
});
403403

404-
it.skipIf(isWindows && ctx.preset === "nitro-dev")(
405-
"universal import.meta",
406-
async () => {
407-
const { status, data } = await callHandler({ url: "/api/import-meta" });
408-
expect(status).toBe(200);
409-
expect(data.testFile).toMatch(/[/\\]test.txt$/);
410-
expect(data.hasEnv).toBe(true);
411-
}
412-
);
413-
414404
it("handles custom server assets", async () => {
415405
const { data: html, status: htmlStatus } = await callHandler({
416406
url: "/file?filename=index.html",

0 commit comments

Comments
 (0)