1+ import fs from 'node:fs/promises'
2+ import path from 'node:path'
13import type { Options } from 'tsup'
24import { defineConfig } from 'tsup'
35
6+ async function writeCommonJSEntry ( ) {
7+ await fs . writeFile (
8+ path . join ( 'dist/cjs/' , 'index.js' ) ,
9+ `'use strict'
10+ if (process.env.NODE_ENV === 'production') {
11+ module.exports = require('./reselect.production.min.cjs')
12+ } else {
13+ module.exports = require('./reselect.development.cjs')
14+ }`
15+ )
16+ }
17+
418const tsconfig = 'tsconfig.build.json' satisfies Options [ 'tsconfig' ]
519
620export default defineConfig ( ( options ) : Options [ ] => {
@@ -10,48 +24,83 @@ export default defineConfig((options): Options[] => {
1024 } ,
1125 sourcemap : true ,
1226 tsconfig,
27+ target : [ 'esnext' ] ,
28+ clean : true ,
1329 ...options
1430 }
1531
1632 return [
17- // Modern ESM
1833 {
1934 ...commonOptions ,
35+ name : 'Modern ESM' ,
36+ target : [ 'esnext' ] ,
2037 format : [ 'esm' ] ,
21- outExtension : ( ) => ( { js : '.mjs' } ) ,
22- dts : true ,
23- clean : true
38+ outExtension : ( ) => ( { js : '.mjs' } )
2439 } ,
2540
2641 // Support Webpack 4 by pointing `"module"` to a file with a `.js` extension
2742 // and optional chaining compiled away
2843 {
2944 ...commonOptions ,
45+ name : 'Legacy ESM, Webpack 4' ,
3046 entry : {
3147 'reselect.legacy-esm' : 'src/index.ts'
3248 } ,
3349 format : [ 'esm' ] ,
3450 outExtension : ( ) => ( { js : '.js' } ) ,
35- target : 'es2017'
51+ target : [ 'es2017' ]
3652 } ,
37- // Browser-ready ESM, production + minified
53+
54+ // Meant to be served up via CDNs like `unpkg`.
3855 {
3956 ...commonOptions ,
57+ name : 'Browser-ready ESM' ,
4058 entry : {
4159 'reselect.browser' : 'src/index.ts'
4260 } ,
43- define : {
44- 'process.env.NODE_ENV' : JSON . stringify ( 'production' )
61+ platform : 'browser' ,
62+ env : {
63+ NODE_ENV : 'production'
4564 } ,
4665 format : [ 'esm' ] ,
4766 outExtension : ( ) => ( { js : '.mjs' } ) ,
4867 minify : true
4968 } ,
5069 {
5170 ...commonOptions ,
71+ name : 'CJS Development' ,
72+ entry : {
73+ 'reselect.development' : 'src/index.ts'
74+ } ,
75+ env : {
76+ NODE_ENV : 'development'
77+ } ,
5278 format : [ 'cjs' ] ,
5379 outDir : './dist/cjs/' ,
5480 outExtension : ( ) => ( { js : '.cjs' } )
81+ } ,
82+ {
83+ ...commonOptions ,
84+ name : 'CJS production' ,
85+ entry : {
86+ 'reselect.production.min' : 'src/index.ts'
87+ } ,
88+ env : {
89+ NODE_ENV : 'production'
90+ } ,
91+ format : [ 'cjs' ] ,
92+ outDir : './dist/cjs/' ,
93+ outExtension : ( ) => ( { js : '.cjs' } ) ,
94+ minify : true ,
95+ onSuccess : async ( ) => {
96+ await writeCommonJSEntry ( )
97+ }
98+ } ,
99+ {
100+ ...commonOptions ,
101+ name : 'CJS Type Definitions' ,
102+ format : [ 'cjs' ] ,
103+ dts : { only : true }
55104 }
56105 ]
57106} )
0 commit comments