@@ -33,16 +33,34 @@ const terserOptions = {
33
33
format : { comments : false } ,
34
34
} ;
35
35
36
+ let independentPackages = [ ] ;
37
+ async function findIndependentPackages ( ) {
38
+ const { subpackages } = await fs . readJson (
39
+ path . resolve ( 'src' , 'app.json' ) ,
40
+ 'utf8' ,
41
+ ) ;
42
+ if ( subpackages ) {
43
+ independentPackages = subpackages
44
+ . filter ( ( { independent } ) => independent )
45
+ . map ( ( { root } ) => root ) ;
46
+ }
47
+ }
48
+
36
49
const builtLibraries = [ ] ;
37
- const bundledModules = new Set ( ) ;
38
- async function bundleModule ( module ) {
50
+ const bundledModules = new Map ( ) ;
51
+ async function bundleModule ( module , pkg ) {
52
+ const bundled = bundledModules . get ( pkg ) ;
39
53
if (
40
- bundledModules . has ( module ) ||
54
+ bundled ? .has ( module ) ||
41
55
builtLibraries . some ( ( library ) => module . startsWith ( library ) )
42
56
) {
43
- return ;
57
+ return false ;
58
+ }
59
+ if ( bundled ) {
60
+ bundled . add ( module ) ;
61
+ } else {
62
+ bundledModules . set ( pkg , new Set ( [ module ] ) ) ;
44
63
}
45
- bundledModules . add ( module ) ;
46
64
47
65
const {
48
66
packageJson : { peerDependencies } ,
@@ -64,12 +82,13 @@ async function bundleModule(module) {
64
82
} ) ;
65
83
await bundle . write ( {
66
84
exports : 'named' ,
67
- file : `dist/miniprogram_npm/${ module } /index.js` ,
85
+ file : `${ pkg . replace ( 'src' , ' dist' ) } /miniprogram_npm/${ module } /index.js` ,
68
86
format : 'cjs' ,
69
87
} ) ;
88
+ return true ;
70
89
}
71
90
72
- function traverseAST ( ast , babelOnly = false ) {
91
+ function traverseAST ( ast , pkg , babelOnly = false ) {
73
92
traverse . default ( ast , {
74
93
CallExpression ( { node } ) {
75
94
if (
@@ -81,7 +100,27 @@ function traverseAST(ast, babelOnly = false) {
81
100
return ;
82
101
}
83
102
84
- const promise = bundleModule ( node . arguments [ 0 ] . value ) ;
103
+ const module = node . arguments [ 0 ] . value ;
104
+ let promise = bundleModule ( module , pkg ) ;
105
+ if ( babelOnly ) {
106
+ promise = promise . then ( ( valid ) => {
107
+ if ( ! valid ) return ;
108
+ return Promise . all (
109
+ independentPackages . map ( ( item ) => {
110
+ const bundled = bundledModules . get ( item ) ;
111
+ if ( bundled ) {
112
+ bundled . add ( module ) ;
113
+ } else {
114
+ bundledModules . set ( pkg , new Set ( [ module ] ) ) ;
115
+ }
116
+ return fs . copy (
117
+ path . resolve ( 'dist' , 'miniprogram_npm' , module ) ,
118
+ path . resolve ( 'dist' , item , 'miniprogram_npm' , module ) ,
119
+ ) ;
120
+ } ) ,
121
+ ) ;
122
+ } ) ;
123
+ }
85
124
bundleJobs ?. push ( promise ) ;
86
125
} ,
87
126
} ) ;
@@ -118,15 +157,15 @@ async function buildComponentLibrary(name) {
118
157
const jobs = [ ] ;
119
158
const tnm = async ( filePath ) => {
120
159
const result = await babel . transformFileAsync ( filePath , { ast : true } ) ;
121
- traverseAST ( result . ast , true ) ;
160
+ traverseAST ( result . ast , 'src' , true ) ;
122
161
const code = __PROD__
123
162
? ( await minify ( result . code , terserOptions ) ) . code
124
163
: result . code ;
125
164
await fs . writeFile ( filePath , code ) ;
126
165
} ;
127
166
128
167
const watcher = chokidar . watch ( [ destination ] , {
129
- ignored : ( path , stats ) => stats ?. isFile ( ) && ! path . endsWith ( '.js' ) ,
168
+ ignored : ( file , stats ) => stats ?. isFile ( ) && ! file . endsWith ( '.js' ) ,
130
169
} ) ;
131
170
watcher . on ( 'add' , ( filePath ) => {
132
171
const promise = tnm ( filePath ) ;
@@ -136,6 +175,16 @@ async function buildComponentLibrary(name) {
136
175
const promise = watcher . close ( ) ;
137
176
jobs . push ( promise ) ;
138
177
await Promise . all ( jobs ) ;
178
+ if ( independentPackages . length > 0 ) {
179
+ await Promise . all (
180
+ independentPackages . map ( ( item ) =>
181
+ fs . copy (
182
+ destination ,
183
+ path . resolve ( 'dist' , item , 'miniprogram_npm' , name ) ,
184
+ ) ,
185
+ ) ,
186
+ ) ;
187
+ }
139
188
resolve ( ) ;
140
189
} ) ;
141
190
} ) ;
@@ -166,7 +215,11 @@ async function processScript(filePath) {
166
215
return ;
167
216
}
168
217
169
- traverseAST ( ast ) ;
218
+ const pkg = independentPackages . find ( ( item ) =>
219
+ filePath . startsWith ( path . normalize ( `src/${ item } ` ) ) ,
220
+ ) ;
221
+ // The `src/` prefix is added to to distinguish `src` and `src/src`.
222
+ traverseAST ( ast , pkg ? `src/${ pkg } ` : 'src' ) ;
170
223
171
224
if ( __PROD__ ) {
172
225
code = ( await minify ( code , terserOptions ) ) . code ;
@@ -231,12 +284,13 @@ const cb = async (filePath) => {
231
284
232
285
async function dev ( ) {
233
286
await fs . remove ( 'dist' ) ;
287
+ await findIndependentPackages ( ) ;
234
288
await scanDependencies ( ) ;
235
289
chokidar
236
290
. watch ( [ 'src' ] , {
237
- ignored : ( path , stats ) =>
291
+ ignored : ( file , stats ) =>
238
292
stats ?. isFile ( ) &&
239
- ( path . endsWith ( '.gitkeep' ) || path . endsWith ( '.DS_Store' ) ) ,
293
+ ( file . endsWith ( '.gitkeep' ) || file . endsWith ( '.DS_Store' ) ) ,
240
294
} )
241
295
. on ( 'add' , ( filePath ) => {
242
296
const promise = cb ( filePath ) ;
@@ -258,11 +312,12 @@ async function dev() {
258
312
259
313
async function prod ( ) {
260
314
await fs . remove ( 'dist' ) ;
315
+ await findIndependentPackages ( ) ;
261
316
await scanDependencies ( ) ;
262
317
const watcher = chokidar . watch ( [ 'src' ] , {
263
- ignored : ( path , stats ) =>
318
+ ignored : ( file , stats ) =>
264
319
stats ?. isFile ( ) &&
265
- ( path . endsWith ( '.gitkeep' ) || path . endsWith ( '.DS_Store' ) ) ,
320
+ ( file . endsWith ( '.gitkeep' ) || file . endsWith ( '.DS_Store' ) ) ,
266
321
} ) ;
267
322
watcher . on ( 'add' , ( filePath ) => {
268
323
const promise = cb ( filePath ) ;
0 commit comments