11import { existsSync } from 'node:fs' ;
22import { mkdir , realpath , writeFile } from 'node:fs/promises' ;
33import os from 'node:os' ;
4- import { basename , extname , join , resolve , win32 , posix } from 'path' ;
4+ import { dirname , basename , extname , join , resolve , win32 , posix } from 'path' ;
55import { pathToFileURL } from 'url' ;
66
77import chalk from 'chalk' ;
@@ -58,7 +58,8 @@ Builds a template and saves the result
5858// Credit: https://github.com/rollup/plugins/blob/master/packages/pluginutils/src/normalizePath.ts#L5
5959const normalizePath = ( filename : string ) => filename . split ( win32 . sep ) . join ( posix . sep ) ;
6060
61- export const build = async ( path : string , argv : BuildOptions ) => {
61+ // FIXME: in v2 change the signature to an object
62+ export const build = async ( path : string , argv : BuildOptions , outputBasePath ?: string ) => {
6263 const { out, plain, props = '{}' , writeToFile = true } = argv ;
6364 const platformPath = isWindows ? pathToFileURL ( normalizePath ( path ) ) . toString ( ) : path ;
6465 const template = await import ( platformPath ) ;
@@ -96,9 +97,15 @@ export const build = async (path: string, argv: BuildOptions) => {
9697 process . exit ( 1 ) ;
9798 }
9899
100+ await mkdir ( out ! , { recursive : true } ) ;
101+
99102 const buildProps = JSON . parse ( props ) ;
100103 const component = componentExport ( buildProps ) ;
101- const writePath = join ( out ! , basename ( path ) . replace ( extname ( path ) , extension ) ) ;
104+ const writePath = outputBasePath
105+ ? join ( out ! , path . replace ( outputBasePath , '' ) . replace ( extname ( path ) , extension ) )
106+ : join ( out ! , basename ( path ) . replace ( extname ( path ) , extension ) ) ;
107+
108+ await mkdir ( dirname ( writePath ) , { recursive : true } ) ;
102109
103110 if ( plain ) {
104111 const plainText = await render ( component , { plainText : plain } ) ;
@@ -108,7 +115,6 @@ export const build = async (path: string, argv: BuildOptions) => {
108115
109116 const html = await render ( component , argv as any ) ;
110117
111- await mkdir ( out ! , { recursive : true } ) ;
112118 if ( writeToFile ) await writeFile ( writePath , html , 'utf8' ) ;
113119
114120 return html ;
@@ -125,7 +131,7 @@ const compile = async (files: string[], outDir: string) => {
125131 write : true
126132 } ) ;
127133
128- return globby ( [ normalizePath ( join ( outDir , '*.js' ) ) ] ) ;
134+ return globby ( [ normalizePath ( join ( outDir , '**/* .js' ) ) ] ) ;
129135} ;
130136
131137export const buildTemplates = async ( target : string , options : BuildOptionsInternal ) => {
@@ -150,7 +156,7 @@ export const buildTemplates = async (target: string, options: BuildOptionsIntern
150156 compiledFiles . map ( async ( filePath , index ) => {
151157 const result = {
152158 fileName : targetFiles [ index ] ,
153- html : await build ( filePath , { ...options , out : outputPath } )
159+ html : await build ( filePath , { ...options , out : outputPath } , esbuildOutPath )
154160 } ;
155161
156162 if ( showStats ) {
0 commit comments