@@ -14,6 +14,35 @@ import { type PackageAnalyzer } from './package-analyzer';
1414import { type PackageInstaller } from './package-installer' ;
1515import { type SourceInclusion } from './source-inclusion' ;
1616import { type DependencyDescription } from './dependency-description' ;
17+ import { type DependencyInclusion } from './dependency-inclusion' ;
18+ import { type LoaderOptions } from './loader' ;
19+
20+ /**
21+ * onRequiringModule callback is called before auto-tracing on a moduleId. It would not be called for any modules provided by app's src files or explicit dependencies config in aurelia.json.
22+
23+ * Three types possible result (all can be returned in promise):
24+ * 1. Boolean false: ignore this moduleId;
25+ * 2. Array of strings like ['a', 'b']: require module id "a" and "b" instead;
26+ * 3. A string: the full JavaScript content of this module
27+ * 4. All other returns are ignored and go onto performing auto-tracing.
28+ *
29+ * Usage example in applications `build.ts` file:
30+ *
31+ * function writeBundles() {
32+ * return buildCLI.dest({
33+ * // use onRequiringModule to ignore tracing "template/**\/*"
34+ * onRequiringModule: moduleId => {
35+ * if (moduleId.startsWith("template/")) {
36+ * return false;
37+ * }
38+ * }
39+ * });
40+ *
41+ */
42+ export type BuildOptions = { onRequiringModule ?: onRequiringModuleCallback ; onNotBundled ?: onNotBundledCallback } ;
43+ type onRequiringModuleResult = boolean | string | string [ ] | Promise < boolean | string | string [ ] >
44+ type onRequiringModuleCallback = ( moduleId : string ) => onRequiringModuleResult ;
45+ type onNotBundledCallback = ( items : BundledSource [ ] ) => void ;
1746
1847const logger = getLogger ( 'Bundler' ) ;
1948
@@ -28,7 +57,7 @@ export class Bundler{
2857 private readonly autoInstall : boolean ;
2958 private triedAutoInstalls : Set < string > ;
3059 public buildOptions : Configuration ;
31- public loaderOptions : Omit < AureliaJson . ILoader , "plugins" > & { plugins : LoaderPlugin [ ] } ;
60+ public loaderOptions : LoaderOptions ;
3261 public loaderConfig : AureliaJson . ILoaderConfig ;
3362 public configTargetBundle : Bundle ;
3463
@@ -155,8 +184,8 @@ export class Bundler{
155184 }
156185 }
157186
158- build ( opts ?) {
159- let onRequiringModule , onNotBundled ;
187+ build ( opts ?: BuildOptions ) {
188+ let onRequiringModule : onRequiringModuleCallback | undefined , onNotBundled : onNotBundledCallback | undefined ;
160189 if ( opts && typeof opts . onRequiringModule === 'function' ) {
161190 onRequiringModule = opts . onRequiringModule ;
162191 }
@@ -200,9 +229,9 @@ export class Bundler{
200229 return Utils . runSequentially (
201230 Array . from ( deps ) . sort ( ) ,
202231 d => {
203- return new Promise ( resolve => {
232+ return new Promise < undefined | onRequiringModuleResult > ( resolve => {
204233 resolve ( onRequiringModule && onRequiringModule ( d ) ) ;
205- } ) . then (
234+ } ) . then < void , void > (
206235 result => {
207236 // ignore this module id
208237 if ( result === false ) return ;
@@ -270,7 +299,7 @@ export class Bundler{
270299 }
271300
272301 getDependencyInclusions ( ) {
273- return this . bundles . reduce ( ( a , b ) => a . concat ( b . getDependencyInclusions ( ) ) , [ ] ) ;
302+ return this . bundles . reduce < DependencyInclusion [ ] > ( ( a , b ) => a . concat ( b . getDependencyInclusions ( ) ) , [ ] ) ;
274303 }
275304
276305 addMissingDep ( id : string ) {
@@ -358,7 +387,7 @@ function analyzeDependency(packageAnalyzer: PackageAnalyzer, dependency: ILoader
358387 return packageAnalyzer . reverseEngineer ( dependency ) ;
359388}
360389
361- function subsume ( bundles , item ) {
390+ function subsume ( bundles : Bundle [ ] , item : BundledSource ) {
362391 for ( let i = 0 , ii = bundles . length ; i < ii ; ++ i ) {
363392 if ( bundles [ i ] . trySubsume ( item ) ) {
364393 return ;
@@ -367,7 +396,7 @@ function subsume(bundles, item) {
367396 logger . warn ( item . path + ' is not captured by any bundle file. You might need to adjust the bundles source matcher in aurelia.json.' ) ;
368397}
369398
370- function normalizeKey ( p ) {
399+ function normalizeKey ( p : string ) {
371400 return path . normalize ( p ) ;
372401}
373402
0 commit comments