|
| 1 | +/** |
| 2 | + * This file resolves extension-less imports to JS files if they exist, preserving sourcemaps. |
| 3 | + * This allows to write extension-less imports in typescript files (which is required to allow transpilation). |
| 4 | + * And still produces correctly resolving transpiled results, which can be used even without bundler. |
| 5 | + * Note that we use inline sourcemaps here, as it is what we configure in tsconfig.json. |
| 6 | + */ |
| 7 | +import fs from 'fs'; |
| 8 | +import path from 'path'; |
| 9 | + |
| 10 | +import {parse, print} from 'recast'; |
| 11 | +import * as typescript from 'recast/parsers/typescript.js'; |
| 12 | + |
| 13 | +/** |
| 14 | + * |
| 15 | + * @param {string} dir |
| 16 | + */ |
| 17 | +async function* walk(dir) { |
| 18 | + for await (const d of await fs.promises.opendir(dir)) { |
| 19 | + const entry = path.join(dir, d.name); |
| 20 | + if (d.isDirectory()) {yield* walk(entry);} |
| 21 | + else if (d.isFile()) {yield entry;} |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +for await (const p of walk('lib')) { |
| 26 | + if (p.endsWith('js')) { |
| 27 | + console.log(p); |
| 28 | + const source = fs.readFileSync(p); |
| 29 | + const sourceMapPath = p + '.map'; |
| 30 | + let inputSourceMap; |
| 31 | + if (fs.existsSync(sourceMapPath)) { |
| 32 | + inputSourceMap = fs.readFileSync(sourceMapPath); |
| 33 | + } |
| 34 | + const ast = parse(source, { |
| 35 | + sourceFileName: p, |
| 36 | + parser: typescript, |
| 37 | + inputSourceMap, |
| 38 | + }); |
| 39 | + rewriteJSEnds(p, ast); |
| 40 | + |
| 41 | + // Remove sourcemaps |
| 42 | + const comments = ast.program.body.at(-1).comments; |
| 43 | + const hasInlineSourceMap = comments && comments[0].value.startsWith('# sourceMappingURL=data:'); |
| 44 | + const hasExternalSourceMap = !hasInlineSourceMap && comments && comments[0].value.startsWith('# sourceMappingURL'); |
| 45 | + if (hasInlineSourceMap || hasExternalSourceMap) { |
| 46 | + ast.program.body.at(-1).comments = undefined; // remove comment |
| 47 | + } |
| 48 | + |
| 49 | + // Output |
| 50 | + const output = print(ast, { |
| 51 | + sourceMapName: sourceMapPath |
| 52 | + }); |
| 53 | + fs.writeFileSync(p, output.code); |
| 54 | + |
| 55 | + // Write sourcemap |
| 56 | + if (hasInlineSourceMap) { |
| 57 | + const inlineSourceMap = ` |
| 58 | +//# sourceMappingURL=data:application/json;base64,${btoa(JSON.stringify(output.map))} |
| 59 | +`; |
| 60 | + fs.appendFileSync(p, inlineSourceMap); |
| 61 | + } else if (hasExternalSourceMap) { |
| 62 | + const externalSourceMap = ` |
| 63 | +//# sourceMappingURL=${p}.map} |
| 64 | +`; |
| 65 | + fs.appendFileSync(p, externalSourceMap); |
| 66 | + fs.writeFileSync(`${p}.map`, JSON.stringify(output.map)); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | + |
| 72 | +/** |
| 73 | + * |
| 74 | + * @param {string} source |
| 75 | + * @return {string} if import rewritten |
| 76 | + */ |
| 77 | +function sourceRewrite(inFile, source) { |
| 78 | + if (!source.startsWith('.')) { |
| 79 | + return; // non relative paths are not in our project |
| 80 | + } |
| 81 | + const newSource = source + '.js'; |
| 82 | + const newsourcePath = path.join(path.dirname(inFile), newSource); |
| 83 | + if (!fs.existsSync(newsourcePath)) { |
| 84 | + return; // if there is no js there that means we should not rewrite it |
| 85 | + } |
| 86 | + return newSource; |
| 87 | + |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * |
| 92 | + * @param {string} p path to the file |
| 93 | + * @param {*} ast |
| 94 | + */ |
| 95 | +function rewriteJSEnds(p, ast) { |
| 96 | + const body = ast.program.body; |
| 97 | + for (const node of body) { |
| 98 | + switch (node.type) { |
| 99 | + case 'ImportDeclaration': |
| 100 | + case 'ExportNamedDeclaration': { |
| 101 | + if (!node.source) { |
| 102 | + continue; |
| 103 | + } |
| 104 | + const source = node.source.value; |
| 105 | + const newSource = sourceRewrite(p, source); |
| 106 | + if (newSource) { |
| 107 | + node.source.value = newSource; |
| 108 | + } |
| 109 | + break; |
| 110 | + } |
| 111 | + default: { |
| 112 | + // pass |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments