@@ -3,7 +3,7 @@ import path from 'upath';
33import fs from 'fs-extra' ;
44import fg from 'fast-glob' ;
55import mm from 'micromatch' ;
6- import { Entry , UserConfig } from './types' ;
6+ import { Entry , UserConfig , UserEntryConfigItem } from './types' ;
77import { flattenPath , normalizeRoutePath , toArray } from './utils' ;
88import { DEFAULT_ENTRY_MODULE_ID , DEFAULT_HTML_PATH } from './constants' ;
99import {
@@ -25,35 +25,64 @@ function getHtmlOutDir(root: string) {
2525 return path . resolve ( nodeModulesDir , '.conventional-entries' ) ;
2626}
2727
28+ function resolveUserEntries (
29+ root : string ,
30+ userEntries : UserConfig [ 'entries' ] = 'src'
31+ ) : Required < UserEntryConfigItem > [ ] {
32+ return toArray ( userEntries ) . map ( userEntry => {
33+ const userEntryObj =
34+ typeof userEntry === 'string' ? { dir : userEntry } : userEntry ;
35+ return {
36+ basePath : userEntryObj . basePath || '/' ,
37+ dir : path . resolve ( root , userEntryObj . dir ) ,
38+ pattern : userEntryObj . pattern || '**/main.{js,jsx,ts,tsx}' ,
39+ ignore : userEntryObj . ignore || [ ] ,
40+ } ;
41+ } ) ;
42+ }
43+
2844function resolveEntries (
2945 root : string ,
30- src : string ,
31- pattern : string | string [ ] ,
32- basePath : string
46+ userEntries : Required < UserEntryConfigItem > [ ]
3347) : Entry [ ] {
3448 const htmlOutDir = getHtmlOutDir ( root ) ;
3549
36- return fg
37- . sync ( pattern , { cwd : src , absolute : true } )
38- . sort ( ( a , b ) => b . length - a . length )
39- . map < Entry > ( entryPath => {
40- const routePath = normalizeRoutePath (
41- basePath + '/' + path . relative ( src , path . dirname ( entryPath ) )
42- ) ;
43- const serverPath = normalizeRoutePath ( path . relative ( root , entryPath ) ) ;
44- // will create symlink for html path later
45- const htmlPath = path . resolve (
46- htmlOutDir ,
47- `${ flattenPath ( routePath . slice ( basePath . length ) ) || 'index' } .html`
48- ) ;
49-
50- return {
51- entryPath,
52- routePath,
53- serverPath,
54- htmlPath,
55- } ;
56- } ) ;
50+ userEntries = toArray ( userEntries ) ;
51+
52+ return userEntries . flatMap ( userEntry => {
53+ const {
54+ basePath = '/' ,
55+ dir,
56+ pattern = '**/main.{js,jsx,ts,tsx}' ,
57+ ignore = [ ] ,
58+ } = typeof userEntry === 'string' ? { dir : userEntry } : userEntry ;
59+
60+ return fg
61+ . sync ( pattern || '**/main.{js,jsx,ts,tsx}' , {
62+ cwd : dir ,
63+ ignore : toArray ( ignore ) ,
64+ absolute : true ,
65+ } )
66+ . sort ( ( a , b ) => b . length - a . length )
67+ . map < Entry > ( entryPath => {
68+ const routePath = normalizeRoutePath (
69+ basePath + '/' + path . relative ( dir , path . dirname ( entryPath ) )
70+ ) ;
71+ const serverPath = normalizeRoutePath ( path . relative ( root , entryPath ) ) ;
72+ // will create link for html path later
73+ const htmlPath = path . resolve (
74+ htmlOutDir ,
75+ `${ flattenPath ( routePath . slice ( basePath . length ) ) || 'index' } .html`
76+ ) ;
77+
78+ return {
79+ entryPath,
80+ routePath,
81+ serverPath,
82+ htmlPath,
83+ } ;
84+ } ) ;
85+ } ) ;
5786}
5887
5988function resolveInput (
@@ -104,15 +133,25 @@ function ensureLinkHtmlPath(root: string, entry: Entry) {
104133 }
105134}
106135
136+ function isMatchEntry (
137+ userEntries : Required < UserEntryConfigItem > [ ] ,
138+ filePath : string
139+ ) : boolean {
140+ return userEntries . some ( ( { dir, pattern, ignore } ) => {
141+ return (
142+ filePath . startsWith ( dir ) &&
143+ mm . isMatch ( filePath , pattern , { cwd : dir , ignore } )
144+ ) ;
145+ } ) ;
146+ }
147+
107148export function conventionalEntries ( userConfig : UserConfig = { } ) : Plugin [ ] {
108149 const {
109- pattern = '**/main.{js,jsx,ts,tsx}' ,
110- basePath = '/' ,
111150 prettifyHtml : userPrettifyHtml = false ,
112151 minifyHtml : userMinifyHtml = true ,
113152 } = userConfig ;
114153
115- let src : string ;
154+ let userEntries : Required < UserEntryConfigItem > [ ] ;
116155 let entries : Entry [ ] ;
117156
118157 return [
@@ -126,18 +165,18 @@ export function conventionalEntries(userConfig: UserConfig = {}): Plugin[] {
126165 : process . cwd ( )
127166 ) ;
128167
129- src = path . resolve ( root , userConfig ?. src || 'src' ) ;
130- entries = resolveEntries ( root , src , pattern , basePath ) ;
131-
132- const srcFromRoot = path . relative ( root , src ) ;
168+ userEntries = resolveUserEntries ( root , userConfig . entries ) ;
169+ entries = resolveEntries ( root , userEntries ) ;
133170
134171 return {
135172 // Since html may dynamically append the page entry after starting the server,
136173 // we cannot rely on vite's default optimization strategy.
137174 // We need to manually write the entries here,
138175 // so that vite can perform dependency crawling and optimization
139176 optimizeDeps : {
140- entries : toArray ( pattern ) . map ( p => `${ srcFromRoot } /${ p } ` ) ,
177+ entries : userEntries . flatMap ( ( { dir, pattern } ) =>
178+ toArray ( pattern ) . map ( p => `${ path . relative ( root , dir ) } /${ p } ` )
179+ ) ,
141180 include : [ 'react' , 'react-dom/client' ] ,
142181 } ,
143182 build : {
@@ -154,7 +193,7 @@ export function conventionalEntries(userConfig: UserConfig = {}): Plugin[] {
154193 } ,
155194 configureServer ( server ) {
156195 function listener ( filePath : string ) {
157- if ( ! mm . isMatch ( filePath , pattern , { cwd : src } ) ) {
196+ if ( ! isMatchEntry ( userEntries , filePath ) ) {
158197 return ;
159198 }
160199
0 commit comments