Compare commits

...

3 Commits

Author SHA1 Message Date
Derrick Hammer 0eb71ff289
*Update dist 2023-01-01 01:50:45 -05:00
Derrick Hammer 158331ef8b
*add node-gyp-build-optional-packages 2023-01-01 01:50:29 -05:00
Derrick Hammer 0a73d099f5
*Add support for node-gyp-build-optional-packages
*refactor to have loadNativeModuleTemp require loady
*Allow cjs files
*Add detection for "loadNAPI__default"
*Crawl the package tree to find package.json if needed
*Add heuristic for building moduleName for node-gyp-build
2023-01-01 01:49:59 -05:00
4 changed files with 118 additions and 17 deletions

56
dist/index.js vendored
View File

@ -10,9 +10,13 @@ const magic_string_1 = __importDefault(require("magic-string"));
const loady_1 = __importDefault(require("loady")); const loady_1 = __importDefault(require("loady"));
// @ts-ignore // @ts-ignore
const node_gyp_build_1 = __importDefault(require("node-gyp-build")); const node_gyp_build_1 = __importDefault(require("node-gyp-build"));
// @ts-ignore
const node_gyp_build_optional_packages_1 = __importDefault(require("node-gyp-build-optional-packages"));
const fs_1 = __importDefault(require("fs")); const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path")); const path_1 = __importDefault(require("path"));
const loaderFunction = `function loadNativeModuleTemp(module, data) { const loaderFunction = `
function loadNativeModuleTemp (module, data) {
const loady = require("loady");
const tempDir = require("os").tmpdir(); const tempDir = require("os").tmpdir();
const fs = require("fs"); const fs = require("fs");
const path = require("path"); const path = require("path");
@ -26,6 +30,8 @@ const loaderFunction = `function loadNativeModuleTemp(module, data) {
if (process.pkg) { if (process.pkg) {
process.pkg = undefined; process.pkg = undefined;
} }
loady(module, loadPath);
return loadPath; return loadPath;
}`; }`;
@ -33,7 +39,7 @@ function bundleNativeModulesPlugin() {
return { return {
name: "bundle-native-modules", name: "bundle-native-modules",
transform(src, id, ast) { transform(src, id, ast) {
if (!/\.(js)$/.test(id)) { if (!/\.(c?js)$/.test(id)) {
return null; return null;
} }
const magicString = new magic_string_1.default(src); const magicString = new magic_string_1.default(src);
@ -78,7 +84,7 @@ function bundleNativeModulesPlugin() {
const modulePath = loady_1.default.resolve(match.match.aName, id); const modulePath = loady_1.default.resolve(match.match.aName, id);
const moduleFile = fs_1.default.readFileSync(modulePath); const moduleFile = fs_1.default.readFileSync(modulePath);
const moduleB64 = moduleFile.toString("base64"); const moduleB64 = moduleFile.toString("base64");
magicString.overwrite(match.node.start, match.node.end, `require('loady')('${match.match.aName}', loadNativeModuleTemp('${match.match.aName}', '${moduleB64}'))`); magicString.overwrite(match.node.start, match.node.end, `loadNativeModuleTemp('${match.match.aName}', '${moduleB64}')`);
} }
} }
} }
@ -86,22 +92,56 @@ function bundleNativeModulesPlugin() {
for (const matchString of [ for (const matchString of [
"require('node-gyp-build')(__any)", "require('node-gyp-build')(__any)",
"loadNAPI(__any)", "loadNAPI(__any)",
"loadNAPI__default[__any](__any)",
]) { ]) {
const findNodeBuildGyp = (0, ast_matcher_1.default)(matchString); const findNodeBuildGyp = (0, ast_matcher_1.default)(matchString);
const nodeBuildGypMatches = findNodeBuildGyp(ast); const nodeBuildGypMatches = findNodeBuildGyp(ast);
if (nodeBuildGypMatches?.length) { if (nodeBuildGypMatches?.length) {
for (const match of nodeBuildGypMatches) { for (const match of nodeBuildGypMatches) {
if (markEdited(match.node, edits)) { if (markEdited(match.node, edits)) {
const modulePath = node_gyp_build_1.default.path(path_1.default.dirname(id)); let modulePath;
const moduleName = modulePath try {
modulePath = node_gyp_build_1.default.path(path_1.default.dirname(id));
}
catch { }
if (!modulePath) {
try {
modulePath = node_gyp_build_optional_packages_1.default.path(path_1.default.dirname(id));
}
catch { }
let parentDir = path_1.default.dirname(id);
do {
parentDir = path_1.default.dirname(parentDir);
} while (!fs_1.default.existsSync(path_1.default.join(parentDir, "package.json")) &&
parentDir !== "/");
try {
modulePath = node_gyp_build_optional_packages_1.default.path(parentDir);
}
catch { }
}
if (!modulePath) {
throw new Error(`Could not process native module for ${id}`);
}
let moduleName = "";
for (const part of modulePath
.split("node_modules") .split("node_modules")
.pop() .pop()
.split("/") .split("/")
.slice(1) .slice(1)) {
.shift(); if (part.includes(".node")) {
continue;
}
if (part === "prebuilds") {
break;
}
moduleName += part;
if (part.includes("@")) {
moduleName += "_";
}
}
const moduleFile = fs_1.default.readFileSync(modulePath); const moduleFile = fs_1.default.readFileSync(modulePath);
const moduleB64 = moduleFile.toString("base64"); const moduleB64 = moduleFile.toString("base64");
magicString.overwrite(match.node.start, match.node.end, `require('loady')('${moduleName}', loadNativeModuleTemp('${moduleName}', '${moduleB64}'))`); magicString.overwrite(match.node.start, match.node.end, `loadNativeModuleTemp('${moduleName}', '${moduleB64}')`);
} }
} }
} }

View File

@ -6,7 +6,8 @@
"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" "node-gyp-build": "^4.5.0",
"node-gyp-build-optional-packages": "^5.0.5"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^18.11.14", "@types/node": "^18.11.14",

View File

@ -4,12 +4,16 @@ import MagicString from "magic-string";
import loady from "loady"; import loady from "loady";
// @ts-ignore // @ts-ignore
import nodeGybBuild from "node-gyp-build"; import nodeGybBuild from "node-gyp-build";
// @ts-ignore
import nodeGybBuildOptional from "node-gyp-build-optional-packages";
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"; import path from "path";
const loaderFunction = `function loadNativeModuleTemp(module, data) { const loaderFunction = `
function loadNativeModuleTemp (module, data) {
const loady = require("loady");
const tempDir = require("os").tmpdir(); const tempDir = require("os").tmpdir();
const fs = require("fs"); const fs = require("fs");
const path = require("path"); const path = require("path");
@ -23,6 +27,8 @@ const loaderFunction = `function loadNativeModuleTemp(module, data) {
if (process.pkg) { if (process.pkg) {
process.pkg = undefined; process.pkg = undefined;
} }
loady(module, loadPath);
return loadPath; return loadPath;
}`; }`;
@ -34,7 +40,7 @@ export default function bundleNativeModulesPlugin() {
return { return {
name: "bundle-native-modules", name: "bundle-native-modules",
transform(src, id, ast: any) { transform(src, id, ast: any) {
if (!/\.(js)$/.test(id)) { if (!/\.(c?js)$/.test(id)) {
return null; return null;
} }
const magicString = new MagicString(src); const magicString = new MagicString(src);
@ -93,7 +99,7 @@ export default function bundleNativeModulesPlugin() {
magicString.overwrite( magicString.overwrite(
match.node.start, match.node.start,
match.node.end, match.node.end,
`require('loady')('${match.match.aName}', loadNativeModuleTemp('${match.match.aName}', '${moduleB64}'))` `loadNativeModuleTemp('${match.match.aName}', '${moduleB64}')`
); );
} }
} }
@ -103,6 +109,7 @@ export default function bundleNativeModulesPlugin() {
for (const matchString of [ for (const matchString of [
"require('node-gyp-build')(__any)", "require('node-gyp-build')(__any)",
"loadNAPI(__any)", "loadNAPI(__any)",
"loadNAPI__default[__any](__any)",
]) { ]) {
const findNodeBuildGyp = astMatcher(matchString); const findNodeBuildGyp = astMatcher(matchString);
const nodeBuildGypMatches = findNodeBuildGyp(ast); const nodeBuildGypMatches = findNodeBuildGyp(ast);
@ -110,19 +117,60 @@ export default function bundleNativeModulesPlugin() {
if (nodeBuildGypMatches?.length) { if (nodeBuildGypMatches?.length) {
for (const match of nodeBuildGypMatches) { for (const match of nodeBuildGypMatches) {
if (markEdited(match.node, edits)) { if (markEdited(match.node, edits)) {
const modulePath = nodeGybBuild.path(path.dirname(id)); let modulePath;
const moduleName = modulePath
try {
modulePath = nodeGybBuild.path(path.dirname(id));
} catch {}
if (!modulePath) {
try {
modulePath = nodeGybBuildOptional.path(path.dirname(id));
} catch {}
let parentDir = path.dirname(id);
do {
parentDir = path.dirname(parentDir);
} while (
!fs.existsSync(path.join(parentDir, "package.json")) &&
parentDir !== "/"
);
try {
modulePath = nodeGybBuildOptional.path(parentDir);
} catch {}
}
if (!modulePath) {
throw new Error(`Could not process native module for ${id}`);
}
let moduleName = "";
for (const part of modulePath
.split("node_modules") .split("node_modules")
.pop() .pop()
.split("/") .split("/")
.slice(1) .slice(1)) {
.shift(); if (part.includes(".node")) {
continue;
}
if (part === "prebuilds") {
break;
}
moduleName += part;
if (part.includes("@")) {
moduleName += "_";
}
}
const moduleFile = fs.readFileSync(modulePath); const moduleFile = fs.readFileSync(modulePath);
const moduleB64 = moduleFile.toString("base64"); const moduleB64 = moduleFile.toString("base64");
magicString.overwrite( magicString.overwrite(
match.node.start, match.node.start,
match.node.end, match.node.end,
`require('loady')('${moduleName}', loadNativeModuleTemp('${moduleName}', '${moduleB64}'))` `loadNativeModuleTemp('${moduleName}', '${moduleB64}')`
); );
} }
} }

View File

@ -182,6 +182,7 @@ __metadata:
loady: ^0.0.5 loady: ^0.0.5
magic-string: ^0.27.0 magic-string: ^0.27.0
node-gyp-build: ^4.5.0 node-gyp-build: ^4.5.0
node-gyp-build-optional-packages: ^5.0.5
prettier: ^2.8.1 prettier: ^2.8.1
rollup: ^3.7.4 rollup: ^3.7.4
typescript: ^4.9.4 typescript: ^4.9.4
@ -934,6 +935,17 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"node-gyp-build-optional-packages@npm:^5.0.5":
version: 5.0.5
resolution: "node-gyp-build-optional-packages@npm:5.0.5"
bin:
node-gyp-build-optional-packages: bin.js
node-gyp-build-optional-packages-optional: optional.js
node-gyp-build-optional-packages-test: build-test.js
checksum: d0d1119237479b74c6b3de74fd51e3c9e39a656e5c3d2db7d7b6cdc613a952a926f63fba691066dbb2034524faf63678ff05f79394cc5ce21922815f83532eaf
languageName: node
linkType: hard
"node-gyp-build@npm:^4.5.0": "node-gyp-build@npm:^4.5.0":
version: 4.5.0 version: 4.5.0
resolution: "node-gyp-build@npm:4.5.0" resolution: "node-gyp-build@npm:4.5.0"