|
| 1 | +/* eslint-disable no-console */ |
| 2 | + |
| 3 | +/** |
| 4 | + * This script prepares the central `build` directory for NPM package creation. |
| 5 | + * It first copies all non-code files into the `build` directory, including `package.json`, which |
| 6 | + * is edited to adjust entry point paths. These corrections are performed so that the paths align with |
| 7 | + * the directory structure inside `build`. |
| 8 | + * |
| 9 | + * TODO(v9): Remove this script and change the Deno SDK to import from build/X. |
| 10 | + */ |
| 11 | + |
| 12 | +const fs = require('node:fs'); |
| 13 | +const path = require('node:path'); |
| 14 | + |
| 15 | +const BUILD_DIR = 'build'; |
| 16 | + |
| 17 | +const ENTRY_POINTS = ['main', 'module', 'types', 'browser']; |
| 18 | +const EXPORT_MAP_ENTRY_POINT = 'exports'; |
| 19 | +const TYPES_VERSIONS_ENTRY_POINT = 'typesVersions'; |
| 20 | + |
| 21 | +const PACKAGE_JSON = 'package.json'; |
| 22 | + |
| 23 | +/** |
| 24 | + * @typedef {Record<(typeof ENTRY_POINTS)[number], string>} PackageJsonEntryPoints - an object containing module details |
| 25 | + */ |
| 26 | + |
| 27 | +/** |
| 28 | + * @typedef {Record<string, string>} ConditionalExportEntryPoints - an object containing module details |
| 29 | + */ |
| 30 | + |
| 31 | +/** |
| 32 | + * @typedef {Record<string, Record<string, string[]>>} TypeVersions - an object containing module details |
| 33 | + */ |
| 34 | + |
| 35 | +/** |
| 36 | + * @typedef {Partial<ConditionalExportEntryPoints> & Record<string, Partial<ConditionalExportEntryPoints>>} PackageJsonExports - types for `package.json` exports |
| 37 | + */ |
| 38 | + |
| 39 | +/** |
| 40 | + * @typedef {Record<string, unknown> & PackageJsonEntryPoints & {[EXPORT_MAP_ENTRY_POINT]: PackageJsonExports} & {[TYPES_VERSIONS_ENTRY_POINT]: TypeVersions}} PackageJson - types for `package.json` |
| 41 | + */ |
| 42 | + |
| 43 | +/** |
| 44 | + * @type {PackageJson} |
| 45 | + */ |
| 46 | +const pkgJson = require(path.resolve(PACKAGE_JSON)); |
| 47 | + |
| 48 | +// check if build dir exists |
| 49 | +if (!fs.existsSync(path.resolve(BUILD_DIR))) { |
| 50 | + console.error(`\nERROR: Directory '${BUILD_DIR}' does not exist in ${pkgJson.name}.`); |
| 51 | + console.error("This script should only be executed after you've run `yarn build`."); |
| 52 | + process.exit(1); |
| 53 | +} |
| 54 | + |
| 55 | +// package.json modifications |
| 56 | +/** |
| 57 | + * @type {PackageJson} |
| 58 | + */ |
| 59 | +const newPkgJson = { ...pkgJson }; |
| 60 | + |
| 61 | +// modify entry points to point to correct paths (i.e. strip out the build directory) |
| 62 | +ENTRY_POINTS.filter(entryPoint => newPkgJson[entryPoint]).forEach(entryPoint => { |
| 63 | + newPkgJson[entryPoint] = newPkgJson[entryPoint].replace(`${BUILD_DIR}/`, ''); |
| 64 | +}); |
| 65 | + |
| 66 | +/** |
| 67 | + * Recursively traverses the exports object and rewrites all string values to remove the build directory. |
| 68 | + * |
| 69 | + * @param {PackageJsonExports} exportsObject - the exports object to traverse |
| 70 | + * @param {string} key - the key of the current exports object |
| 71 | + */ |
| 72 | +function rewriteConditionalExportEntryPoint(exportsObject, key) { |
| 73 | + const exportsField = exportsObject[key]; |
| 74 | + if (!exportsField) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + if (typeof exportsField === 'string') { |
| 79 | + exportsObject[key] = exportsField.replace(`${BUILD_DIR}/`, ''); |
| 80 | + return; |
| 81 | + } |
| 82 | + Object.keys(exportsField).forEach(subfieldKey => { |
| 83 | + rewriteConditionalExportEntryPoint(exportsField, subfieldKey); |
| 84 | + }); |
| 85 | +} |
| 86 | + |
| 87 | +if (newPkgJson[EXPORT_MAP_ENTRY_POINT]) { |
| 88 | + Object.keys(newPkgJson[EXPORT_MAP_ENTRY_POINT]).forEach(key => { |
| 89 | + rewriteConditionalExportEntryPoint(newPkgJson[EXPORT_MAP_ENTRY_POINT], key); |
| 90 | + }); |
| 91 | +} |
| 92 | + |
| 93 | +if (newPkgJson[TYPES_VERSIONS_ENTRY_POINT]) { |
| 94 | + Object.entries(newPkgJson[TYPES_VERSIONS_ENTRY_POINT]).forEach(([key, val]) => { |
| 95 | + newPkgJson[TYPES_VERSIONS_ENTRY_POINT][key] = Object.entries(val).reduce((acc, [key, val]) => { |
| 96 | + const newKey = key.replace(`${BUILD_DIR}/`, ''); |
| 97 | + acc[newKey] = val.map(v => v.replace(`${BUILD_DIR}/`, '')); |
| 98 | + return acc; |
| 99 | + }, {}); |
| 100 | + }); |
| 101 | +} |
| 102 | + |
| 103 | +const newPackageJsonPath = path.resolve(BUILD_DIR, PACKAGE_JSON); |
| 104 | + |
| 105 | +// write modified package.json to file (pretty-printed with 2 spaces) |
| 106 | +try { |
| 107 | + fs.writeFileSync(newPackageJsonPath, JSON.stringify(newPkgJson, null, 2)); |
| 108 | +} catch (error) { |
| 109 | + console.error(`\nERROR: Error while writing modified ${PACKAGE_JSON} to disk in ${pkgJson.name}:\n`, error); |
| 110 | + process.exit(1); |
| 111 | +} |
0 commit comments