Skip to content

Commit 4e3eee1

Browse files
authored
Merge pull request #5 from nkCoding/master
Add `usingComponents` support
2 parents 19673a5 + fa8c318 commit 4e3eee1

14 files changed

Lines changed: 62 additions & 4 deletions

File tree

.eslintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"__DEV__": true,
6161
"App": true,
6262
"Page": true,
63+
"Component": true,
6364
"wx": true,
6465
"getApp": true,
6566
},

src/index.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { resolve, dirname, relative, join, parse } from 'path';
44
import { optimize, LoaderTargetPlugin, JsonpTemplatePlugin } from 'webpack';
55
import { ConcatSource } from 'webpack-sources';
66
import globby from 'globby';
7-
import { defaults } from 'lodash';
7+
import { defaults, values } from 'lodash';
88
import MultiEntryPlugin from 'webpack/lib/MultiEntryPlugin';
99
import SingleEntryPlugin from 'webpack/lib/SingleEntryPlugin';
1010
import 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) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Component({})
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"component": true
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<view class="common-text">Common Component loaded.</view>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.common-text {
2+
color: grey;
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Component({})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"component": true,
3+
"usingComponents": {
4+
"common-component": "../common-component/common-component"
5+
}
6+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<view>
2+
<view>'Index Component loaded.'</view>
3+
<common-component></common-component>
4+
</view>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Component({})

0 commit comments

Comments
 (0)