reboost
Version:
A super fast dev server for rapid web development
129 lines (128 loc) • 6.96 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformCommonJS = void 0;
const estree_toolkit_1 = require("estree-toolkit");
const module_1 = require("module");
const getReboostResolveCall = (source) => estree_toolkit_1.builders.callExpression(estree_toolkit_1.builders.identifier('__reboost_resolve'), [estree_toolkit_1.builders.literal(source)]);
const transformCommonJS = (programPath, filePath, id) => {
let usedModuleExports;
let usedExports;
let modImports;
let importIdentifierMap;
const importerIdentifier = estree_toolkit_1.builders.identifier(`importer_${id}`);
let usedImporter;
let replacements;
let importIdx = 0;
programPath.traverse({
Program(path) {
if (path.scope.hasGlobalBinding('module'))
usedModuleExports = true;
if (path.scope.hasGlobalBinding('exports'))
usedExports = true;
},
ImportDeclaration(path) {
if (path.node.specifiers.length === 0)
return;
const declarators = [];
const identifier = estree_toolkit_1.builders.identifier(`import_${importIdx++}_${id}`);
usedImporter = true;
path.node.specifiers.forEach((specifier) => {
let usage;
let importedName;
const localName = specifier.local.name;
const commons = [
getReboostResolveCall(path.node.source.value),
estree_toolkit_1.builders.literal(filePath)
];
if (estree_toolkit_1.is.importDefaultSpecifier(specifier)) {
usage = 'Default';
}
else if (estree_toolkit_1.is.importNamespaceSpecifier(specifier)) {
usage = 'All';
}
else if (estree_toolkit_1.is.importSpecifier(specifier)) {
usage = 'Member';
importedName = specifier.imported.name;
}
declarators.push(estree_toolkit_1.builders.variableDeclarator(estree_toolkit_1.builders.identifier(localName), estree_toolkit_1.builders.callExpression(estree_toolkit_1.builders.memberExpression(importerIdentifier, estree_toolkit_1.builders.identifier(usage)), importedName ? [
identifier,
estree_toolkit_1.builders.literal(importedName),
...commons
] : [identifier, ...commons])));
});
(replacements || (replacements = [])).push([
path,
estree_toolkit_1.builders.importDeclaration([
estree_toolkit_1.builders.importNamespaceSpecifier(identifier)
], estree_toolkit_1.builders.literal(path.node.source.value)),
declarators
]);
},
CallExpression(path) {
if (estree_toolkit_1.is.identifier(path.node.callee, { name: 'require' }) &&
path.node.arguments.length === 1 &&
estree_toolkit_1.is.literal(path.node.arguments[0], { value: (v) => typeof v === 'string' }) &&
!path.scope.hasBinding('require')) {
const importPath = path.node.arguments[0].value;
const importIdentifier = (importIdentifierMap || (importIdentifierMap = {}))[importPath] ||
estree_toolkit_1.builders.identifier(`imported_${importIdx++}_${id}`);
// Don't resolve built-in modules like path, fs, etc.
if (module_1.builtinModules.includes(importPath))
return;
if (!(importPath in importIdentifierMap)) {
importIdentifierMap[importPath] = importIdentifier;
(modImports || (modImports = [])).push(estree_toolkit_1.builders.importDeclaration([estree_toolkit_1.builders.importNamespaceSpecifier(importIdentifier)], estree_toolkit_1.builders.literal(importPath)));
}
usedImporter = true;
path.replaceWith(estree_toolkit_1.builders.callExpression(estree_toolkit_1.builders.memberExpression(importerIdentifier, estree_toolkit_1.builders.identifier('All')), [
importIdentifier,
getReboostResolveCall(importPath),
estree_toolkit_1.builders.literal(filePath)
]));
}
}
});
if (usedModuleExports || usedExports) {
if (usedModuleExports) {
programPath.unshiftContainer('body', [
estree_toolkit_1.builders.variableDeclaration('const', [
estree_toolkit_1.builders.variableDeclarator(estree_toolkit_1.builders.identifier('module'), estree_toolkit_1.builders.objectExpression([
estree_toolkit_1.builders.property('init', estree_toolkit_1.builders.identifier('exports'), usedExports ? estree_toolkit_1.builders.identifier('exports') : estree_toolkit_1.builders.objectExpression([]), false, usedExports)
])),
])
]);
}
if (usedExports) {
programPath.unshiftContainer('body', [
estree_toolkit_1.builders.variableDeclaration('const', [
estree_toolkit_1.builders.variableDeclarator(estree_toolkit_1.builders.identifier('exports'), estree_toolkit_1.builders.objectExpression([]))
])
]);
}
programPath.pushContainer('body', [
estree_toolkit_1.builders.exportNamedDeclaration(estree_toolkit_1.builders.variableDeclaration('var', [
estree_toolkit_1.builders.variableDeclarator(estree_toolkit_1.builders.identifier('__cjsExports'), usedModuleExports
? estree_toolkit_1.builders.memberExpression(estree_toolkit_1.builders.identifier('module'), estree_toolkit_1.builders.identifier('exports'))
: estree_toolkit_1.builders.identifier('exports'))
]))
]);
}
if (modImports)
programPath.unshiftContainer('body', modImports);
if (usedImporter) {
if (replacements) {
replacements.forEach(([path, replacement, declarators]) => {
if (declarators.length) {
path.insertAfter([estree_toolkit_1.builders.variableDeclaration('const', declarators)]);
}
path.replaceWith(replacement);
});
}
programPath.unshiftContainer('body', [
estree_toolkit_1.builders.importDeclaration([
estree_toolkit_1.builders.importDefaultSpecifier(importerIdentifier)
], estree_toolkit_1.builders.literal('#/importer'))
]);
}
};
exports.transformCommonJS = transformCommonJS;