File size: 3,966 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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
import type { Options } from 'tsup'
import fs from 'fs'
import path from 'path'
interface BuildOptions {
format: 'cjs' | 'esm'
name:
| 'development'
| 'production.min'
| 'legacy-esm'
| 'modern'
| 'modern.development'
| 'modern.production.min'
minify: boolean
env: 'development' | 'production' | ''
target?:
| 'es2017'
| 'es2018'
| 'es2019'
| 'es2020'
| 'es2021'
| 'es2022'
| 'esnext'
dts?: boolean
}
const buildTargets: BuildOptions[] = [
{
format: 'cjs',
name: 'development',
target: 'es2020',
minify: false,
env: 'development',
dts: true,
},
{
format: 'cjs',
name: 'production.min',
target: 'es2020',
minify: true,
env: 'production',
},
// ESM, embedded `process`: modern Webpack dev
{
format: 'esm',
name: 'modern',
target: 'es2020',
minify: false,
env: '',
dts: true,
},
// ESM, embedded `process`: fallback for Webpack 4,
// which doesn't support `exports` field or optional chaining
{
format: 'esm',
name: 'legacy-esm',
target: 'es2020',
minify: false,
env: '',
},
// ESM, pre-compiled "dev": browser development
{
format: 'esm',
name: 'modern.development',
target: 'es2020',
minify: false,
env: 'development',
},
// ESM, pre-compiled "prod": browser prod
{
format: 'esm',
name: 'modern.production.min',
target: 'es2020',
minify: true,
env: 'production',
},
]
function writeCommonJSEntry(folder: string, prefix: string) {
fs.writeFileSync(
path.join(folder, 'index.js'),
`'use strict'
if (process.env.NODE_ENV === 'production') {
module.exports = require('./${prefix}.production.min.cjs')
} else {
module.exports = require('./${prefix}.development.cjs')
}`
)
}
interface ConfigOptions {
name: string
entry: string
buildFilter?: Array<BuildOptions['name']>
}
export const defaultConfig = (
{ name: prefix, entry, buildFilter }: ConfigOptions,
options: Options
): Options[] => {
const artifactOptions: Options[] = buildTargets
.filter(target => !buildFilter || buildFilter.includes(target.name))
.map(({ format, minify, env, name, target, dts }) => {
const outputFilename = `${prefix}.${name}`
const folderSegments = ['dist']
if (format === 'cjs') {
folderSegments.push('cjs')
}
const outputFolder = path.join(...folderSegments)
const extension =
name === 'legacy-esm' ? '.js' : format === 'esm' ? '.mjs' : '.cjs'
const defineValues: Record<string, string> = {}
if (env) {
Object.assign(defineValues, {
'process.env.NODE_ENV': JSON.stringify(env),
})
}
return {
entry: {
[outputFilename]: entry,
},
dts,
format,
outDir: outputFolder,
target,
outExtension: () => ({ js: extension }),
minify,
sourcemap: false,
clean: !options.watch,
external: [
'react',
'react-dom',
'react-native',
'@react-three/fiber',
'three',
'react-konva',
'konva',
'react-zdog',
'zdog',
],
esbuildOptions(options, context) {
// Prevent compiling ES while context is set to CJS
if (context.format !== 'cjs') {
// Needed to prevent auto-replacing of process.env.NODE_ENV in all builds
options.platform = 'neutral'
// Needed to return to normal lookup behavior when platform: 'neutral'
options.mainFields = ['browser', 'module', 'main']
options.conditions = ['browser']
}
},
define: defineValues,
async onSuccess() {
if (format === 'cjs' && name === 'production.min') {
writeCommonJSEntry(outputFolder, prefix)
}
},
}
})
return artifactOptions
}
|