Open History to see the last commit
1import fs from 'node:fs'
2import 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β
15console.log('Running prepack script')
16β
17/**
18 * Files to copy to the dist directory
19 * @type {string[]}
20 */
21const FILES_TO_COPY = ['README.md']
22β
23/**
24 * Fields to remove from the package.json copy
25 * @type {string[]}
26 */
27const 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 */
40function 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 */
51function 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β
64console.log('Copying modified package.json')
65β
66/** @type {Record<string, any>} */
67const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8'))
68β
69const modifiedPackageJson = { ...packageJson }
70β
71if (modifiedPackageJson.types) {
72 modifiedPackageJson.types = replaceDist(modifiedPackageJson.types)
73}
74β
75if (modifiedPackageJson.module) {
76 modifiedPackageJson.module = replaceDist(modifiedPackageJson.module)
77}
78β
79if (modifiedPackageJson.exports) {
80 modifiedPackageJson.exports = processExports(modifiedPackageJson.exports)
81}
82β
83for (const field of FIELDS_TO_REMOVE) {
84 delete modifiedPackageJson[field]
85}
86β
87if (!fs.existsSync('dist')) {
88 fs.mkdirSync('dist', { recursive: true })
89}
90β
91fs.writeFileSync(
92 path.join('dist', 'package.json'),
93 JSON.stringify(modifiedPackageJson, null, 2),
94)
95β
96console.log('Copying other files')
97for (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β
106console.log('prepack complete')