*Add support for node-gyp-build

This commit is contained in:
Derrick Hammer 2022-12-20 13:28:49 -05:00
parent 8ea326514b
commit 49c07dda52
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
3 changed files with 1374 additions and 224 deletions

View File

@ -5,12 +5,14 @@
"dependencies": { "dependencies": {
"ast-matcher": "^1.1.1", "ast-matcher": "^1.1.1",
"loady": "^0.0.5", "loady": "^0.0.5",
"magic-string": "^0.27.0" "magic-string": "^0.27.0",
"node-gyp-build": "^4.5.0"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^18.11.14", "@types/node": "^18.11.14",
"prettier": "^2.8.1", "prettier": "^2.8.1",
"rollup": "^3.7.4", "rollup": "^3.7.4",
"typescript": "^4.9.4",
"vite": "^4.0.1" "vite": "^4.0.1"
} }
} }

View File

@ -2,9 +2,12 @@ import astMatcher from "ast-matcher";
import MagicString from "magic-string"; import MagicString from "magic-string";
// @ts-ignore // @ts-ignore
import loady from "loady"; import loady from "loady";
// @ts-ignore
import nodeGybBuild from "node-gyp-build";
import fs from "fs"; import fs from "fs";
import type { PluginContext } from "rollup"; import type { PluginContext } from "rollup";
import type { Plugin } from "vite"; import type { Plugin } from "vite";
import path from "path";
const loaderFunction = `function loadNativeModuleTemp(module, data) { const loaderFunction = `function loadNativeModuleTemp(module, data) {
const tempDir = require("os").tmpdir(); const tempDir = require("os").tmpdir();
@ -34,10 +37,6 @@ export default function bundleNativeModulesPlugin() {
if (!/\.(js)$/.test(id)) { if (!/\.(js)$/.test(id)) {
return null; return null;
} }
if (!/binding/.test(src)) {
return null;
}
const magicString = new MagicString(src); const magicString = new MagicString(src);
const parse = ( const parse = (
@ -99,6 +98,30 @@ export default function bundleNativeModulesPlugin() {
} }
} }
const findNodeBuildGyp = astMatcher("require('node-gyp-build')(__any)");
const nodeBuildGypMatches = findNodeBuildGyp(ast);
if (nodeBuildGypMatches?.length) {
for (const match of nodeBuildGypMatches) {
if (markEdited(match.node, edits)) {
const modulePath = nodeGybBuild.path(path.dirname(id));
const moduleName = modulePath
.split("node_modules")
.pop()
.split("/")
.slice(1)
.shift();
const moduleFile = fs.readFileSync(modulePath);
const moduleB64 = moduleFile.toString("base64");
magicString.overwrite(
match.node.start,
match.node.end,
`require('loady')('${moduleName}', loadNativeModuleTemp('${moduleName}', '${moduleB64}'))`
);
}
}
}
if (edits.length === 0) { if (edits.length === 0) {
return null; return null;
} }

1563
yarn.lock

File diff suppressed because it is too large Load Diff