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,22 @@ async function bundle(options: BuildOptions & EntryPointOptions) {
132139 entryPoint,
133140 } = options
134141
135- const outputFolder = path . join ( 'dist' , folder )
142+ const folderSegments = [ 'dist' , 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+ fs . mkdirs ( outputFolder )
137152 const outputFilePath = path . join ( outputFolder , outputFilename )
138153
154+ if ( format === 'cjs' ) {
155+ await writeCommonJSEntry ( outputFolder , prefix )
156+ }
157+
139158 const result = await build ( {
140159 entryPoints : [ entryPoint ] ,
141160 outfile : outputFilePath ,
@@ -212,7 +231,7 @@ async function bundle(options: BuildOptions & EntryPointOptions) {
212231 let mapping : RawSourceMap = mergedSourcemap
213232
214233 if ( minify ) {
215- const transformResult = await terser . minify (
234+ const transformResult = await terserMinify (
216235 appendInlineSourceMap ( code , mapping ) ,
217236 {
218237 sourceMap : {
@@ -237,12 +256,14 @@ async function bundle(options: BuildOptions & EntryPointOptions) {
237256 }
238257
239258 const relativePath = path . relative ( process . cwd ( ) , chunk . path )
240- console . log ( `Build artifact: ${ relativePath } , settings: ` , {
241- target,
242- output : ts . ScriptTarget [ esVersion ] ,
243- } )
244259 await fs . writeFile ( chunk . path , code )
245260 await fs . writeJSON ( chunk . path + '.map' , mapping )
261+
262+ if ( ! chunk . path . includes ( '.map' ) ) {
263+ console . log ( `Build artifact: ${ relativePath } , settings: ` , {
264+ target,
265+ } )
266+ }
246267 }
247268}
248269
@@ -279,16 +300,18 @@ async function buildUMD(
279300}
280301
281302// Generates an index file to handle importing CJS dev/prod
282- async function writeEntry ( folder : string , prefix : string ) {
303+ async function writeCommonJSEntry ( folder : string , prefix : string ) {
283304 await fs . writeFile (
284- path . join ( 'dist' , folder , 'index.js' ) ,
305+ path . join ( folder , 'index.js' ) ,
285306 `'use strict'
286307if (process.env.NODE_ENV === 'production') {
287308 module.exports = require('./${ prefix } .cjs.production.min.js')
288309} else {
289310 module.exports = require('./${ prefix } .cjs.development.js')
290311}`
291312 )
313+
314+ await fs . writeFile ( path . join ( folder , 'package.json' ) , `{"type": "commonjs"}` )
292315}
293316
294317interface BuildArgs {
@@ -313,14 +336,13 @@ async function main({ skipExtraction = false, local = false }: BuildArgs) {
313336 } )
314337 )
315338 await Promise . all ( bundlePromises )
316- await writeEntry ( folder , prefix )
317339 }
318340
319341 // Run UMD builds after everything else so we don't have to sleep after each set
320342 for ( let entryPoint of entryPoints ) {
321343 const { folder } = entryPoint
322344 const outputPath = path . join ( 'dist' , folder )
323- await buildUMD ( outputPath , entryPoint . prefix , entryPoint . globalName )
345+ // await buildUMD(outputPath, entryPoint.prefix, entryPoint.globalName)
324346 }
325347
326348 if ( ! skipExtraction ) {
0 commit comments