1
1
// 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' ;
3
4
4
5
// packages
5
6
import babelPresetEnv from '@babel/preset-env' ;
6
7
import babelPresetTypescript from '@babel/preset-typescript' ;
7
8
import 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' ;
17
10
import babel from 'rollup-plugin-babel' ;
18
11
import commonjs from '@rollup/plugin-commonjs' ;
19
12
import dts from 'rollup-plugin-dts' ;
@@ -22,11 +15,30 @@ import nodeResolve from '@rollup/plugin-node-resolve';
22
15
import { terser } from 'rollup-plugin-terser' ;
23
16
import glob from 'tiny-glob' ;
24
17
18
+ // types
19
+ import type {
20
+ OutputOptions ,
21
+ InputOptions ,
22
+ RollupWarning ,
23
+ RollupWatchOptions ,
24
+ } from 'rollup' ;
25
+
25
26
const extensions = [ '.ts' , '.js' , '.mjs' ] ;
26
27
const hashbangRegex = / ^ # ! ( .* ) / ;
27
28
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
+
28
40
async 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' ) ;
30
42
31
43
return JSON . parse ( pkg ) ;
32
44
}
@@ -58,7 +70,7 @@ async function createRollupConfig({
58
70
59
71
const inputOptions : InputOptions = {
60
72
input,
61
- external : id => {
73
+ external : ( id ) => {
62
74
// a special case for when we are importing a local index
63
75
if ( withMultipleInputs && id === '.' ) {
64
76
return true ;
@@ -92,8 +104,11 @@ async function createRollupConfig({
92
104
exclude : 'node_modules/**' ,
93
105
extensions,
94
106
presets : [
95
- [ babelPresetEnv , { targets : { node : nodeTarget } } ] ,
96
- babelPresetTypescript ,
107
+ [ babelPresetEnv , { bugfixes : true , targets : { node : nodeTarget } } ] ,
108
+ [
109
+ babelPresetTypescript ,
110
+ { allowDeclareFields : true , onlyRemoveTypeImports : true } ,
111
+ ] ,
97
112
] ,
98
113
} ) ,
99
114
compress &&
@@ -224,7 +239,7 @@ export async function bundler({
224
239
const entry = inputs [ idx ] ;
225
240
226
241
const externalDependencies = pkgDependencies . concat (
227
- inputs . filter ( e => e !== entry )
242
+ inputs . filter ( ( e ) => e !== entry )
228
243
) ;
229
244
230
245
const options = await createRollupConfig ( {
@@ -256,7 +271,7 @@ export async function bundler({
256
271
257
272
const watcher = watch ( watchOptions ) ;
258
273
259
- watcher . on ( 'event' , event => {
274
+ watcher . on ( 'event' , ( event ) => {
260
275
switch ( event . code ) {
261
276
case 'ERROR' :
262
277
throw event . error ;
0 commit comments