Skip to content

Commit

Permalink
feat: 引入taro
Browse files Browse the repository at this point in the history
  • Loading branch information
ximing committed Mar 19, 2019
1 parent 8318c70 commit 068b82f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
7 changes: 2 additions & 5 deletions src/loader/postcss-loader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ const path = require('path');
const validateOptions = require('schema-utils');
const postcss = require('postcss');
const postcssrc = require('postcss-load-config');
const workerpool = require('workerpool');

const pool = workerpool.pool(`${__dirname}/worker.js`);

const Warning = require('./Warning.js');
const SyntaxError = require('./Error.js');
const parseOptions = require('./options.js');
Expand Down Expand Up @@ -110,7 +106,8 @@ async function loader(asset, options = {}) {

messages.forEach((msg) => {
if (msg.type === 'dependency') {
this.addDependency(msg.file);
console.error('[postcss addDependency]', msg.file);
// this.addDependency(msg.file);
}
});

Expand Down
24 changes: 7 additions & 17 deletions src/loader/taro-js-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ const template = require('babel-template');

const taroJsFramework = '@tarojs/taro';
const taroJsComponents = '@tarojs/components';
const config = {
src: 'src',
dest: 'dist',
cwd: process.cwd()
};
const Asset = require('../asset');

const PARSE_AST_TYPE = {
ENTRY: 'ENTRY',
PAGE: 'PAGE',
Expand Down Expand Up @@ -458,14 +455,9 @@ function parseAst(type, ast, depComponents, sourceFilePath) {

module.exports = async function(asset, opts) {
const { path: sourcePath, outputFilePath, contents } = asset;
// console.log('start');
try {
// const pageWXMLPath = assetPath.replace(p.extname(assetPath), wxml);
// const outputPath = p.join(
// config.cwd,
// config.dest,
// assetPath.replace(p.join(config.cwd, config.src), '')
// );
const pageWXMLPath = sourcePath.replace(path.extname(sourcePath), '.wxml');
const outputPath = path.join(this.dest, pageWXMLPath.replace(this.src, ''));
if (contents.indexOf(taroJsFramework) >= 0) {
console.log('handle taro file');
const baseOptions = {
Expand All @@ -483,15 +475,13 @@ module.exports = async function(asset, opts) {
})
);
const parseRes = parseAst(PARSE_AST_TYPE.PAGE, ast, pageDepComponents);
// fse.outputFileSync(outputPath.replace(p.extname(sourcePath), wxml), compressedTemplate, {
// encoding
// });
// console.log('code', parseRes.code);
asset.contents = parseRes.code;
const wxmlAsset = new Asset(pageWXMLPath, outputPath, { virtual_file: true });
wxmlAsset.contents = compressedTemplate;
await this.assetManager.addAsset(wxmlAsset);
}
} catch (e) {
console.error(e);
}
// console.log('end');
return asset;
};
25 changes: 21 additions & 4 deletions src/plugin/minifyPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,27 @@ module.exports = class MinifyPlugin {
if (mpb.optimization.minimize) {
mpb.hooks.beforeEmitFile.tapPromise('MinifyPlugin', async (asset) => {
if (asset.contents) {
asset.contents = await pool.exec(minify, [
asset.contents,
asset.outputFilePath
]);
// if (/\.js$/.test(asset.outputFilePath)) {
// const result = UglifyJS.minify(asset.contents);
// if (result.error) console.error('[MinifyPlugin]', result.error);
// if (result.warnings) console.warn('[MinifyPlugin]', result.warnings);
// asset.contents = result.code;
// } else if (/\.json$/.test(asset.outputFilePath)) {
// asset.contents = jsonminify(asset.contents).toString();
// } else if (/\.wxml$/.test(asset.outputFilePath)) {
// asset.contents = asset.contents = htmlmin.minify(asset.contents, {
// removeComments: true,
// keepClosingSlash: true,
// collapseWhitespace: true,
// caseSensitive: true
// });
// }
if (/\.(js|json|wxml)$/.test(asset.outputFilePath)) {
asset.contents = await pool.exec(minify, [
asset.contents,
asset.outputFilePath
]);
}
}
return Promise.resolve();
});
Expand Down

0 comments on commit 068b82f

Please sign in to comment.