11// native
2- import { basename , format , parse , resolve } from 'path' ;
2+ import { promises as fs } from 'fs' ;
3+ import { basename , dirname , format , parse , resolve } from 'path' ;
34
45// packages
56import babelPresetEnv from '@babel/preset-env' ;
67import babelPresetTypescript from '@babel/preset-typescript' ;
78import builtinModules from 'builtin-modules' ;
8- import { outputFile , readFile } from 'fs-extra' ;
9- import {
10- rollup ,
11- watch ,
12- OutputOptions ,
13- InputOptions ,
14- RollupWarning ,
15- RollupWatchOptions ,
16- } from 'rollup' ;
9+ import { rollup , watch } from 'rollup' ;
1710import babel from 'rollup-plugin-babel' ;
1811import commonjs from '@rollup/plugin-commonjs' ;
1912import dts from 'rollup-plugin-dts' ;
@@ -22,11 +15,30 @@ import nodeResolve from '@rollup/plugin-node-resolve';
2215import { terser } from 'rollup-plugin-terser' ;
2316import glob from 'tiny-glob' ;
2417
18+ // types
19+ import type {
20+ OutputOptions ,
21+ InputOptions ,
22+ RollupWarning ,
23+ RollupWatchOptions ,
24+ } from 'rollup' ;
25+
2526const extensions = [ '.ts' , '.js' , '.mjs' ] ;
2627const hashbangRegex = / ^ # ! ( .* ) / ;
2728
29+ async function outputFile ( filepath : string , data : any ) {
30+ // determine the directory
31+ const dir = dirname ( filepath ) ;
32+
33+ // make sure the directory exists
34+ await fs . mkdir ( dir , { recursive : true } ) ;
35+
36+ // write the file
37+ await fs . writeFile ( filepath , data ) ;
38+ }
39+
2840async function getConfig ( cwd : string ) {
29- const pkg = await readFile ( resolve ( cwd , 'package.json' ) , 'utf8' ) ;
41+ const pkg = await fs . readFile ( resolve ( cwd , 'package.json' ) , 'utf8' ) ;
3042
3143 return JSON . parse ( pkg ) ;
3244}
@@ -58,7 +70,7 @@ async function createRollupConfig({
5870
5971 const inputOptions : InputOptions = {
6072 input,
61- external : id => {
73+ external : ( id ) => {
6274 // a special case for when we are importing a local index
6375 if ( withMultipleInputs && id === '.' ) {
6476 return true ;
@@ -92,8 +104,11 @@ async function createRollupConfig({
92104 exclude : 'node_modules/**' ,
93105 extensions,
94106 presets : [
95- [ babelPresetEnv , { targets : { node : nodeTarget } } ] ,
96- babelPresetTypescript ,
107+ [ babelPresetEnv , { bugfixes : true , targets : { node : nodeTarget } } ] ,
108+ [
109+ babelPresetTypescript ,
110+ { allowDeclareFields : true , onlyRemoveTypeImports : true } ,
111+ ] ,
97112 ] ,
98113 } ) ,
99114 compress &&
@@ -224,7 +239,7 @@ export async function bundler({
224239 const entry = inputs [ idx ] ;
225240
226241 const externalDependencies = pkgDependencies . concat (
227- inputs . filter ( e => e !== entry )
242+ inputs . filter ( ( e ) => e !== entry )
228243 ) ;
229244
230245 const options = await createRollupConfig ( {
@@ -256,7 +271,7 @@ export async function bundler({
256271
257272 const watcher = watch ( watchOptions ) ;
258273
259- watcher . on ( 'event' , event => {
274+ watcher . on ( 'event' , ( event ) => {
260275 switch ( event . code ) {
261276 case 'ERROR' :
262277 throw event . error ;
0 commit comments