Skip to content

Commit

Permalink
fix: 支持asset自定义 target
Browse files Browse the repository at this point in the history
  • Loading branch information
ximing committed Apr 1, 2019
1 parent 6f8e016 commit 04b3339
Show file tree
Hide file tree
Showing 15 changed files with 501 additions and 263 deletions.
8 changes: 6 additions & 2 deletions src/asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
const path = require('path');
const fs = require('fs');
const fse = require('fs-extra');
const { assetType } = require('./consts');

module.exports = class Asset {
constructor(filePath, outputFilePath, meta) {
Expand All @@ -15,7 +16,10 @@ module.exports = class Asset {
this.path = filePath;
this.filePath = filePath;
this.__content = null;
this.__meta = meta || {};
this.__meta = meta || {
type: assetType.normal
};
this.shouldOutput = true;
this.outputFilePath = outputFilePath;
}

Expand Down Expand Up @@ -84,7 +88,7 @@ module.exports = class Asset {

render(targetPath) {
if (targetPath || this.outputFilePath) {
if (this.contents != null) {
if (this.__content != null) {
// TODO 做一个
// if (this.outputFilePath.includes('/mnt/d/project/mall-wxapp/dist')) {
// return fse.outputFile(targetPath || this.outputFilePath, this.contents);
Expand Down
31 changes: 20 additions & 11 deletions src/assetManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Created by ximing on 2019-03-15.
*/
const chalk = require('chalk');
const fse = require('fs-extra');

const Asset = require('./asset');
const log = require('./log');
Expand Down Expand Up @@ -45,17 +46,25 @@ module.exports = class AssetManager {
// 更新asset
this.setAsset(asset);
return this.mpb.hooks.addAsset.promise(asset).then(
(asset) =>
this.mpb.hooks.beforeEmitFile.promise(asset).then(
() => {
this.emitFile(asset);
return asset;
},
(err) => {
console.error(err);
// throw err;
}
),
(asset) => {
if (asset.shouldOutput) {
return this.mpb.hooks.beforeEmitFile.promise(asset).then(
() => {
this.emitFile(asset);
return asset;
},
(err) => {
console.error(err);
// throw err;
}
);
}
// 如果不输出,就删除之前的构建文件,这样小程序工具上直观能体现出来有代码有问题了
return fse
.remove(asset.outputFilePat)
.then((_) => asset)
.catch((_) => asset);
},
(err) => {
console.error(err);
// throw err;
Expand Down
9 changes: 9 additions & 0 deletions src/consts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Created by ximing on 2019-03-29.
*/
module.exports.assetType = {
page: 'page',
component: 'component',
app: 'app',
normal: 'normal'
};
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class Mpbuilder {
this.initPlugin();
this.assetManager = new AssetManager(this);
this.hasInit = false;
this.isWatch = false;
}

initPlugin() {
Expand Down Expand Up @@ -90,13 +91,15 @@ class Mpbuilder {
}

async watch() {
this.isWatch = true;
await this.run();
log.info('开启watching');
this.watching.watch();
this.watching.watchEntry();
}

async run() {
await this.loaderManager.initRules();
await this.scan.run();
this.hasInit = true;
}
Expand Down
34 changes: 19 additions & 15 deletions src/loader/babel-loader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@
*/
const babel = require('@babel/core');

module.exports = function(file, opts) {
opts = opts || {};
const fileOpts = Object.assign({}, opts, {
filename: file.path,
filenameRelative: file.relative,
sourceMap: Boolean(file.sourceMap),
sourceFileName: file.relative,
caller: Object.assign({ name: 'babel-mbp' }, opts.caller)
});
return babel.transformAsync(file.contents, fileOpts).then((res) => {
if (res) {
file.contents = res.code;
file.babel = res.metadata;
module.exports = function(opts = {}) {
return function(file) {
if (file.contents) {
const fileOpts = Object.assign({}, opts, {
filename: file.path,
filenameRelative: file.relative,
sourceMap: Boolean(file.sourceMap),
sourceFileName: file.relative,
caller: Object.assign({ name: 'babel-mbp' }, opts.caller)
});
return babel.transformAsync(file.contents, fileOpts).then((res) => {
if (res) {
file.contents = res.code;
file.babel = res.metadata;
}
return file;
});
}
return file;
});
return Promise.resolve(file);
};
};
14 changes: 8 additions & 6 deletions src/loader/file-loader.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/**
* Created by ximing on 2019-03-14.
*/
module.exports = async function(asset, opts) {
const { helper, config } = this.mpb;
console.log('outputFilePath', asset.outputFilePath);
// 触发编译
await asset.render();
return asset;
module.exports = function() {
return async function(asset) {
const { helper, config } = this;
console.log('outputFilePath', asset.outputFilePath);
// 触发编译
await asset.render();
return asset;
};
};
Loading

0 comments on commit 04b3339

Please sign in to comment.