Compare commits

..

No commits in common. "0eb71ff289de35267ae46b47e86168135a2f5c65" and "366ffe1ac9c3a61cc98212f8bb7d9e59b02a16f4" have entirely different histories.

4 changed files with 17 additions and 118 deletions

56
dist/index.js vendored
View File

@ -10,13 +10,9 @@ 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 = ` const loaderFunction = `function loadNativeModuleTemp(module, data) {
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");
@ -30,8 +26,6 @@ function loadNativeModuleTemp (module, data) {
if (process.pkg) { if (process.pkg) {
process.pkg = undefined; process.pkg = undefined;
} }
loady(module, loadPath);
return loadPath; return loadPath;
}`; }`;
@ -39,7 +33,7 @@ function bundleNativeModulesPlugin() {
return { return {
name: "bundle-native-modules", name: "bundle-native-modules",
transform(src, id, ast) { transform(src, id, ast) {
if (!/\.(c?js)$/.test(id)) { if (!/\.(js)$/.test(id)) {
return null; return null;
} }
const magicString = new magic_string_1.default(src); const magicString = new magic_string_1.default(src);
@ -84,7 +78,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, `loadNativeModuleTemp('${match.match.aName}', '${moduleB64}')`); magicString.overwrite(match.node.start, match.node.end, `require('loady')('${match.match.aName}', loadNativeModuleTemp('${match.match.aName}', '${moduleB64}'))`);
} }
} }
} }
@ -92,56 +86,22 @@ 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)) {
let modulePath; const modulePath = node_gyp_build_1.default.path(path_1.default.dirname(id));
try { const moduleName = modulePath
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)
if (part.includes(".node")) { .shift();
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, `loadNativeModuleTemp('${moduleName}', '${moduleB64}')`); magicString.overwrite(match.node.start, match.node.end, `require('loady')('${moduleName}', loadNativeModuleTemp('${moduleName}', '${moduleB64}'))`);
} }
} }
} }

View File

@ -6,8 +6,7 @@
"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,16 +4,12 @@ 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 = ` const loaderFunction = `function loadNativeModuleTemp(module, data) {
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");
@ -27,8 +23,6 @@ function loadNativeModuleTemp (module, data) {
if (process.pkg) { if (process.pkg) {
process.pkg = undefined; process.pkg = undefined;
} }
loady(module, loadPath);
return loadPath; return loadPath;
}`; }`;
@ -40,7 +34,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 (!/\.(c?js)$/.test(id)) { if (!/\.(js)$/.test(id)) {
return null; return null;
} }
const magicString = new MagicString(src); const magicString = new MagicString(src);
@ -99,7 +93,7 @@ export default function bundleNativeModulesPlugin() {
magicString.overwrite( magicString.overwrite(
match.node.start, match.node.start,
match.node.end, match.node.end,
`loadNativeModuleTemp('${match.match.aName}', '${moduleB64}')` `require('loady')('${match.match.aName}', loadNativeModuleTemp('${match.match.aName}', '${moduleB64}'))`
); );
} }
} }
@ -109,7 +103,6 @@ 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);
@ -117,60 +110,19 @@ 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)) {
let modulePath; const modulePath = nodeGybBuild.path(path.dirname(id));
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)
if (part.includes(".node")) { .shift();
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,
`loadNativeModuleTemp('${moduleName}', '${moduleB64}')` `require('loady')('${moduleName}', loadNativeModuleTemp('${moduleName}', '${moduleB64}'))`
); );
} }
} }

View File

@ -182,7 +182,6 @@ __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
@ -935,17 +934,6 @@ __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"