react-native-builder-bob
Version:
CLI to build JavaScript files for React Native libraries
58 lines • 2.34 kB
JavaScript
import path from 'node:path';
import { isCodegenSpec } from "./utils/isCodegenSpec.js";
import { resolveModuleSpecifier, SOURCE_EXTENSIONS, } from "./utils/resolveModuleSpecifier.js";
const isTypeImport = (node) => ('importKind' in node && node.importKind === 'type') ||
('exportKind' in node && node.exportKind === 'type');
const assertFilename = (filename) => {
if (filename == null) {
throw new Error("Couldn't find a filename for the current file.");
}
};
export default function (api, { extension, platforms }) {
api.assertVersion(7);
const codegenEnabled = api.caller((caller) => caller?.codegenEnabled);
const toExtensions = (sources) => extension == null
? []
: sources.map((source) => ({ source, output: extension }));
const rewriteExtensions = toExtensions(extension ? [...SOURCE_EXTENSIONS, extension] : SOURCE_EXTENSIONS);
const explicitRewriteExtensions = toExtensions(['ts', 'tsx']);
function addExtension({ node, }, state) {
if (extension == null ||
// Skip type imports as they'll be removed
isTypeImport(node) ||
// Skip non-relative imports
!node.source?.value.startsWith('.')) {
return;
}
assertFilename(state.filename);
const filename = path.resolve(path.dirname(state.filename), node.source.value);
// Skip imports for codegen spec if codegen is enabled
if (codegenEnabled &&
(isCodegenSpec(filename) ||
SOURCE_EXTENSIONS.some((ext) => isCodegenSpec(`${filename}.${ext}`)))) {
return;
}
node.source.value = resolveModuleSpecifier({
filepath: state.filename,
specifier: node.source.value,
extensions: rewriteExtensions,
explicitExtensions: explicitRewriteExtensions,
platforms,
});
}
return {
name: 'react-native-builder-bob',
visitor: {
ImportDeclaration(path, state) {
addExtension(path, state);
},
ExportNamedDeclaration(path, state) {
addExtension(path, state);
},
ExportAllDeclaration(path, state) {
addExtension(path, state);
},
},
};
}
//# sourceMappingURL=babel.js.map