1- 'use strict' ;
21/* eslint-disable no-console -- output */
3- const { promisify } = require ( 'node:util' ) ;
4- const { mkdir , readFile , unlink , writeFile } = require ( 'node:fs/promises' ) ;
5- const { dirname , join } = require ( 'node:path' ) ;
6- const tmpdir = require ( 'node:os' ) . tmpdir ( ) ;
7- const webpack = promisify ( require ( 'webpack' ) ) ;
8- const compat = require ( '@core-js/compat/compat' ) ;
9- const { banner } = require ( './config' ) ;
2+ import { mkdir , readFile , rm , writeFile } from 'node:fs/promises' ;
3+ import { dirname , join } from 'node:path' ;
4+ import { fileURLToPath } from 'node:url' ;
5+ import { build } from 'rolldown' ;
6+ import { transform } from '@swc/core' ;
7+ import compat from '@core-js/compat/compat.js' ;
8+ import banner from './config.mjs' ;
109
1110function normalizeSummary ( unit = { } ) {
1211 let size , modules ;
@@ -18,7 +17,19 @@ function normalizeSummary(unit = {}) {
1817 } return { size, modules } ;
1918}
2019
21- module . exports = async function ( {
20+ function importIt ( it ) {
21+ return `import 'core-js/modules/${ it } .js';\n` ;
22+ }
23+
24+ function requireIt ( it ) {
25+ return `require('core-js/modules/${ it } ');\n` ;
26+ }
27+
28+ function importModules ( modules , format = 'esm' ) {
29+ return modules . map ( format === 'esm' ? importIt : requireIt ) . join ( '' ) ;
30+ }
31+
32+ export default async function ( {
2233 modules = null ,
2334 exclude = [ ] ,
2435 targets = null ,
@@ -37,33 +48,46 @@ module.exports = async function ({
3748
3849 if ( list . length ) {
3950 if ( format === 'bundle' ) {
40- const tempFileName = `core-js-${ Math . random ( ) . toString ( 36 ) . slice ( 2 ) } .js` ;
41- const tempFile = join ( tmpdir , tempFileName ) ;
42-
43- await webpack ( {
44- mode : 'none' ,
45- node : false ,
46- target : [ 'es5' , 'node' ] ,
47- entry : list . map ( it => require . resolve ( `core-js/modules/${ it } ` ) ) ,
48- output : {
49- filename : tempFileName ,
50- path : tmpdir ,
51- } ,
52- } ) ;
51+ const tempDir = join ( dirname ( fileURLToPath ( import . meta. url ) ) , '__tmp__' ) ;
52+ const tempFile = join ( tempDir , `core-js-${ Math . random ( ) . toString ( 36 ) . slice ( 2 ) } .js` ) ;
53+ const templateFile = `${ tempFile } -template.js` ;
54+
55+ try {
56+ await mkdir ( tempDir , { recursive : true } ) ;
57+ await writeFile ( templateFile , importModules ( list ) ) ;
5358
54- const file = await readFile ( tempFile ) ;
59+ await build ( {
60+ input : templateFile ,
61+ platform : 'neutral' ,
62+ treeshake : false ,
63+ output : {
64+ externalLiveBindings : false ,
65+ format : 'iife' ,
66+ file : tempFile ,
67+ keepNames : true ,
68+ minifyInternalExports : true ,
69+ } ,
70+ } ) ;
5571
56- await unlink ( tempFile ) ;
72+ code = String ( await readFile ( tempFile , 'utf8' ) ) ;
73+ } finally {
74+ await rm ( templateFile , { force : true } ) ;
75+ await rm ( tempFile , { force : true } ) ;
76+ }
5777
58- code = `!function (undefined) { 'use strict'; ${
59- // compress `__webpack_require__` with `keep_fnames` option
60- String ( file ) . replace ( / f u n c t i o n _ _ w e b p a c k _ r e q u i r e _ _ / , 'var __webpack_require__ = function ' )
61- } }();\n`;
78+ // rolldown helpers / wrappers contain arrows
79+ code = ( await transform ( code , {
80+ env : {
81+ include : [
82+ 'transform-arrow-functions' ,
83+ 'transform-shorthand-properties' ,
84+ ] ,
85+ } ,
86+ } ) ) . code ;
87+
88+ code = `!function (undefined) { 'use strict'; ${ code } }();\n` ;
6289 } else {
63- const template = it => format === 'esm'
64- ? `import 'core-js/modules/${ it } .js';\n`
65- : `require('core-js/modules/${ it } ');\n` ;
66- code = list . map ( template ) . join ( '' ) ;
90+ code = importModules ( list , format ) ;
6791 }
6892 }
6993
@@ -90,4 +114,4 @@ module.exports = async function ({
90114 }
91115
92116 return { script } ;
93- } ;
117+ }
0 commit comments