2022-12-13 10:30:40 +00:00
|
|
|
"use strict";
|
|
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
|
|
};
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2022-12-13 10:35:46 +00:00
|
|
|
exports.bundleNativeModulesPlugin = void 0;
|
2022-12-13 10:30:40 +00:00
|
|
|
const ast_matcher_1 = __importDefault(require("ast-matcher"));
|
|
|
|
const magic_string_1 = __importDefault(require("magic-string"));
|
|
|
|
// @ts-ignore
|
|
|
|
const loady_1 = __importDefault(require("loady"));
|
2022-12-20 18:29:04 +00:00
|
|
|
// @ts-ignore
|
|
|
|
const node_gyp_build_1 = __importDefault(require("node-gyp-build"));
|
2023-01-01 06:50:45 +00:00
|
|
|
// @ts-ignore
|
|
|
|
const node_gyp_build_optional_packages_1 = __importDefault(require("node-gyp-build-optional-packages"));
|
2022-12-13 10:30:40 +00:00
|
|
|
const fs_1 = __importDefault(require("fs"));
|
2022-12-20 18:29:04 +00:00
|
|
|
const path_1 = __importDefault(require("path"));
|
2023-01-01 06:50:45 +00:00
|
|
|
const loaderFunction = `
|
2023-01-01 17:57:50 +00:00
|
|
|
import loady from "loady";
|
2023-01-01 06:50:45 +00:00
|
|
|
function loadNativeModuleTemp (module, data) {
|
2022-12-13 10:30:40 +00:00
|
|
|
const tempDir = require("os").tmpdir();
|
|
|
|
const fs = require("fs");
|
|
|
|
const path = require("path");
|
2022-12-14 18:48:36 +00:00
|
|
|
const loadPath = path.join(tempDir, module);
|
|
|
|
const outputPath = path.join(loadPath, "build", "Release");
|
2022-12-13 10:30:40 +00:00
|
|
|
const modulePath = path.join(outputPath, module + ".node");
|
|
|
|
|
|
|
|
fs.mkdirSync(outputPath, { recursive: true });
|
|
|
|
fs.writeFileSync(modulePath, Buffer.from(data, "base64"));
|
|
|
|
|
2022-12-14 18:48:36 +00:00
|
|
|
if (process.pkg) {
|
|
|
|
process.pkg = undefined;
|
|
|
|
}
|
2023-01-01 06:50:45 +00:00
|
|
|
|
2023-01-01 18:13:09 +00:00
|
|
|
return loady(module, loadPath);
|
2022-12-13 10:30:40 +00:00
|
|
|
}`;
|
|
|
|
function bundleNativeModulesPlugin() {
|
|
|
|
return {
|
|
|
|
name: "bundle-native-modules",
|
|
|
|
transform(src, id, ast) {
|
2023-01-01 06:50:45 +00:00
|
|
|
if (!/\.(c?js)$/.test(id)) {
|
2022-12-13 10:30:40 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const magicString = new magic_string_1.default(src);
|
|
|
|
const parse = (code, source = code) => {
|
|
|
|
try {
|
|
|
|
return this.parse(code, undefined);
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
error.message += ` in ${source}`;
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
ast_matcher_1.default.setParser(parse);
|
|
|
|
if (!ast) {
|
|
|
|
try {
|
|
|
|
ast = parse(src);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
2022-12-14 19:35:00 +00:00
|
|
|
const edits = [];
|
|
|
|
/*
|
|
|
|
Copied from https://github.com/sastan/rollup-plugin-define/blob/main/src/define.ts
|
|
|
|
*/
|
|
|
|
function markEdited(node, edits) {
|
|
|
|
for (const [start, end] of edits) {
|
|
|
|
if ((start <= node.start && node.start < end) ||
|
|
|
|
(start < node.end && node.end <= end)) {
|
|
|
|
return false; // Already edited
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Not edited
|
|
|
|
return edits.push([node.start, node.end]);
|
|
|
|
}
|
2023-01-01 03:45:26 +00:00
|
|
|
for (const matchString of ["require('loady')(__str_aName, __any)"]) {
|
|
|
|
const findLoady = (0, ast_matcher_1.default)(matchString);
|
|
|
|
const loadyMatches = findLoady(ast);
|
|
|
|
if (loadyMatches?.length) {
|
|
|
|
for (const match of loadyMatches) {
|
|
|
|
if (markEdited(match.node, edits)) {
|
|
|
|
const modulePath = loady_1.default.resolve(match.match.aName, id);
|
|
|
|
const moduleFile = fs_1.default.readFileSync(modulePath);
|
|
|
|
const moduleB64 = moduleFile.toString("base64");
|
2023-01-01 06:50:45 +00:00
|
|
|
magicString.overwrite(match.node.start, match.node.end, `loadNativeModuleTemp('${match.match.aName}', '${moduleB64}')`);
|
2023-01-01 03:45:26 +00:00
|
|
|
}
|
2022-12-13 10:30:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-01 03:45:26 +00:00
|
|
|
for (const matchString of [
|
|
|
|
"require('node-gyp-build')(__any)",
|
|
|
|
"loadNAPI(__any)",
|
2023-01-01 06:50:45 +00:00
|
|
|
"loadNAPI__default[__any](__any)",
|
2023-01-01 03:45:26 +00:00
|
|
|
]) {
|
|
|
|
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)) {
|
2023-01-01 06:50:45 +00:00
|
|
|
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
|
2023-01-01 03:45:26 +00:00
|
|
|
.split("node_modules")
|
|
|
|
.pop()
|
|
|
|
.split("/")
|
2023-01-01 06:50:45 +00:00
|
|
|
.slice(1)) {
|
|
|
|
if (part.includes(".node")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (part === "prebuilds") {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
moduleName += part;
|
|
|
|
if (part.includes("@")) {
|
|
|
|
moduleName += "_";
|
|
|
|
}
|
|
|
|
}
|
2023-01-01 03:45:26 +00:00
|
|
|
const moduleFile = fs_1.default.readFileSync(modulePath);
|
|
|
|
const moduleB64 = moduleFile.toString("base64");
|
2023-01-01 06:50:45 +00:00
|
|
|
magicString.overwrite(match.node.start, match.node.end, `loadNativeModuleTemp('${moduleName}', '${moduleB64}')`);
|
2023-01-01 03:45:26 +00:00
|
|
|
}
|
2022-12-20 18:29:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-13 10:30:40 +00:00
|
|
|
if (edits.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const findStrict = (0, ast_matcher_1.default)('"use strict";');
|
|
|
|
const strictMatches = findStrict(ast);
|
|
|
|
let injectNode;
|
|
|
|
if (strictMatches?.length) {
|
|
|
|
injectNode = strictMatches[0].node;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
injectNode = ast.body[0];
|
|
|
|
}
|
|
|
|
magicString.appendRight(injectNode.end + 1, loaderFunction);
|
|
|
|
return {
|
|
|
|
code: magicString.toString(),
|
|
|
|
map: magicString.generateMap({
|
|
|
|
source: src,
|
|
|
|
includeContent: true,
|
|
|
|
hires: true,
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
exports.default = bundleNativeModulesPlugin;
|
2022-12-13 10:35:46 +00:00
|
|
|
exports.bundleNativeModulesPlugin = bundleNativeModulesPlugin;
|
2022-12-13 10:30:40 +00:00
|
|
|
//# sourceMappingURL=index.js.map
|