*Add support for node-gyp-build-optional-packages

This commit is contained in:
Derrick Hammer 2022-12-31 22:45:07 -05:00
parent 1f5eb3657e
commit d220d9831e
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 39 additions and 33 deletions

View File

@ -80,48 +80,54 @@ export default function bundleNativeModulesPlugin() {
return edits.push([node.start, node.end]); return edits.push([node.start, node.end]);
} }
const findLoady = astMatcher("require('loady')(__str_aName, __any)"); for (const matchString of ["require('loady')(__str_aName, __any)"]) {
const loadyMatches = findLoady(ast); const findLoady = astMatcher(matchString);
const loadyMatches = findLoady(ast);
if (loadyMatches?.length) { if (loadyMatches?.length) {
for (const match of loadyMatches) { for (const match of loadyMatches) {
if (markEdited(match.node, edits)) { if (markEdited(match.node, edits)) {
const modulePath = loady.resolve(match.match.aName, id); const modulePath = loady.resolve(match.match.aName, id);
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')('${match.match.aName}', loadNativeModuleTemp('${match.match.aName}', '${moduleB64}'))` `require('loady')('${match.match.aName}', loadNativeModuleTemp('${match.match.aName}', '${moduleB64}'))`
); );
}
} }
} }
} }
const findNodeBuildGyp = astMatcher("require('node-gyp-build')(__any)"); for (const matchString of [
const nodeBuildGypMatches = findNodeBuildGyp(ast); "require('node-gyp-build')(__any)",
"loadNAPI(__any)",
]) {
const findNodeBuildGyp = astMatcher(matchString);
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 = nodeGybBuild.path(path.dirname(id)); const modulePath = nodeGybBuild.path(path.dirname(id));
const moduleName = modulePath const moduleName = modulePath
.split("node_modules") .split("node_modules")
.pop() .pop()
.split("/") .split("/")
.slice(1) .slice(1)
.shift(); .shift();
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}'))` `require('loady')('${moduleName}', loadNativeModuleTemp('${moduleName}', '${moduleB64}'))`
); );
}
} }
} }
} }
if (edits.length === 0) { if (edits.length === 0) {
return null; return null;
} }