-
Notifications
You must be signed in to change notification settings - Fork 458
/
import-meta.ts
35 lines (32 loc) · 1.01 KB
/
import-meta.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import type { Plugin } from "rollup";
import { Nitro } from "../../types";
export const ImportMetaRe = /import\.meta|globalThis._importMeta_/;
export function importMeta(nitro: Nitro): Plugin {
return {
name: "import-meta",
renderChunk(code, chunk) {
const isEntry = chunk.isEntry;
if (
!isEntry &&
(!ImportMetaRe.test(code) || code.includes("ROLLUP_NO_REPLACE"))
) {
return;
}
const url =
nitro.options.node && isEntry && !code.includes("ROLLUP_NO_REPLACE")
? "_import_meta_url_"
: '"file:///_entry.js"';
const envImport = nitro.options.node
? "import process from 'node:process';"
: "";
const env = nitro.options.node ? "process.env" : "{}";
const ref = "globalThis._importMeta_";
const stub = `{url:${url},env:${env}}`;
const stubInit = isEntry ? `${ref}=${stub};` : `${ref}=${ref}||${stub};`;
return {
code: envImport + stubInit + code,
map: null,
};
},
};
}