-
Notifications
You must be signed in to change notification settings - Fork 546
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(deno-server): deno v2 compatibility (#3050)
Co-authored-by: Pooya Parsa <[email protected]>
- Loading branch information
1 parent
547410d
commit 601ade2
Showing
6 changed files
with
160 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import { builtinModules } from "node:module"; | ||
import MagicString from "magic-string"; | ||
import { findStaticImports } from "mlly"; | ||
import { defineNitroPreset } from "nitropack/kit"; | ||
import { writeFile } from "nitropack/kit"; | ||
import { isAbsolute, resolve } from "pathe"; | ||
|
||
// nitro/src/rollup/plugin/import-meta.ts | ||
const ImportMetaRe = /import\.meta|globalThis._importMeta_/; | ||
|
||
export const denoServerLegacy = defineNitroPreset( | ||
{ | ||
extends: "node-server", | ||
entry: "./runtime/deno-server", | ||
exportConditions: ["deno"], | ||
commands: { | ||
preview: "deno task --config ./deno.json start", | ||
}, | ||
unenv: { | ||
inject: { | ||
global: ["unenv/runtime/polyfill/global-this", "default"], | ||
Buffer: ["node:buffer", "Buffer"], | ||
setTimeout: ["node:timers", "setTimeout"], | ||
clearTimeout: ["node:timers", "clearTimeout"], | ||
setInterval: ["node:timers", "setInterval"], | ||
clearInterval: ["node:timers", "clearInterval"], | ||
setImmediate: ["node:timers", "setImmediate"], | ||
clearImmediate: ["node:timers", "clearImmediate"], | ||
// process: ["node:process", "default"], | ||
}, | ||
}, | ||
rollupConfig: { | ||
output: { | ||
hoistTransitiveImports: false, | ||
}, | ||
plugins: [ | ||
{ | ||
name: "rollup-plugin-node-deno", | ||
resolveId(id) { | ||
id = id.replace("node:", ""); | ||
if (builtinModules.includes(id)) { | ||
return { | ||
id: `node:${id}`, | ||
moduleSideEffects: false, | ||
external: true, | ||
}; | ||
} | ||
if (isHTTPImport(id)) { | ||
return { | ||
id, | ||
external: true, | ||
}; | ||
} | ||
}, | ||
renderChunk(code) { | ||
const s = new MagicString(code); | ||
const imports = findStaticImports(code); | ||
for (const i of imports) { | ||
if ( | ||
!i.specifier.startsWith(".") && | ||
!isAbsolute(i.specifier) && | ||
!isHTTPImport(i.specifier) && | ||
!i.specifier.startsWith("npm:") | ||
) { | ||
const specifier = i.specifier.replace("node:", ""); | ||
s.replace( | ||
i.code, | ||
i.code.replace( | ||
new RegExp(`(?<quote>['"])${i.specifier}\\k<quote>`), | ||
JSON.stringify( | ||
builtinModules.includes(specifier) | ||
? "node:" + specifier | ||
: "npm:" + specifier | ||
) | ||
) | ||
); | ||
} | ||
} | ||
if (s.hasChanged()) { | ||
return { | ||
code: s.toString(), | ||
map: s.generateMap({ includeContent: true }), | ||
}; | ||
} | ||
}, | ||
}, | ||
{ | ||
name: "inject-process", | ||
renderChunk: { | ||
order: "post", | ||
handler(code, chunk) { | ||
if ( | ||
!chunk.isEntry && | ||
(!ImportMetaRe.test(code) || code.includes("ROLLUP_NO_REPLACE")) | ||
) { | ||
return; | ||
} | ||
|
||
const s = new MagicString(code); | ||
s.prepend( | ||
"import __process__ from 'node:process';globalThis.process=globalThis.process||__process__;" | ||
); | ||
|
||
return { | ||
code: s.toString(), | ||
map: s.generateMap({ includeContent: true }), | ||
}; | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
hooks: { | ||
async compiled(nitro) { | ||
// https://deno.com/manual@v1.34.3/getting_started/configuration_file | ||
const denoJSON = { | ||
tasks: { | ||
start: | ||
"deno run --allow-net --allow-read --allow-write --allow-env --unstable-byonm ./server/index.mjs", | ||
}, | ||
}; | ||
await writeFile( | ||
resolve(nitro.options.output.dir, "deno.json"), | ||
JSON.stringify(denoJSON, null, 2) | ||
); | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "deno-server-legacy" as const, | ||
aliases: ["deno-server"], | ||
url: import.meta.url, | ||
} | ||
); | ||
|
||
const HTTP_IMPORT_RE = /^(https?:)?\/\//; | ||
|
||
function isHTTPImport(id: string) { | ||
return HTTP_IMPORT_RE.test(id); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters