UNPKG

bob-the-bundler

Version:
34 lines (33 loc) 1.29 kB
import path from 'path'; import fse from 'fs-extra'; function isFolderSync(path) { try { return fse.statSync(path).isDirectory(); } catch (_a) { return false; } } function rewriteSourceValue(sourceValue, relativeDirname) { if (sourceValue.startsWith('.') && !sourceValue.endsWith('.js')) { const targetPath = path.resolve(relativeDirname, sourceValue); // If the target path is a folder, we need to import from the index.js file if (isFolderSync(targetPath)) { sourceValue += '/index'; } sourceValue += '.js'; } return sourceValue; } /** * Rewrite import and export statements to append the correct .js file extension when needed */ export function rewriteCodeImports(fileContents, absoluteFilePath) { const relativeDirname = path.dirname(absoluteFilePath); return fileContents.replace( /* this regex should hopefully catch all kind of import/export expressions that are relative. */ /((?:import|export)\s+[\s\w,{}*]*\s+from\s+["'])((?:\.\/|\.\.\/)(?:(?!\.js).)+)(["'])/g, (_, importFromPart, modulePath, hyphenEndPart) => { const sourcePath = rewriteSourceValue(modulePath, relativeDirname); return `${importFromPart}${sourcePath}${hyphenEndPart}`; }); }