UNPKG

next-turbopack-nodenext

Version:

Adds module: NodeNext support to Turbopack in Next.js

45 lines (44 loc) 2.22 kB
import fs from 'node:fs'; import path from 'node:path'; import fg from 'fast-glob'; export default function withNodeNext(config) { var _a, _b, _c, _d, _e; const extensionAlias = ((_b = (_a = config.experimental) === null || _a === void 0 ? void 0 : _a.extensionAlias) === null || _b === void 0 ? void 0 : _b['.js']) || [ '.ts', '.tsx', '.js', '.jsx', ]; const resolveAlias = fg .globSync(['app/**/*.{ts,tsx}', 'pages/**/*.{ts,tsx}', 'src/**/*.{ts,tsx}']) .reduce((acc, file) => { // For each file, read contents and look for import statements ending with "".js" const contents = fs.readFileSync(file, 'utf8'); const imports = contents.match(/import\s+.*\s+from\s+['"].*\.js['"]/g); if (!imports) { return acc; } // For each import statement, add an alias for the .js file to the .ts or .tsx file for (const imp of imports) { const match = imp.match(/import\s+.*\s+from\s+['"](.*\.js)['"]/); if (!match) { continue; } const [, matchedPath] = match; if (!matchedPath) { continue; } for (const extensionAliasItem of extensionAlias) { const extPath = matchedPath.replace(/\.js$/, extensionAliasItem); const extPathRelativeToFile = path.resolve(path.dirname(file), extPath); const extExists = fs.existsSync(extPathRelativeToFile); if (extExists) { acc[matchedPath] = extPathRelativeToFile.replace(process.cwd(), '.'); break; } } } return acc; }, {}); return Object.assign(Object.assign({}, config), { experimental: Object.assign(Object.assign({}, (config.experimental || {})), { turbo: Object.assign(Object.assign({}, (((_c = config.experimental) === null || _c === void 0 ? void 0 : _c.turbo) || {})), { resolveAlias: Object.assign(Object.assign({}, (((_e = (_d = config.experimental) === null || _d === void 0 ? void 0 : _d.turbo) === null || _e === void 0 ? void 0 : _e.resolveAlias) || {})), resolveAlias) }) }) }); }