File size: 2,248 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
import fs from 'node:fs'
import path from 'node:path'
// Currently unused as life-cycle scripts do not run on CI
console.log('Running prepack script')
/**
* Files to copy to the dist directory
* @type {string[]}
*/
const FILES_TO_COPY = ['README.md']
/**
* Fields to remove from the package.json copy
* @type {string[]}
*/
const FIELDS_TO_REMOVE = [
'devDependencies',
'files',
'publishConfig',
'scripts',
]
/**
* Replaces 'dist/' or './dist/' prefix from a file path with './'
* @param {string} filePath - The file path to process
* @returns {string} The path without dist prefix
*/
function removeDist(filePath) {
return filePath.replace(/^(\.\/)?dist\//, './')
}
/**
* Recursively processes exports object to remove dist prefixes
* @param {Record<string, any>} exports - The exports object to process
* @returns {Record<string, any>} The processed exports object
*/
function processExports(exports) {
return Object.fromEntries(
Object.entries(exports).map(([key, value]) => [
key,
typeof value === 'string'
? removeDist(value)
: typeof value === 'object' && value !== null
? processExports(value)
: value,
]),
)
}
console.log('Copying modified package.json')
/** @type {Record<string, any>} */
const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8'))
const distPackageJson = { ...packageJson }
if (distPackageJson.types) {
distPackageJson.types = removeDist(distPackageJson.types)
}
if (distPackageJson.module) {
distPackageJson.module = removeDist(distPackageJson.module)
}
if (distPackageJson.exports) {
distPackageJson.exports = processExports(distPackageJson.exports)
}
for (const field of FIELDS_TO_REMOVE) {
delete distPackageJson[field]
}
if (!fs.existsSync('dist')) {
fs.mkdirSync('dist', { recursive: true })
}
fs.writeFileSync(
path.join('dist', 'package.json'),
JSON.stringify(distPackageJson, null, 2),
)
console.log('Copying other files')
for (const fileName of FILES_TO_COPY) {
if (fs.existsSync(fileName)) {
fs.copyFileSync(fileName, path.join('dist', fileName))
console.log(`${fileName}`)
} else {
console.log(`${fileName} not found, skipping`)
}
}
console.log('prepack complete')
|