Compare commits
No commits in common. "1f5eb3657e6699067003be2e8ff146aef0115355" and "8ea326514be198e99584e21b6da9668045bac4d4" have entirely different histories.
1f5eb3657e
...
8ea326514b
|
@ -8,10 +8,7 @@ const ast_matcher_1 = __importDefault(require("ast-matcher"));
|
|||
const magic_string_1 = __importDefault(require("magic-string"));
|
||||
// @ts-ignore
|
||||
const loady_1 = __importDefault(require("loady"));
|
||||
// @ts-ignore
|
||||
const node_gyp_build_1 = __importDefault(require("node-gyp-build"));
|
||||
const fs_1 = __importDefault(require("fs"));
|
||||
const path_1 = __importDefault(require("path"));
|
||||
const loaderFunction = `function loadNativeModuleTemp(module, data) {
|
||||
const tempDir = require("os").tmpdir();
|
||||
const fs = require("fs");
|
||||
|
@ -36,6 +33,9 @@ function bundleNativeModulesPlugin() {
|
|||
if (!/\.(js)$/.test(id)) {
|
||||
return null;
|
||||
}
|
||||
if (!/binding/.test(src)) {
|
||||
return null;
|
||||
}
|
||||
const magicString = new magic_string_1.default(src);
|
||||
const parse = (code, source = code) => {
|
||||
try {
|
||||
|
@ -81,24 +81,6 @@ function bundleNativeModulesPlugin() {
|
|||
}
|
||||
}
|
||||
}
|
||||
const findNodeBuildGyp = (0, ast_matcher_1.default)("require('node-gyp-build')(__any)");
|
||||
const nodeBuildGypMatches = findNodeBuildGyp(ast);
|
||||
if (nodeBuildGypMatches?.length) {
|
||||
for (const match of nodeBuildGypMatches) {
|
||||
if (markEdited(match.node, edits)) {
|
||||
const modulePath = node_gyp_build_1.default.path(path_1.default.dirname(id));
|
||||
const moduleName = modulePath
|
||||
.split("node_modules")
|
||||
.pop()
|
||||
.split("/")
|
||||
.slice(1)
|
||||
.shift();
|
||||
const moduleFile = fs_1.default.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) {
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -5,14 +5,12 @@
|
|||
"dependencies": {
|
||||
"ast-matcher": "^1.1.1",
|
||||
"loady": "^0.0.5",
|
||||
"magic-string": "^0.27.0",
|
||||
"node-gyp-build": "^4.5.0"
|
||||
"magic-string": "^0.27.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^18.11.14",
|
||||
"prettier": "^2.8.1",
|
||||
"rollup": "^3.7.4",
|
||||
"typescript": "^4.9.4",
|
||||
"vite": "^4.0.1"
|
||||
}
|
||||
}
|
||||
|
|
31
src/index.ts
31
src/index.ts
|
@ -2,12 +2,9 @@ import astMatcher from "ast-matcher";
|
|||
import MagicString from "magic-string";
|
||||
// @ts-ignore
|
||||
import loady from "loady";
|
||||
// @ts-ignore
|
||||
import nodeGybBuild from "node-gyp-build";
|
||||
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 tempDir = require("os").tmpdir();
|
||||
|
@ -37,6 +34,10 @@ export default function bundleNativeModulesPlugin() {
|
|||
if (!/\.(js)$/.test(id)) {
|
||||
return null;
|
||||
}
|
||||
if (!/binding/.test(src)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const magicString = new MagicString(src);
|
||||
|
||||
const parse = (
|
||||
|
@ -98,30 +99,6 @@ 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) {
|
||||
return null;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue