Skip to content

Commit

Permalink
feat: 支持 json 继承
Browse files Browse the repository at this point in the history
  • Loading branch information
ximing committed May 28, 2019
1 parent 80c72d2 commit ff30f50
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
12 changes: 5 additions & 7 deletions src/asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,27 +87,25 @@ module.exports = class Asset {
return this.mtime !== asset.mtime;
}

render(targetPath, mpb) {
if (targetPath || this.outputFilePath) {
render(mpb) {
if (this.outputFilePath) {
if (this.__content != null) {
if (mpb.hasInit && mpb.isWatch) {
console.log(chalk.cyan('[watching-output]'), this.outputFilePath);
}
// TODO 做一个
// if (this.outputFilePath.includes('/mnt/d/project/mall-wxapp/dist')) {
// return fse.outputFile(targetPath || this.outputFilePath, this.contents);
// return fse.outputFile(this.outputFilePath, this.contents);
// }
// return Promise.reject(new Error('dist not in project: ' + this.outputFilePath));
return fse.outputFile(targetPath || this.outputFilePath, this.contents);
return fse.outputFile(this.outputFilePath, this.contents);
}
if (mpb.hasInit && mpb.isWatch) {
console.log('[watch]:文件内容为空,不输出', this.outputFilePath);
}
return Promise.resolve(this);
}
return Promise.reject(
new Error(`[asset.js] emit ${this.path} must have targetPath or outputFilePath`)
);
return Promise.reject(new Error(`[asset.js] emit ${this.path} must have outputFilePath`));
}

del() {
Expand Down
2 changes: 1 addition & 1 deletion src/assetManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ module.exports = class AssetManager {
}

emitFile(asset) {
asset.render(null, this.mpb).catch((err) => {
asset.render(this.mpb).catch((err) => {
console.error(err);
});
}
Expand Down
28 changes: 28 additions & 0 deletions src/loader/json-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const path = require('path');
const _ = require('lodash');

module.exports = function(opts) {
return function replaceLoader(asset) {
try {
let contents = JSON.parse(asset.contents);
if (contents.extends) {
if (typeof contents.extends === 'string') {
let filePath = '';
if (contents.extends[0] === '.') {
filePath = path.resolve(asset.dir, contents.extends);
} else {
filePath = path.join(this.src, contents.extends);
}
contents = _.merge({}, contents, require(filePath));
delete contents.extends;
asset.contents = JSON.stringify(contents);
} else {
console.error('[json-loader] extends 必须为字符串');
}
}
} catch (e) {
console.error('[json-loader]', e);
}
return asset;
};
};
4 changes: 3 additions & 1 deletion src/loaderManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ const replaceLoader = require('./loader/replace-loader');
const tsLoader = require('./loader/ts-loader');
const tsLoaderNext = require('./loader/ts-loader-next');
const postcssLoader = require('./loader/postcss-loader');
const jsonLoader = require('./loader/json-loader');

const map = {
'babel-loader': babelLoader,
'file-loader': fileLoader,
'replace-loader': replaceLoader,
'ts-loader': tsLoader,
'json-loader': jsonLoader,
'ts-loader-next': tsLoaderNext,
'postcss-loader': postcssLoader
};
Expand Down Expand Up @@ -64,7 +66,7 @@ module.exports = class LoaderManager {
this.rules = [];
for (let i = this.mpb.config.module.rules.length - 1; i >= 0; i--) {
const rule = this.mpb.config.module.rules[i];
let { use, test, exclude, include } = rule;
const { use, test, exclude, include } = rule;
for (let j = use.length - 1; j >= 0; j--) {
const { loader, options } = use[j];
if (!use[j].loaderInstance) {
Expand Down
2 changes: 1 addition & 1 deletion src/scan.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ module.exports = class ScanDep {

async init() {
perf.start('init');
this.findEntry();
await this.findEntry();
// find App
await this.addAssetByEXT('app', path.join(this.mpb.dest, 'app'), assetType.app);
// find Pages
Expand Down

0 comments on commit ff30f50

Please sign in to comment.