-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
1,555 additions
and
1,165 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,20 @@ | ||
import esbuild from "esbuild"; | ||
|
||
const externalizedModules = {}; | ||
|
||
await esbuild.build({ | ||
//inject: ["cjs-shim.ts"], | ||
entryPoints: ["index.ts"], | ||
bundle: true, | ||
sourcemap: "inline", | ||
platform: "node", | ||
target: "node20", | ||
//external: ["fs", "child_process", "crypto", "os", "path"], | ||
//plugins: [externalNativeModulesPlugin(externalizedModules)], | ||
|
||
format: "cjs", | ||
external: ["fs", "child_process", "crypto", "os", "path"], | ||
outfile: "out2.js", | ||
outfile: "out2.cjs", | ||
|
||
//format: "esm", | ||
//outfile: "out2.mjs", | ||
}); |
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,3 @@ | ||
# https://esbuild.github.io/plugins/[esbuild plugin] to handle https://nodejs.org/dist/latest-v16.x/docs/api/addons.html[NodeJS native modules] | ||
|
||
The source code here was shamelessly taken directly from the https://github.com/netlify/zip-it-and-ship-it/tree/v4.23.6/src/runtimes/node/native_modules[Netlify zip-it-and-ship-it repository]. |
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,19 @@ | ||
import { extname } from 'node:path'; | ||
|
||
const markerModules = ['bindings', 'nan', 'node-gyp', 'node-gyp-build', 'node-pre-gyp', 'prebuild'] | ||
|
||
export const isNativeModule = ({ binary, dependencies = {}, devDependencies = {}, files = [], gypfile }) => { | ||
if (binary || gypfile) { | ||
return true | ||
} | ||
|
||
const hasMarkerModule = markerModules.some((marker) => dependencies[marker] || devDependencies[marker]) | ||
|
||
if (hasMarkerModule) { | ||
return true | ||
} | ||
|
||
const hasBinaryFile = files.some((path) => !path.startsWith('!') && extname(path) === '.node') | ||
|
||
return hasBinaryFile | ||
} |
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,67 @@ | ||
import path from 'node:path'; | ||
|
||
import readPackageJson from 'read-package-json-fast'; | ||
|
||
import { isNativeModule } from './detector.js'; | ||
|
||
// Filters out relative or absolute file paths. | ||
const packageFilter = /^([^./]*)$/ | ||
|
||
// Filters valid package names and extracts the base directory. | ||
const packageName = /^([^@][^/]*|@[^/]*\/[^/]+)(?:\/|$)/ | ||
|
||
const findNativeModule = (packageJsonPath, cache) => { | ||
if (cache[packageJsonPath] === undefined) { | ||
cache[packageJsonPath] = readPackageJson(packageJsonPath).then( | ||
(data) => [Boolean(isNativeModule(data), data), data], | ||
() => [], | ||
) | ||
} | ||
|
||
return cache[packageJsonPath] | ||
} | ||
|
||
export const externalNativeModulesPlugin = (externalizedModules) => ({ | ||
name: 'external-native-modules', | ||
setup(build) { | ||
const cache = {} | ||
|
||
build.onResolve({ filter: packageFilter }, async (args) => { | ||
const packageJson = packageName.exec(args.path) | ||
|
||
if (!packageJson) return | ||
|
||
let directory = args.resolveDir | ||
|
||
while (true) { | ||
if (path.basename(directory) !== 'node_modules') { | ||
const modulePath = path.join(directory, 'node_modules', packageJson[1]) | ||
const packageJsonPath = path.join(modulePath, 'package.json') | ||
const [isNative, packageJsonData] = await findNativeModule(packageJsonPath, cache) | ||
|
||
if (isNative === true) { | ||
if (externalizedModules[args.path] === undefined) { | ||
externalizedModules[args.path] = {} | ||
} | ||
|
||
externalizedModules[args.path][modulePath] = packageJsonData.version | ||
|
||
return { path: args.path, external: true } | ||
} | ||
|
||
if (isNative === false) { | ||
return | ||
} | ||
} | ||
|
||
const parentDirectory = path.dirname(directory) | ||
|
||
if (parentDirectory === directory) { | ||
break | ||
} | ||
|
||
directory = parentDirectory | ||
} | ||
}) | ||
}, | ||
}) |
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
Oops, something went wrong.