为native环境注入了OAK_PLATFORM环境变量的处理

This commit is contained in:
Xu Chang 2023-11-27 17:08:35 +08:00
parent 4b21beaa55
commit a6034dae1b
2 changed files with 40 additions and 2 deletions

View File

@ -0,0 +1,37 @@
const t = require('@babel/types');
const assert = require('assert');
const REPLACE_HOLDERS = {
OAK_PLATFORM: 'native',
};
function replaceEnvExpressionPlugin() {
return {
visitor: {
MemberExpression(path, state) {
const { node } = path;
// process.env.XXXXX
if (
t.isMemberExpression(node) &&
t.isMemberExpression(node.object) &&
t.isIdentifier(node.object.object) &&
node.object.object.name === 'process' &&
t.isIdentifier(node.object.property) &&
node.object.property.name === 'env'
) {
const { property } = node;
assert(t.isIdentifier(property));
const { name } = property;
if (REPLACE_HOLDERS[name]) {
console.log(state.filename, name);
path.replaceWith(
t.stringLiteral(REPLACE_HOLDERS[name])
);
}
}
}
}
};
}
module.exports = replaceEnvExpressionPlugin;

View File

@ -18,6 +18,7 @@ const less = require('less');
const css2rn = require("css-to-react-native-transform").default;
const { parseSync, transformFromAstSync, transformSync: babelTransform } = require("@babel/core");
const nullthrows = require("nullthrows");
const replaceEnvExpressionPlugin = require('./babelEnvPlugin');
const { injectGetRender } = require('../utils/injectGetRender');
@ -49,7 +50,7 @@ function transform({ filename, options, plugins, src }) {
cwd: options.projectRoot,
highlightCode: true,
filename,
plugins,
plugins: plugins.concat(replaceEnvExpressionPlugin),
sourceType: "module",
// NOTE(EvanBacon): We split the parse/transform steps up to accommodate
// Hermes parsing, but this defaults to cloning the AST which increases
@ -81,7 +82,7 @@ function transform({ filename, options, plugins, src }) {
}
})
}
// injectGetRender(transformResult.ast.program.body, path.resolve(process.cwd(), '..'), filename, 'native');
return {
ast: nullthrows(transformResult.ast),
metadata: transformResult.metadata,