fix: add node shims for ESM target

This commit is contained in:
Amin Yahyaabadi 2024-09-04 00:05:21 -07:00
parent 7547f75b96
commit 050615f245
No known key found for this signature in database
GPG Key ID: F52AF77F636088F0
6 changed files with 763 additions and 111 deletions

View File

@ -62,6 +62,7 @@ words:
- libdw - libdw
- libstdc - libstdc
- libtinfo - libtinfo
- liuli
- memoizee - memoizee
- msbuild - msbuild
- msvc - msvc

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -64,6 +64,7 @@
"@babel/plugin-transform-private-methods": "^7.25.4", "@babel/plugin-transform-private-methods": "^7.25.4",
"@biomejs/biome": "^1.8.3", "@biomejs/biome": "^1.8.3",
"@iarna/toml": "^2.2.5", "@iarna/toml": "^2.2.5",
"@liuli-util/vite-plugin-node": "^0.9.0",
"@octokit/core": "^6.1.2", "@octokit/core": "^6.1.2",
"@octokit/openapi-types": "^22.2.0", "@octokit/openapi-types": "^22.2.0",
"@octokit/plugin-paginate-rest": "^11.3.3", "@octokit/plugin-paginate-rest": "^11.3.3",
@ -102,6 +103,7 @@
"jest": "^29.7.0", "jest": "^29.7.0",
"lefthook": "^1.7.15", "lefthook": "^1.7.15",
"macos-release": "^3.3.0", "macos-release": "^3.3.0",
"magic-string": "^0.30.11",
"memoizee": "^0.4.17", "memoizee": "^0.4.17",
"mkdirp": "^3.0.1", "mkdirp": "^3.0.1",
"mri": "^1.2.0", "mri": "^1.2.0",
@ -118,6 +120,7 @@
"prettier-config-atomic": "^4.0.0", "prettier-config-atomic": "^4.0.0",
"readme-md-generator": "^1.0.0", "readme-md-generator": "^1.0.0",
"retry-as-promised": "^7.0.4", "retry-as-promised": "^7.0.4",
"rollup": "^4.21.2",
"safe-stable-stringify": "^2.5.0", "safe-stable-stringify": "^2.5.0",
"semver": "7.6.3", "semver": "7.6.3",
"setup-apt": "workspace:*", "setup-apt": "workspace:*",

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,38 @@
import module from "module" import module from "module"
import MagicString from "magic-string"
import { type TerserOptions, defineConfig } from "vite" import { type TerserOptions, defineConfig } from "vite"
import type { Plugin } from "vite"
import terserRc from "./.terserrc.mjs" import terserRc from "./.terserrc.mjs"
function shims(): Plugin {
return {
name: "node-shims",
renderChunk(code, chunk) {
if (!chunk.fileName.endsWith(".js")) {
return
}
const s = new MagicString(code)
s.prepend(`
import __path from 'path'
import { fileURLToPath as __fileURLToPath } from 'url'
import { createRequire as __createRequire } from 'module'
const __getFilename = () => __fileURLToPath(import.meta.url)
const __getDirname = () => __path.dirname(__getFilename())
const __dirname = __getDirname()
const __filename = __getFilename()
const self = globalThis
const require = __createRequire(import.meta.url)
`)
return {
code: s.toString(),
map: s.generateMap(),
}
},
apply: "build",
}
}
const viteConfig = defineConfig({ const viteConfig = defineConfig({
build: { build: {
ssr: "./src/setup-cpp.ts", ssr: "./src/setup-cpp.ts",
@ -16,6 +47,7 @@ const viteConfig = defineConfig({
noExternal: true, noExternal: true,
external: module.builtinModules, external: module.builtinModules,
}, },
plugins: [shims()],
}) })
export default viteConfig export default viteConfig