|
| 1 | +import fs from 'node:fs' |
| 2 | +import path from 'node:path' |
| 3 | + |
| 4 | +/** |
| 5 | + * Prepack script that prepares the package for publishing by: |
| 6 | + * 1. Creating a modified package.json without dev dependencies, publishConfig and build scripts |
| 7 | + * 2. Updating file paths to remove 'dist/' prefixes (since files will be at root in published package) |
| 8 | + * 3. Writing this modified package.json to the `dist` directory |
| 9 | + * 4. Copying additional files like README.md to the dist directory |
| 10 | + * |
| 11 | + * Type declarations need to be in the package root or corresponding sub-path to support |
| 12 | + * sub-path exports in applications still using `moduleResolution: node`. |
| 13 | + */ |
| 14 | + |
1 | 15 | console.log('Running prepack script')
|
| 16 | + |
| 17 | +/** |
| 18 | + * Files to copy to the dist directory |
| 19 | + * @type {string[]} |
| 20 | + */ |
| 21 | +const FILES_TO_COPY = ['README.md'] |
| 22 | + |
| 23 | +/** |
| 24 | + * Fields to remove from the package.json copy |
| 25 | + * @type {string[]} |
| 26 | + */ |
| 27 | +const FIELDS_TO_REMOVE = [ |
| 28 | + 'devDependencies', |
| 29 | + 'files', |
| 30 | + 'publishConfig', |
| 31 | + 'scripts', |
| 32 | +] |
| 33 | + |
| 34 | +/** |
| 35 | + * Replaces 'dist/' or './dist/' prefix from a file path with './' |
| 36 | + * Only matches at the start of the path to avoid false matches |
| 37 | + * @param {string} filePath - The file path to process |
| 38 | + * @returns {string} The path without dist prefix |
| 39 | + */ |
| 40 | +function replaceDist(filePath) { |
| 41 | + // Only match dist/ at the beginning of the path, followed by a filename |
| 42 | + // This prevents matching strings like "distributed/file.js" or "some/dist/path" |
| 43 | + return filePath.replace(/^(?:\.\/)?dist\/(?=.+)/, './') |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Recursively processes package.json `exports` to remove dist prefixes |
| 48 | + * @param {Record<string, any>} exports - The exports object to process |
| 49 | + * @returns {Record<string, any>} The processed exports object |
| 50 | + */ |
| 51 | +function processExports(exports) { |
| 52 | + return Object.fromEntries( |
| 53 | + Object.entries(exports).map(([key, value]) => [ |
| 54 | + key, |
| 55 | + typeof value === 'string' |
| 56 | + ? replaceDist(value) |
| 57 | + : typeof value === 'object' && value !== null |
| 58 | + ? processExports(value) |
| 59 | + : value, |
| 60 | + ]), |
| 61 | + ) |
| 62 | +} |
| 63 | + |
| 64 | +console.log('Copying modified package.json') |
| 65 | + |
| 66 | +/** @type {Record<string, any>} */ |
| 67 | +const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8')) |
| 68 | + |
| 69 | +const modifiedPackageJson = { ...packageJson } |
| 70 | + |
| 71 | +if (modifiedPackageJson.types) { |
| 72 | + modifiedPackageJson.types = replaceDist(modifiedPackageJson.types) |
| 73 | +} |
| 74 | + |
| 75 | +if (modifiedPackageJson.module) { |
| 76 | + modifiedPackageJson.module = replaceDist(modifiedPackageJson.module) |
| 77 | +} |
| 78 | + |
| 79 | +if (modifiedPackageJson.exports) { |
| 80 | + modifiedPackageJson.exports = processExports(modifiedPackageJson.exports) |
| 81 | +} |
| 82 | + |
| 83 | +for (const field of FIELDS_TO_REMOVE) { |
| 84 | + delete modifiedPackageJson[field] |
| 85 | +} |
| 86 | + |
| 87 | +if (!fs.existsSync('dist')) { |
| 88 | + fs.mkdirSync('dist', { recursive: true }) |
| 89 | +} |
| 90 | + |
| 91 | +fs.writeFileSync( |
| 92 | + path.join('dist', 'package.json'), |
| 93 | + JSON.stringify(modifiedPackageJson, null, 2), |
| 94 | +) |
| 95 | + |
| 96 | +console.log('Copying other files') |
| 97 | +for (const file of FILES_TO_COPY) { |
| 98 | + if (fs.existsSync(file)) { |
| 99 | + fs.copyFileSync(file, path.join('dist', file)) |
| 100 | + console.log(`${file}`) |
| 101 | + } else { |
| 102 | + console.log(`${file} not found, skipping`) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +console.log('prepack complete') |
0 commit comments