@@ -7,15 +7,12 @@ import { Context } from 'build-scripts';
7
7
import type { CommandArgs , CommandName } from 'build-scripts' ;
8
8
import type { Config } from '@ice/shared-config/types' ;
9
9
import type { AppConfig } from '@ice/runtime/types' ;
10
- import fse from 'fs-extra' ;
11
10
import webpack from '@ice/bundles/compiled/webpack/index.js' ;
12
11
import type {
13
12
DeclarationData ,
14
13
PluginData ,
15
14
ExtendsPluginAPI ,
16
- TargetDeclarationData ,
17
15
} from './types/index.js' ;
18
- import { DeclarationType } from './types/index.js' ;
19
16
import Generator from './service/runtimeGenerator.js' ;
20
17
import { createServerCompiler } from './service/serverCompiler.js' ;
21
18
import createWatch from './service/watchSource.js' ;
@@ -41,6 +38,7 @@ import addPolyfills from './utils/runtimePolyfill.js';
41
38
import webpackBundler from './bundler/webpack/index.js' ;
42
39
import rspackBundler from './bundler/rspack/index.js' ;
43
40
import getDefaultTaskConfig from './plugins/task.js' ;
41
+ import hasDocument from './utils/hasDocument.js' ;
44
42
45
43
const require = createRequire ( import . meta. url ) ;
46
44
const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
@@ -75,56 +73,32 @@ async function createService({ rootDir, command, commandArgs }: CreateServiceOpt
75
73
let entryCode = 'render();' ;
76
74
77
75
const generatorAPI = {
78
- addExport : ( declarationData : Omit < DeclarationData , 'declarationType' > ) => {
79
- generator . addDeclaration ( 'framework' , {
80
- ...declarationData ,
81
- declarationType : DeclarationType . NORMAL ,
82
- } ) ;
76
+ addExport : ( declarationData : DeclarationData ) => {
77
+ generator . addDeclaration ( 'framework' , declarationData ) ;
83
78
} ,
84
- addTargetExport : ( declarationData : Omit < TargetDeclarationData , 'declarationType' > ) => {
85
- generator . addDeclaration ( 'framework' , {
86
- ...declarationData ,
87
- declarationType : DeclarationType . TARGET ,
88
- } ) ;
79
+ addExportTypes : ( declarationData : DeclarationData ) => {
80
+ generator . addDeclaration ( 'frameworkTypes' , declarationData ) ;
89
81
} ,
90
- addExportTypes : ( declarationData : Omit < DeclarationData , 'declarationType' > ) => {
91
- generator . addDeclaration ( 'frameworkTypes' , {
92
- ...declarationData ,
93
- declarationType : DeclarationType . NORMAL ,
94
- } ) ;
95
- } ,
96
- addRuntimeOptions : ( declarationData : Omit < DeclarationData , 'declarationType' > ) => {
97
- generator . addDeclaration ( 'runtimeOptions' , {
98
- ...declarationData ,
99
- declarationType : DeclarationType . NORMAL ,
100
- } ) ;
82
+ addRuntimeOptions : ( declarationData : DeclarationData ) => {
83
+ generator . addDeclaration ( 'runtimeOptions' , declarationData ) ;
101
84
} ,
102
85
removeRuntimeOptions : ( removeSource : string | string [ ] ) => {
103
86
generator . removeDeclaration ( 'runtimeOptions' , removeSource ) ;
104
87
} ,
105
- addRouteTypes : ( declarationData : Omit < DeclarationData , 'declarationType' > ) => {
106
- generator . addDeclaration ( 'routeConfigTypes' , {
107
- ...declarationData ,
108
- declarationType : DeclarationType . NORMAL ,
109
- } ) ;
88
+ addRouteTypes : ( declarationData : DeclarationData ) => {
89
+ generator . addDeclaration ( 'routeConfigTypes' , declarationData ) ;
110
90
} ,
111
91
addRenderFile : generator . addRenderFile ,
112
92
addRenderTemplate : generator . addTemplateFiles ,
113
93
addEntryCode : ( callback : ( originalCode : string ) => string ) => {
114
94
entryCode = callback ( entryCode ) ;
115
95
} ,
116
96
addEntryImportAhead : ( declarationData : Pick < DeclarationData , 'source' > ) => {
117
- generator . addDeclaration ( 'entry' , {
118
- ...declarationData ,
119
- declarationType : DeclarationType . NORMAL ,
120
- } ) ;
97
+ generator . addDeclaration ( 'entry' , declarationData ) ;
121
98
} ,
122
99
modifyRenderData : generator . modifyRenderData ,
123
100
addDataLoaderImport : ( declarationData : DeclarationData ) => {
124
- generator . addDeclaration ( 'dataLoaderImport' , {
125
- ...declarationData ,
126
- declarationType : DeclarationType . NORMAL ,
127
- } ) ;
101
+ generator . addDeclaration ( 'dataLoaderImport' , declarationData ) ;
128
102
} ,
129
103
getExportList : ( registerKey : string ) => {
130
104
return generator . getExportList ( registerKey ) ;
@@ -239,7 +213,7 @@ async function createService({ rootDir, command, commandArgs }: CreateServiceOpt
239
213
240
214
// get userConfig after setup because of userConfig maybe modified by plugins
241
215
const { userConfig } = ctx ;
242
- const { routes : routesConfig , server, syntaxFeatures, polyfill, output : { distType } } = userConfig ;
216
+ const { routes : routesConfig , server, syntaxFeatures, polyfill } = userConfig ;
243
217
244
218
const coreEnvKeys = getCoreEnvKeys ( ) ;
245
219
@@ -270,6 +244,8 @@ async function createService({ rootDir, command, commandArgs }: CreateServiceOpt
270
244
// Only when code splitting use the default strategy or set to `router`, the router will be lazy loaded.
271
245
const lazy = [ true , 'chunks' , 'page' , 'page-vendors' ] . includes ( userConfig . codeSplitting ) ;
272
246
const { routeImports, routeDefinition } = getRoutesDefinition ( routesInfo . routes , lazy ) ;
247
+ const loaderExports = hasExportAppData || Boolean ( routesInfo . loaders ) ;
248
+ const hasDataLoader = Boolean ( userConfig . dataLoader ) && loaderExports ;
273
249
// add render data
274
250
generator . setRenderData ( {
275
251
...routesInfo ,
@@ -286,16 +262,15 @@ async function createService({ rootDir, command, commandArgs }: CreateServiceOpt
286
262
// Enable react-router for web as default.
287
263
enableRoutes : true ,
288
264
entryCode,
289
- jsOutput : distType . includes ( 'javascript' ) ,
290
- hasDocument : fse . existsSync ( path . join ( rootDir , 'src/document.tsx' ) ) || fse . existsSync ( path . join ( rootDir , 'src/document.jsx' ) ) || fse . existsSync ( path . join ( rootDir , 'src/document.js' ) ) ,
265
+ hasDocument : hasDocument ( rootDir ) ,
291
266
dataLoader : userConfig . dataLoader ,
267
+ hasDataLoader,
292
268
routeImports,
293
269
routeDefinition,
294
270
} ) ;
295
271
dataCache . set ( 'routes' , JSON . stringify ( routesInfo ) ) ;
296
272
dataCache . set ( 'hasExportAppData' , hasExportAppData ? 'true' : '' ) ;
297
273
298
- const hasDataLoader = Boolean ( userConfig . dataLoader ) && ( hasExportAppData || Boolean ( routesInfo . loaders ) ) ;
299
274
// Render exports files if route component export dataLoader / pageConfig.
300
275
renderExportsTemplate (
301
276
{
@@ -378,8 +353,12 @@ async function createService({ rootDir, command, commandArgs }: CreateServiceOpt
378
353
) ;
379
354
380
355
const appConfig : AppConfig = ( await getAppConfig ( ) ) . default ;
381
-
382
- updateRuntimeEnv ( appConfig , { disableRouter } ) ;
356
+ updateRuntimeEnv ( appConfig , {
357
+ disableRouter,
358
+ // The optimization for runtime size should only be enabled in production mode.
359
+ routesConfig : command !== 'build' || routesInfo . routesExports . length > 0 ,
360
+ dataLoader : command !== 'build' || loaderExports ,
361
+ } ) ;
383
362
384
363
return {
385
364
run : async ( ) => {
0 commit comments