Compare commits
No commits in common. "0eb71ff289de35267ae46b47e86168135a2f5c65" and "366ffe1ac9c3a61cc98212f8bb7d9e59b02a16f4" have entirely different histories.
0eb71ff289
...
366ffe1ac9
|
@ -10,13 +10,9 @@ const magic_string_1 = __importDefault(require("magic-string"));
|
|||
const loady_1 = __importDefault(require("loady"));
|
||||
// @ts-ignore
|
||||
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 path_1 = __importDefault(require("path"));
|
||||
const loaderFunction = `
|
||||
function loadNativeModuleTemp (module, data) {
|
||||
const loady = require("loady");
|
||||
const loaderFunction = `function loadNativeModuleTemp(module, data) {
|
||||
const tempDir = require("os").tmpdir();
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
@ -30,8 +26,6 @@ function loadNativeModuleTemp (module, data) {
|
|||
if (process.pkg) {
|
||||
process.pkg = undefined;
|
||||
}
|
||||
|
||||
loady(module, loadPath);
|
||||
|
||||
return loadPath;
|
||||
}`;
|
||||
|
@ -39,7 +33,7 @@ function bundleNativeModulesPlugin() {
|
|||
return {
|
||||
name: "bundle-native-modules",
|
||||
transform(src, id, ast) {
|
||||
if (!/\.(c?js)$/.test(id)) {
|
||||
if (!/\.(js)$/.test(id)) {
|
||||
return null;
|
||||
}
|
||||
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 moduleFile = fs_1.default.readFileSync(modulePath);
|
||||
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 [
|
||||
"require('node-gyp-build')(__any)",
|
||||
"loadNAPI(__any)",
|
||||
"loadNAPI__default[__any](__any)",
|
||||
]) {
|
||||
const findNodeBuildGyp = (0, ast_matcher_1.default)(matchString);
|
||||
const nodeBuildGypMatches = findNodeBuildGyp(ast);
|
||||
if (nodeBuildGypMatches?.length) {
|
||||
for (const match of nodeBuildGypMatches) {
|
||||
if (markEdited(match.node, edits)) {
|
||||
let 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
|
||||
const modulePath = node_gyp_build_1.default.path(path_1.default.dirname(id));
|
||||
const moduleName = modulePath
|
||||
.split("node_modules")
|
||||
.pop()
|
||||
.split("/")
|
||||
.slice(1)) {
|
||||
if (part.includes(".node")) {
|
||||
continue;
|
||||
}
|
||||
if (part === "prebuilds") {
|
||||
break;
|
||||
}
|
||||
moduleName += part;
|
||||
if (part.includes("@")) {
|
||||
moduleName += "_";
|
||||
}
|
||||
}
|
||||
.slice(1)
|
||||
.shift();
|
||||
const moduleFile = fs_1.default.readFileSync(modulePath);
|
||||
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}'))`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,8 +6,7 @@
|
|||
"ast-matcher": "^1.1.1",
|
||||
"loady": "^0.0.5",
|
||||
"magic-string": "^0.27.0",
|
||||
"node-gyp-build": "^4.5.0",
|
||||
"node-gyp-build-optional-packages": "^5.0.5"
|
||||
"node-gyp-build": "^4.5.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^18.11.14",
|
||||
|
|
64
src/index.ts
64
src/index.ts
|
@ -4,16 +4,12 @@ import MagicString from "magic-string";
|
|||
import loady from "loady";
|
||||
// @ts-ignore
|
||||
import nodeGybBuild from "node-gyp-build";
|
||||
// @ts-ignore
|
||||
import nodeGybBuildOptional from "node-gyp-build-optional-packages";
|
||||
import fs from "fs";
|
||||
import type { PluginContext } from "rollup";
|
||||
import type { Plugin } from "vite";
|
||||
import path from "path";
|
||||
|
||||
const loaderFunction = `
|
||||
function loadNativeModuleTemp (module, data) {
|
||||
const loady = require("loady");
|
||||
const loaderFunction = `function loadNativeModuleTemp(module, data) {
|
||||
const tempDir = require("os").tmpdir();
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
@ -27,8 +23,6 @@ function loadNativeModuleTemp (module, data) {
|
|||
if (process.pkg) {
|
||||
process.pkg = undefined;
|
||||
}
|
||||
|
||||
loady(module, loadPath);
|
||||
|
||||
return loadPath;
|
||||
}`;
|
||||
|
@ -40,7 +34,7 @@ export default function bundleNativeModulesPlugin() {
|
|||
return {
|
||||
name: "bundle-native-modules",
|
||||
transform(src, id, ast: any) {
|
||||
if (!/\.(c?js)$/.test(id)) {
|
||||
if (!/\.(js)$/.test(id)) {
|
||||
return null;
|
||||
}
|
||||
const magicString = new MagicString(src);
|
||||
|
@ -99,7 +93,7 @@ export default function bundleNativeModulesPlugin() {
|
|||
magicString.overwrite(
|
||||
match.node.start,
|
||||
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 [
|
||||
"require('node-gyp-build')(__any)",
|
||||
"loadNAPI(__any)",
|
||||
"loadNAPI__default[__any](__any)",
|
||||
]) {
|
||||
const findNodeBuildGyp = astMatcher(matchString);
|
||||
const nodeBuildGypMatches = findNodeBuildGyp(ast);
|
||||
|
@ -117,60 +110,19 @@ export default function bundleNativeModulesPlugin() {
|
|||
if (nodeBuildGypMatches?.length) {
|
||||
for (const match of nodeBuildGypMatches) {
|
||||
if (markEdited(match.node, edits)) {
|
||||
let 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
|
||||
const modulePath = nodeGybBuild.path(path.dirname(id));
|
||||
const moduleName = modulePath
|
||||
.split("node_modules")
|
||||
.pop()
|
||||
.split("/")
|
||||
.slice(1)) {
|
||||
if (part.includes(".node")) {
|
||||
continue;
|
||||
}
|
||||
if (part === "prebuilds") {
|
||||
break;
|
||||
}
|
||||
|
||||
moduleName += part;
|
||||
|
||||
if (part.includes("@")) {
|
||||
moduleName += "_";
|
||||
}
|
||||
}
|
||||
|
||||
.slice(1)
|
||||
.shift();
|
||||
const moduleFile = fs.readFileSync(modulePath);
|
||||
const moduleB64 = moduleFile.toString("base64");
|
||||
magicString.overwrite(
|
||||
match.node.start,
|
||||
match.node.end,
|
||||
`loadNativeModuleTemp('${moduleName}', '${moduleB64}')`
|
||||
`require('loady')('${moduleName}', loadNativeModuleTemp('${moduleName}', '${moduleB64}'))`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
12
yarn.lock
12
yarn.lock
|
@ -182,7 +182,6 @@ __metadata:
|
|||
loady: ^0.0.5
|
||||
magic-string: ^0.27.0
|
||||
node-gyp-build: ^4.5.0
|
||||
node-gyp-build-optional-packages: ^5.0.5
|
||||
prettier: ^2.8.1
|
||||
rollup: ^3.7.4
|
||||
typescript: ^4.9.4
|
||||
|
@ -935,17 +934,6 @@ __metadata:
|
|||
languageName: node
|
||||
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":
|
||||
version: 4.5.0
|
||||
resolution: "node-gyp-build@npm:4.5.0"
|
||||
|
|
Loading…
Reference in New Issue