@@ -11,6 +11,7 @@ const vitePostcssConfig = require('./vite-postcss-config');
11
11
module . exports = ( self ) => {
12
12
return {
13
13
async initWhenReady ( ) {
14
+ self . isDebug = self . apos . asset . isDebugMode ( ) ;
14
15
self . buildRoot = self . apos . asset . getBuildRootDir ( ) ;
15
16
self . buildRootSource = path . join ( self . buildRoot , self . buildSourceFolderName ) ;
16
17
self . distRoot = path . join ( self . buildRoot , self . distFolderName ) ;
@@ -41,6 +42,12 @@ module.exports = (self) => {
41
42
await fs . mkdir ( self . buildRootSource , { recursive : true } ) ;
42
43
} ,
43
44
45
+ printDebug ( id , ...args ) {
46
+ if ( self . isDebug ) {
47
+ self . logDebug ( 'vite-' + id , ...args ) ;
48
+ }
49
+ } ,
50
+
44
51
async buildBefore ( options = { } ) {
45
52
if ( options . isTask ) {
46
53
await self . reset ( ) ;
@@ -49,7 +56,8 @@ module.exports = (self) => {
49
56
copyFiles : true
50
57
} ) ;
51
58
const entrypoints = self . apos . asset . getBuildEntrypoints ( options . types ) ;
52
- self . applyModulePreloadPolyfill ( entrypoints ) ;
59
+ self . ensureInitEntry ( entrypoints ) ;
60
+ self . applyModulePreloadPolyfill ( entrypoints , options ) ;
53
61
await self . createImports ( entrypoints ) ;
54
62
55
63
// Copy the public files so that Vite is not complaining about missing files
@@ -77,6 +85,8 @@ module.exports = (self) => {
77
85
await build ( config ) ;
78
86
self . printLabels ( 'apos' , false ) ;
79
87
88
+ self . printDebug ( 'build-apos' , { viteConfig : config } ) ;
89
+
80
90
return Date . now ( ) ;
81
91
} ,
82
92
@@ -96,6 +106,8 @@ module.exports = (self) => {
96
106
const { build, config } = await self . getViteBuild ( 'public' , options ) ;
97
107
await build ( config ) ;
98
108
self . printLabels ( 'public' , false ) ;
109
+
110
+ self . printDebug ( 'build-public' , { viteConfig : config } ) ;
99
111
} ,
100
112
101
113
// Create an entrypoint configuration for the vite client.
@@ -144,13 +156,6 @@ module.exports = (self) => {
144
156
if ( options . isTask || process . env . APOS_DEV === '1' ) {
145
157
return true ;
146
158
}
147
- // Forced build by type. Keeping the core current logic.
148
- // In play when asset option `publicBundle: false` is set (forces ony apos build).
149
- // Here we are only interested if we should force a "rebuild" or we can
150
- // use the cache.
151
- if ( options . types ?. includes ( id ) ) {
152
- return true ;
153
- }
154
159
if ( ! self . hasViteBuildManifest ( id ) ) {
155
160
return true ;
156
161
}
@@ -163,6 +168,13 @@ module.exports = (self) => {
163
168
return false ;
164
169
}
165
170
171
+ // Forced build by type. Keeping the core current logic.
172
+ // In play when asset option `publicBundle: false` is set - forces apos build
173
+ // if not cached.
174
+ if ( options . types ?. includes ( id ) ) {
175
+ return true ;
176
+ }
177
+
166
178
return true ;
167
179
} ,
168
180
@@ -445,6 +457,8 @@ module.exports = (self) => {
445
457
self . apos . util . log (
446
458
`HMR for "${ options . devServer } " started`
447
459
) ;
460
+
461
+ self . printDebug ( 'dev-middleware' , { viteConfig } ) ;
448
462
} ,
449
463
450
464
// Compute metadata for the source files of all modules using
@@ -530,10 +544,19 @@ module.exports = (self) => {
530
544
531
545
async getEntrypointOutput ( entrypoint , suppressErrors = false ) {
532
546
const manager = self . apos . asset . getEntrypointManger ( entrypoint ) ;
533
- const files = manager . getSourceFiles (
534
- self . currentSourceMeta ,
535
- { composePath : self . composeSourceImportPath }
536
- ) ;
547
+
548
+ // synthetic entrypoints are not processed, they only provide
549
+ // a way to inject additional code (prologue) into the build.
550
+ const files = entrypoint . synthetic
551
+ ? entrypoint . outputs ?. reduce ( ( acc , ext ) => ( {
552
+ ...acc ,
553
+ [ ext ] : [ ]
554
+ } ) , { } )
555
+ : manager . getSourceFiles (
556
+ self . currentSourceMeta ,
557
+ { composePath : self . composeSourceImportPath }
558
+ ) ;
559
+
537
560
const output = await manager . getOutput ( files , {
538
561
modules : self . apos . asset . getRegisteredModules ( ) ,
539
562
suppressErrors
@@ -543,13 +566,63 @@ module.exports = (self) => {
543
566
return output ;
544
567
} ,
545
568
569
+ // Esnure there is always an `index` entrypoint, that holds the
570
+ // prologue and the scenes, required for the polyfill.
571
+ // The created synthetic entrypoint will only include the prologue.
572
+ ensureInitEntry ( entrypoints ) {
573
+ const exists = entrypoints . some ( ( entry ) => entry . type === 'index' ) ;
574
+ if ( exists ) {
575
+ return entrypoints ;
576
+ }
577
+ const first = self . apos . asset . getBuildEntrypoints ( )
578
+ . find ( ( entry ) => entry . type === 'index' ) ;
579
+
580
+ const index = {
581
+ name : 'synth-src' ,
582
+ type : 'index' ,
583
+ // Synthetic entrypoints are not built, they only provide
584
+ // a way to inject additional code (prologue) into the build.
585
+ synthetic : true ,
586
+ label : first . label ,
587
+ scenes : first . scenes ,
588
+ inputs : [ ] ,
589
+ outputs : [ 'js' ] ,
590
+ condition : first . condition ,
591
+ prologue : first . prologue ,
592
+ ignoreSources : [ ] ,
593
+ sources : {
594
+ js : [ ] ,
595
+ scss : [ ]
596
+ }
597
+ } ;
598
+ entrypoints . unshift ( index ) ;
599
+
600
+ return entrypoints ;
601
+ } ,
602
+
603
+ // Ensure Vite client is injected as a first entrypoint.
604
+ // This should be called after the `ensureInitEntry` method,
605
+ // basically as a last step. The method will add the Vite client
606
+ // entrypoint only if needed.
607
+ ensureViteClientEntry ( entrypoints , scenes , buildOptions ) {
608
+ if ( buildOptions . devServer && ! entrypoints . some ( ( entry ) => entry . name === 'vite' ) ) {
609
+ entrypoints . unshift ( self . getViteClientEntrypoint ( scenes ) ) ;
610
+ }
611
+ } ,
612
+
546
613
// Add vite module preload polyfill to the first `index` entrypoint.
547
614
// We can probably remove it soon as the browser support looks good:
548
615
// https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/modulepreload#browser_compatibility
549
- applyModulePreloadPolyfill ( entrypoints ) {
616
+ //
617
+ // The polyfill will be skipped for external frontends. External frontends
618
+ // are responsible for including the polyfill themselves if needed.
619
+ applyModulePreloadPolyfill ( entrypoints , buildOptions ) {
620
+ if ( ! buildOptions . modulePreloadPolyfill ) {
621
+ return ;
622
+ }
550
623
const first = entrypoints . find ( ( entry ) => entry . type === 'index' ) ;
551
624
first . prologue = ( first . prologue || '' ) +
552
- '\nimport \'vite/modulepreload-polyfill\';' ;
625
+ '\nimport \'vite/modulepreload-polyfill\';' ;
553
626
} ,
554
627
555
628
// Adds `manifest` property (object) to the entrypoint after a build.
0 commit comments