Skip to content

Commit

Permalink
fix: fixed assets build
Browse files Browse the repository at this point in the history
  • Loading branch information
Gcaufy committed Jul 13, 2018
1 parent b0508d7 commit 5fc76ff
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 68 deletions.
2 changes: 1 addition & 1 deletion packages/wepy-cli/core/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Compile extends Hook {
this.options.entry = path.resolve(path.join(this.options.src, ENTRY_FILE));

this.compiled = {};
this.npm = new moduleSet();
this.vendors = new moduleSet();
this.assets = new moduleSet();
this.resolvers = {};

Expand Down
8 changes: 8 additions & 0 deletions packages/wepy-cli/core/moduleSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ class ModuleSet {
}
}

type (v) {
if (typeof v === 'number') {
return this._type[this._array[v]];
} else {
return this._type[v];
}
}

}


Expand Down
26 changes: 13 additions & 13 deletions packages/wepy-cli/core/plugins/build/vendor.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,27 @@ exports = module.exports = function () {

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

let npmList = this.npm.array();
let npmCode = '';
let vendorList = this.vendors.array();
let code = '';

npmList.forEach((npm, i) => {
let data = this.npm.data(npm);
vendorList.forEach((item, i) => {
let data = this.vendors.data(item);

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

npmCode += '/***** module ' + i + ' start *****/\n';
npmCode += '/***** ' + data.file + ' *****/\n';
npmCode += 'function(module, exports, __wepy_require) {';
npmCode += data.source.source() + '\n';
npmCode += '}';
if (i !== npmList.length - 1) {
npmCode += ',';
code += '/***** module ' + i + ' start *****/\n';
code += '/***** ' + data.file + ' *****/\n';
code += 'function(module, exports, __wepy_require) {';
code += data.source.source() + '\n';
code += '}';
if (i !== vendorList.length - 1) {
code += ',';
}
npmCode += '/***** module ' + i + ' end *****/\n\n\n';
code += '/***** module ' + i + ' end *****/\n\n\n';
});

let template = VENDOR_INJECTION.concat([]);
template[1] = npmCode;
template[1] = code;

vendor.outputCode = template.join('');
vendor.targetFile = path.join(this.context, this.options.target, 'vendor.js');
Expand Down
48 changes: 8 additions & 40 deletions packages/wepy-cli/core/plugins/parser/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,40 +72,6 @@ function ast (source) {
return ast;
}

function parseDep (issuer, dep) {
return this.compilation.resolvers.normal.resolve({issuer: issuer}, path.dirname(issuer), dep.module, {}).then(rst => {
let npm = rst.meta.descriptionFileRoot !== this.compilation.context;

let assets = this.compilation.assets;

if (!rst.path) {
return rst.path;
}
let id = assets.get(rst.path);
if (id !== undefined) {
return id;
}


let ext = path.extname(rst.path);

if (ext === '.js') {
if (npm) {
return this.parse({
code: fs.readFileSync(rst.path, 'utf-8')
}, {
file: rst.path,
npm: npm
});
}
} else if (ext === this.compilation.options.wpyExt) {
return this.compilation.parsers.wpy.parse(rst.path);
}
}).catch(e => {
console.error(e);
});
}

exports = module.exports = function () {

this.register('wepy-parser-dep', function (node, ctx, dep) {
Expand Down Expand Up @@ -155,10 +121,10 @@ exports = module.exports = function () {
let assets = this.assets;
let npmModules = this.npm;
if (ctx.npm) {
if (assets.pending(ctx.file)) {
return Promise.resolve(assets.get(ctx.file));
if (this.vendors.pending(ctx.file)) {
return Promise.resolve(this.vendors.get(ctx.file));
}
assets.add(ctx.file, 'npm');
this.vendors.add(ctx.file, 'npm');
}

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

return Promise.all(depTasks).then(rst => {
let type = ctx.npm ? 'npm' : (ctx.sfc ? 'component' : 'require');
let obj = {
file: ctx.file,
parser: walker,
code: node.compiled.code,
source: source,
depModules: rst
depModules: rst,
type: type
};
let assets = this.assets;
assets.update(ctx.file, obj, ctx.npm ? 'npm' : (ctx.sfc ? 'app' : 'require'));
let assets = ctx.npm ? this.vendors : this.assets;
assets.update(ctx.file, obj, type);
obj.id = assets.get(ctx.file);
return obj;
});
Expand Down
25 changes: 15 additions & 10 deletions packages/wepy-cli/core/plugins/scriptDepFix.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,24 @@ exports = module.exports = function () {
if (isNPM) {
replaceMent = `__wepy_require(${moduleId})`;
} else {
if (typeof moduleId === 'number') {
let npmfile = path.join(this.context, this.options.src, 'vendor.js');
let relativePath = path.relative(path.dirname(parsed.file), npmfile);
replaceMent = `require('${relativePath}')(${moduleId})`;
} else if (depMod && depMod.sfc) {
if (typeof depMod === 'object' && depMod.type !== 'npm') {
let relativePath = path.relative(path.dirname(parsed.file), depMod.file);
let reg = new RegExp('\\' + this.options.wpyExt + '$', 'i');
relativePath = relativePath.replace(reg, '.js');
replaceMent = `require('${relativePath}')`;
} else if (depMod === false) {
replaceMent = '{}';
} else {
replaceMent = `require('${dep.module}')`;
if (typeof moduleId === 'number') {
let npmfile = path.join(this.context, this.options.src, 'vendor.js');
let relativePath = path.relative(path.dirname(parsed.file), npmfile);
replaceMent = `require('${relativePath}')(${moduleId})`;
} else if (depMod && depMod.sfc) {
let relativePath = path.relative(path.dirname(parsed.file), depMod.file);
let reg = new RegExp('\\' + this.options.wpyExt + '$', 'i');
relativePath = relativePath.replace(reg, '.js');
replaceMent = `require('${relativePath}')`;
} else if (depMod === false) {
replaceMent = '{}';
} else {
replaceMent = `require('${dep.module}')`;
}
}
}
parsed.source.replace(dep.expr.start, dep.expr.end, replaceMent);
Expand Down
1 change: 0 additions & 1 deletion packages/wepy-compiler-less/.npmignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
test
node_modules
8 changes: 5 additions & 3 deletions packages/wepy-compiler-less/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wepy-compiler-less",
"version": "1.3.11",
"name": "@wepy/compiler-less",
"version": "2.0.0",
"description": "wepy compile less",
"main": "index.js",
"scripts": {
Expand All @@ -10,9 +10,11 @@
"less"
],
"dependencies": {
"less": "^2.7.1",
"loader-utils": "^1.1.0"
},
"peerDependencies": {
"less": "^2.7.1 || ^3.5.0"
},
"author": "Gcaufy",
"license": "MIT",
"devDependencies": {
Expand Down

0 comments on commit 5fc76ff

Please sign in to comment.