11/* eslint-disable import/first */
2+ import { fileURLToPath } from 'url'
3+
24// @ts -check
35import { build } from 'esbuild'
4- import terser from 'terser'
6+ import { minify as terserMinify } from 'terser'
57import { rollup } from 'rollup'
68import path from 'path'
79import fs from 'fs-extra'
@@ -16,7 +18,9 @@ import { extractInlineSourcemap, removeInlineSourceMap } from './sourcemap'
1618import type { BuildOptions , EntryPointOptions } from './types'
1719import { appendInlineSourceMap , getLocation } from './sourcemap'
1820
19- const sleep = ( ms : number ) => new Promise ( ( resolve ) => setTimeout ( resolve , ms ) )
21+ // No __dirname under Node ESM
22+ const __filename = fileURLToPath ( import . meta. url )
23+ const __dirname = path . dirname ( __filename )
2024
2125const { argv } = yargs ( process . argv )
2226 . option ( 'local' , {
@@ -36,55 +40,55 @@ const buildTargets: BuildOptions[] = [
3640 {
3741 format : 'cjs' ,
3842 name : 'cjs.development' ,
39- target : 'es2018 ' ,
43+ target : 'esnext ' ,
4044 minify : false ,
4145 env : 'development' ,
4246 } ,
4347 {
4448 format : 'cjs' ,
4549 name : 'cjs.production.min' ,
46- target : 'es2018 ' ,
50+ target : 'esnext ' ,
4751 minify : true ,
4852 env : 'production' ,
4953 } ,
5054 // ESM, embedded `process`, ES2017 syntax: modern Webpack dev
5155 {
5256 format : 'esm' ,
5357 name : 'modern' ,
54- target : 'es2018 ' ,
58+ target : 'esnext ' ,
5559 minify : false ,
5660 env : '' ,
5761 } ,
5862 // ESM, pre-compiled "dev", ES2017 syntax: browser development
5963 {
6064 format : 'esm' ,
6165 name : 'modern.development' ,
62- target : 'es2018 ' ,
66+ target : 'esnext ' ,
6367 minify : false ,
6468 env : 'development' ,
6569 } ,
6670 // ESM, pre-compiled "prod", ES2017 syntax: browser prod
6771 {
6872 format : 'esm' ,
6973 name : 'modern.production.min' ,
70- target : 'es2018' ,
71- minify : true ,
72- env : 'production' ,
73- } ,
74- {
75- format : 'umd' ,
76- name : 'umd' ,
77- target : 'es2018' ,
78- minify : false ,
79- env : 'development' ,
80- } ,
81- {
82- format : 'umd' ,
83- name : 'umd.min' ,
84- target : 'es2018' ,
74+ target : 'esnext' ,
8575 minify : true ,
8676 env : 'production' ,
8777 } ,
78+ // {
79+ // format: 'umd',
80+ // name: 'umd',
81+ // target: 'es2018',
82+ // minify: false,
83+ // env: 'development',
84+ // },
85+ // {
86+ // format: 'umd',
87+ // name: 'umd.min',
88+ // target: 'es2018',
89+ // minify: true,
90+ // env: 'production',
91+ // },
8892]
8993
9094const entryPoints : EntryPointOptions [ ] = [
@@ -118,6 +122,9 @@ const esVersionMappings = {
118122 es2018 : ts . ScriptTarget . ES2018 ,
119123 es2019 : ts . ScriptTarget . ES2019 ,
120124 es2020 : ts . ScriptTarget . ES2020 ,
125+ es2021 : ts . ScriptTarget . ES2021 ,
126+ es2022 : ts . ScriptTarget . ES2022 ,
127+ esnext : ts . ScriptTarget . ESNext ,
121128}
122129
123130async function bundle ( options : BuildOptions & EntryPointOptions ) {
@@ -132,10 +139,23 @@ async function bundle(options: BuildOptions & EntryPointOptions) {
132139 entryPoint,
133140 } = options
134141
135- const outputFolder = path . join ( 'dist' , folder )
142+ const folderSegments = [ outputDir , folder ]
143+
144+ if ( format === 'cjs' ) {
145+ folderSegments . push ( 'cjs' )
146+ }
147+
148+ const outputFolder = path . join ( ...folderSegments )
136149 const outputFilename = `${ prefix } .${ name } .js`
150+
151+ await fs . ensureDir ( outputFolder )
152+
137153 const outputFilePath = path . join ( outputFolder , outputFilename )
138154
155+ if ( format === 'cjs' ) {
156+ await writeCommonJSEntry ( outputFolder , prefix )
157+ }
158+
139159 const result = await build ( {
140160 entryPoints : [ entryPoint ] ,
141161 outfile : outputFilePath ,
@@ -212,7 +232,7 @@ async function bundle(options: BuildOptions & EntryPointOptions) {
212232 let mapping : RawSourceMap = mergedSourcemap
213233
214234 if ( minify ) {
215- const transformResult = await terser . minify (
235+ const transformResult = await terserMinify (
216236 appendInlineSourceMap ( code , mapping ) ,
217237 {
218238 sourceMap : {
@@ -237,12 +257,14 @@ async function bundle(options: BuildOptions & EntryPointOptions) {
237257 }
238258
239259 const relativePath = path . relative ( process . cwd ( ) , chunk . path )
240- console . log ( `Build artifact: ${ relativePath } , settings: ` , {
241- target,
242- output : ts . ScriptTarget [ esVersion ] ,
243- } )
244260 await fs . writeFile ( chunk . path , code )
245261 await fs . writeJSON ( chunk . path + '.map' , mapping )
262+
263+ if ( ! chunk . path . includes ( '.map' ) ) {
264+ console . log ( `Build artifact: ${ relativePath } , settings: ` , {
265+ target,
266+ } )
267+ }
246268 }
247269}
248270
@@ -279,16 +301,18 @@ async function buildUMD(
279301}
280302
281303// Generates an index file to handle importing CJS dev/prod
282- async function writeEntry ( folder : string , prefix : string ) {
304+ async function writeCommonJSEntry ( folder : string , prefix : string ) {
283305 await fs . writeFile (
284- path . join ( 'dist' , folder , 'index.js' ) ,
306+ path . join ( folder , 'index.js' ) ,
285307 `'use strict'
286308if (process.env.NODE_ENV === 'production') {
287309 module.exports = require('./${ prefix } .cjs.production.min.js')
288310} else {
289311 module.exports = require('./${ prefix } .cjs.development.js')
290312}`
291313 )
314+
315+ await fs . writeFile ( path . join ( folder , 'package.json' ) , `{"type": "commonjs"}` )
292316}
293317
294318interface BuildArgs {
@@ -313,14 +337,13 @@ async function main({ skipExtraction = false, local = false }: BuildArgs) {
313337 } )
314338 )
315339 await Promise . all ( bundlePromises )
316- await writeEntry ( folder , prefix )
317340 }
318341
319342 // Run UMD builds after everything else so we don't have to sleep after each set
320343 for ( let entryPoint of entryPoints ) {
321344 const { folder } = entryPoint
322345 const outputPath = path . join ( 'dist' , folder )
323- await buildUMD ( outputPath , entryPoint . prefix , entryPoint . globalName )
346+ // await buildUMD(outputPath, entryPoint.prefix, entryPoint.globalName)
324347 }
325348
326349 if ( ! skipExtraction ) {
0 commit comments