@@ -4,7 +4,7 @@ import { resolve, dirname, relative, join, parse } from 'path';
44import { optimize , LoaderTargetPlugin , JsonpTemplatePlugin } from 'webpack' ;
55import { ConcatSource } from 'webpack-sources' ;
66import globby from 'globby' ;
7- import { defaults } from 'lodash' ;
7+ import { defaults , values } from 'lodash' ;
88import MultiEntryPlugin from 'webpack/lib/MultiEntryPlugin' ;
99import SingleEntryPlugin from 'webpack/lib/SingleEntryPlugin' ;
1010import FunctionModulePlugin from 'webpack/lib/FunctionModulePlugin' ;
@@ -143,7 +143,26 @@ export default class WXAppPlugin {
143143 async getEntryResource ( ) {
144144 const appJSONFile = resolve ( this . base , 'app.json' ) ;
145145 const { pages = [ ] } = await readJson ( appJSONFile ) ;
146- return [ 'app' ] . concat ( pages ) ;
146+ const components = new Set ( ) ;
147+ for ( const page of pages ) {
148+ await this . getComponents ( components , resolve ( this . base , page ) ) ;
149+ }
150+ return [ 'app' , ...pages , ...components ] ;
151+ }
152+
153+ async getComponents ( components , instance ) {
154+ const { usingComponents = { } } =
155+ await readJson ( `${ instance } .json` ) . catch (
156+ err => err && err . code !== 'ENOENT' && console . error ( err )
157+ ) || { } ;
158+ const componentBase = parse ( instance ) . dir ;
159+ for ( const relativeComponent of values ( usingComponents ) ) {
160+ const component = resolve ( componentBase , relativeComponent ) ;
161+ if ( ! components . has ( component ) ) {
162+ components . add ( relative ( this . base , component ) ) ;
163+ await this . getComponents ( components , component ) ;
164+ }
165+ }
147166 }
148167
149168 getFullScriptPath ( path ) {
0 commit comments