Skip to content

Commit 5fc76ff

Browse files
committed
fix: fixed assets build
1 parent b0508d7 commit 5fc76ff

File tree

7 files changed

+50
-68
lines changed

7 files changed

+50
-68
lines changed

packages/wepy-cli/core/compile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Compile extends Hook {
3636
this.options.entry = path.resolve(path.join(this.options.src, ENTRY_FILE));
3737

3838
this.compiled = {};
39-
this.npm = new moduleSet();
39+
this.vendors = new moduleSet();
4040
this.assets = new moduleSet();
4141
this.resolvers = {};
4242

packages/wepy-cli/core/moduleSet.js

+8
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ class ModuleSet {
6161
}
6262
}
6363

64+
type (v) {
65+
if (typeof v === 'number') {
66+
return this._type[this._array[v]];
67+
} else {
68+
return this._type[v];
69+
}
70+
}
71+
6472
}
6573

6674

packages/wepy-cli/core/plugins/build/vendor.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -45,27 +45,27 @@ exports = module.exports = function () {
4545

4646
this.logger.info('vendor', 'building vendor');
4747

48-
let npmList = this.npm.array();
49-
let npmCode = '';
48+
let vendorList = this.vendors.array();
49+
let code = '';
5050

51-
npmList.forEach((npm, i) => {
52-
let data = this.npm.data(npm);
51+
vendorList.forEach((item, i) => {
52+
let data = this.vendors.data(item);
5353

5454
this.hookSeq('script-dep-fix', data, true);
5555

56-
npmCode += '/***** module ' + i + ' start *****/\n';
57-
npmCode += '/***** ' + data.file + ' *****/\n';
58-
npmCode += 'function(module, exports, __wepy_require) {';
59-
npmCode += data.source.source() + '\n';
60-
npmCode += '}';
61-
if (i !== npmList.length - 1) {
62-
npmCode += ',';
56+
code += '/***** module ' + i + ' start *****/\n';
57+
code += '/***** ' + data.file + ' *****/\n';
58+
code += 'function(module, exports, __wepy_require) {';
59+
code += data.source.source() + '\n';
60+
code += '}';
61+
if (i !== vendorList.length - 1) {
62+
code += ',';
6363
}
64-
npmCode += '/***** module ' + i + ' end *****/\n\n\n';
64+
code += '/***** module ' + i + ' end *****/\n\n\n';
6565
});
6666

6767
let template = VENDOR_INJECTION.concat([]);
68-
template[1] = npmCode;
68+
template[1] = code;
6969

7070
vendor.outputCode = template.join('');
7171
vendor.targetFile = path.join(this.context, this.options.target, 'vendor.js');

packages/wepy-cli/core/plugins/parser/script.js

+8-40
Original file line numberDiff line numberDiff line change
@@ -72,40 +72,6 @@ function ast (source) {
7272
return ast;
7373
}
7474

75-
function parseDep (issuer, dep) {
76-
return this.compilation.resolvers.normal.resolve({issuer: issuer}, path.dirname(issuer), dep.module, {}).then(rst => {
77-
let npm = rst.meta.descriptionFileRoot !== this.compilation.context;
78-
79-
let assets = this.compilation.assets;
80-
81-
if (!rst.path) {
82-
return rst.path;
83-
}
84-
let id = assets.get(rst.path);
85-
if (id !== undefined) {
86-
return id;
87-
}
88-
89-
90-
let ext = path.extname(rst.path);
91-
92-
if (ext === '.js') {
93-
if (npm) {
94-
return this.parse({
95-
code: fs.readFileSync(rst.path, 'utf-8')
96-
}, {
97-
file: rst.path,
98-
npm: npm
99-
});
100-
}
101-
} else if (ext === this.compilation.options.wpyExt) {
102-
return this.compilation.parsers.wpy.parse(rst.path);
103-
}
104-
}).catch(e => {
105-
console.error(e);
106-
});
107-
}
108-
10975
exports = module.exports = function () {
11076

11177
this.register('wepy-parser-dep', function (node, ctx, dep) {
@@ -155,10 +121,10 @@ exports = module.exports = function () {
155121
let assets = this.assets;
156122
let npmModules = this.npm;
157123
if (ctx.npm) {
158-
if (assets.pending(ctx.file)) {
159-
return Promise.resolve(assets.get(ctx.file));
124+
if (this.vendors.pending(ctx.file)) {
125+
return Promise.resolve(this.vendors.get(ctx.file));
160126
}
161-
assets.add(ctx.file, 'npm');
127+
this.vendors.add(ctx.file, 'npm');
162128
}
163129

164130
let source = new ReplaceSource(new RawSource(node.compiled.code));
@@ -174,15 +140,17 @@ exports = module.exports = function () {
174140
});
175141

176142
return Promise.all(depTasks).then(rst => {
143+
let type = ctx.npm ? 'npm' : (ctx.sfc ? 'component' : 'require');
177144
let obj = {
178145
file: ctx.file,
179146
parser: walker,
180147
code: node.compiled.code,
181148
source: source,
182-
depModules: rst
149+
depModules: rst,
150+
type: type
183151
};
184-
let assets = this.assets;
185-
assets.update(ctx.file, obj, ctx.npm ? 'npm' : (ctx.sfc ? 'app' : 'require'));
152+
let assets = ctx.npm ? this.vendors : this.assets;
153+
assets.update(ctx.file, obj, type);
186154
obj.id = assets.get(ctx.file);
187155
return obj;
188156
});

packages/wepy-cli/core/plugins/scriptDepFix.js

+15-10
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,24 @@ exports = module.exports = function () {
1111
if (isNPM) {
1212
replaceMent = `__wepy_require(${moduleId})`;
1313
} else {
14-
if (typeof moduleId === 'number') {
15-
let npmfile = path.join(this.context, this.options.src, 'vendor.js');
16-
let relativePath = path.relative(path.dirname(parsed.file), npmfile);
17-
replaceMent = `require('${relativePath}')(${moduleId})`;
18-
} else if (depMod && depMod.sfc) {
14+
if (typeof depMod === 'object' && depMod.type !== 'npm') {
1915
let relativePath = path.relative(path.dirname(parsed.file), depMod.file);
20-
let reg = new RegExp('\\' + this.options.wpyExt + '$', 'i');
21-
relativePath = relativePath.replace(reg, '.js');
2216
replaceMent = `require('${relativePath}')`;
23-
} else if (depMod === false) {
24-
replaceMent = '{}';
2517
} else {
26-
replaceMent = `require('${dep.module}')`;
18+
if (typeof moduleId === 'number') {
19+
let npmfile = path.join(this.context, this.options.src, 'vendor.js');
20+
let relativePath = path.relative(path.dirname(parsed.file), npmfile);
21+
replaceMent = `require('${relativePath}')(${moduleId})`;
22+
} else if (depMod && depMod.sfc) {
23+
let relativePath = path.relative(path.dirname(parsed.file), depMod.file);
24+
let reg = new RegExp('\\' + this.options.wpyExt + '$', 'i');
25+
relativePath = relativePath.replace(reg, '.js');
26+
replaceMent = `require('${relativePath}')`;
27+
} else if (depMod === false) {
28+
replaceMent = '{}';
29+
} else {
30+
replaceMent = `require('${dep.module}')`;
31+
}
2732
}
2833
}
2934
parsed.source.replace(dep.expr.start, dep.expr.end, replaceMent);
-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
test
21
node_modules

packages/wepy-compiler-less/package.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "wepy-compiler-less",
3-
"version": "1.3.11",
2+
"name": "@wepy/compiler-less",
3+
"version": "2.0.0",
44
"description": "wepy compile less",
55
"main": "index.js",
66
"scripts": {
@@ -10,9 +10,11 @@
1010
"less"
1111
],
1212
"dependencies": {
13-
"less": "^2.7.1",
1413
"loader-utils": "^1.1.0"
1514
},
15+
"peerDependencies": {
16+
"less": "^2.7.1 || ^3.5.0"
17+
},
1618
"author": "Gcaufy",
1719
"license": "MIT",
1820
"devDependencies": {

0 commit comments

Comments
 (0)