|
| 1 | +import glob from "glob"; |
| 2 | +import { readFile, writeFile } from "fs/promises"; |
| 3 | +import { drop, map, not, pipe, reduce } from "../src/index"; |
| 4 | + |
| 5 | +const SOURCE_DIR = "./src"; |
| 6 | +const OUTPUT_DIR = "./dist"; |
| 7 | +const CJS_ROOT_DIR = `${OUTPUT_DIR}/cjs`; |
| 8 | +const ESM_ROOT_DIR = `${OUTPUT_DIR}/esm`; |
| 9 | +const ESM5_ROOT_DIR = `${OUTPUT_DIR}/esm5`; |
| 10 | + |
| 11 | +const conditionalRootIndex = { |
| 12 | + import: `${ESM_ROOT_DIR}/index.js`, |
| 13 | + require: `${CJS_ROOT_DIR}/index.js`, |
| 14 | +}; |
| 15 | + |
| 16 | +const conditionalRootIndexLazy = { |
| 17 | + import: `${ESM_ROOT_DIR}/Lazy/index.js`, |
| 18 | + require: `${CJS_ROOT_DIR}/Lazy/index.js`, |
| 19 | +}; |
| 20 | + |
| 21 | +const defaultSubPathExports = { |
| 22 | + "./package.json": "./package.json", |
| 23 | + ".": conditionalRootIndex, |
| 24 | + "./index": conditionalRootIndex, |
| 25 | + "./index.js": conditionalRootIndex, |
| 26 | + "./Lazy": conditionalRootIndexLazy, |
| 27 | + "./Lazy/index": conditionalRootIndexLazy, |
| 28 | + "./Lazy/index.js": conditionalRootIndexLazy, |
| 29 | + "./esm5": `${ESM5_ROOT_DIR}/index.js`, |
| 30 | + "./esm5/index": `${ESM5_ROOT_DIR}/index.js`, |
| 31 | + "./esm5/index.js": `${ESM5_ROOT_DIR}/index.js`, |
| 32 | + "./esm5/Lazy": `${ESM5_ROOT_DIR}/Lazy/index.js`, |
| 33 | + "./esm5/Lazy/index": `${ESM5_ROOT_DIR}/Lazy/index.js`, |
| 34 | + "./esm5/Lazy/index.js": `${ESM5_ROOT_DIR}/Lazy/index.js`, |
| 35 | +}; |
| 36 | + |
| 37 | +const searchFiles = (pattern: string): Promise<string[]> => |
| 38 | + new Promise((resolve, reject) => { |
| 39 | + glob(pattern, function (err, files) { |
| 40 | + if (err) reject(err); |
| 41 | + else resolve(files); |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | +async function generateExports() { |
| 46 | + const fileNames = await Promise.all([ |
| 47 | + searchFiles(`${SOURCE_DIR}/*.ts`), |
| 48 | + searchFiles(`${SOURCE_DIR}/_internal/*.ts`), |
| 49 | + searchFiles(`${SOURCE_DIR}/Lazy/*.ts`), |
| 50 | + ]).then((lists) => |
| 51 | + lists |
| 52 | + .flat() |
| 53 | + .filter((a) => not(a.endsWith("index.ts"))) |
| 54 | + .map((fileName) => |
| 55 | + [...drop(2, fileName.split("/"))].join("/").replace(".ts", ""), |
| 56 | + ), |
| 57 | + ); |
| 58 | + |
| 59 | + const subPathExports = pipe( |
| 60 | + fileNames, |
| 61 | + map((name) => { |
| 62 | + const conditionalSubPaths = { |
| 63 | + import: `${ESM_ROOT_DIR}/${name}.js`, |
| 64 | + require: `${CJS_ROOT_DIR}/${name}.js`, |
| 65 | + }; |
| 66 | + return { |
| 67 | + [`./${name}`]: conditionalSubPaths, |
| 68 | + [`./${name}.js`]: conditionalSubPaths, |
| 69 | + [`./esm5/${name}`]: `${ESM5_ROOT_DIR}/${name}.js`, |
| 70 | + [`./esm5/${name}.js`]: `${ESM5_ROOT_DIR}/${name}.js`, |
| 71 | + }; |
| 72 | + }), |
| 73 | + (iter) => reduce((acc, field) => Object.assign(acc, field), {}, iter), |
| 74 | + ); |
| 75 | + |
| 76 | + const packageJsonObject = JSON.parse( |
| 77 | + String(await readFile("./package.json")), |
| 78 | + ); |
| 79 | + |
| 80 | + await writeFile( |
| 81 | + "./package.json", |
| 82 | + JSON.stringify( |
| 83 | + Object.assign(packageJsonObject, { |
| 84 | + exports: { |
| 85 | + ...defaultSubPathExports, |
| 86 | + ...subPathExports, |
| 87 | + }, |
| 88 | + }), |
| 89 | + ), |
| 90 | + ); |
| 91 | +} |
| 92 | + |
| 93 | +(async function main() { |
| 94 | + await Promise.all([ |
| 95 | + // Add package.json file to cjs directory |
| 96 | + writeFile(`${CJS_ROOT_DIR}/package.json`, '{ "type": "commonjs" }'), |
| 97 | + // Generate and add 'exports' field to root package.json |
| 98 | + generateExports(), |
| 99 | + ]); |
| 100 | +})(); |
0 commit comments