Skip to content

Commit

Permalink
feat(okam-build): only ouput used resource files and by default all i…
Browse files Browse the repository at this point in the history
…mage files will all be released
  • Loading branch information
wuhy committed Nov 27, 2018
1 parent 8a63648 commit 0097649
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 68 deletions.
43 changes: 35 additions & 8 deletions packages/okam-build/lib/build/BuildManager.js
Expand Up @@ -312,17 +312,13 @@ class BuildManager extends EventEmitter {
}

/**
* Start to build app
* Build dependencies files
*
* @param {Timer} timer the build timer
* @return {Promise}
* @param {Timer} t the build timer
* @return {boolean}
*/
build(timer) {
let logger = this.logger;

let t = new Timer();
buildDependencies(t) {
let buildFail = false;

// build files that need to compile
let waitingBuildFiles = this.waitingBuildFiles;
while (waitingBuildFiles.length) {
Expand All @@ -333,6 +329,37 @@ class BuildManager extends EventEmitter {
}
}

return buildFail;
}

/**
* Start to build app
*
* @param {Timer} timer the build timer
* @return {Promise}
*/
build(timer) {
let logger = this.logger;
let t = new Timer();

// build files that need to compile
let buildFail = this.buildDependencies(t);
if (!buildFail) {
// build the rest of files that are not processed
let fileList = this.files.getFileList();
for (let i = 0, len = fileList.length; i < len; i++) {
let f = fileList[i];
if (!f.processed && !this.compile(f, t)) {
buildFail = true;
break;
}
}
}

if (!buildFail) {
buildFail = this.buildDependencies(t);
}

if (buildFail) {
return Promise.reject('error happen');
}
Expand Down
7 changes: 3 additions & 4 deletions packages/okam-build/lib/build/load-process-files.js
Expand Up @@ -135,10 +135,9 @@ function loadProcessFiles(options, logger) {
) {
initBuildFiles.unshift(toProcessFile);
}
else if (!toProcessFile.isScript
&& !toProcessFile.isStyle
&& !toProcessFile.isJson
) {
else if (toProcessFile.isImg) {
// by default all image files will be processed and output as for
// we cannot analysis the used image resources correctly
initBuildFiles.push(toProcessFile);
}

Expand Down
10 changes: 3 additions & 7 deletions packages/okam-build/lib/config/base.js
Expand Up @@ -204,12 +204,8 @@ module.exports = {
* @return {boolean|string}
*/
file(path, file) {
if (file.isStyle && file.extname !== 'css' && !file.compiled) {
return false;
}

// do not output sfc file component
if (file.isComponent) {
// do not output not compiled file and sfc file component
if (!file.compiled || file.isComponent) {
return false;
}

Expand Down Expand Up @@ -388,7 +384,7 @@ module.exports = {
* @return {boolean}
*/
match(file) {
if (file.isStyle && !(file.isEntryStyle || file.owner)) {
if (file.isStyle && !file.isEntryStyle && !file.owner) {
// 默认不处理非入口样式及单文件组件的样式文件
return false;
}
Expand Down
7 changes: 2 additions & 5 deletions packages/okam-build/lib/config/quick.js
Expand Up @@ -69,11 +69,8 @@ module.exports = merge({}, baseConf, {
* @return {boolean|string}
*/
file(path, file) {
if (file.isStyle && file.extname !== 'css' && !file.compiled) {
return false;
}

if (file.isComponentConfig) {
// do not output not compiled file and component config file
if (!file.compiled || file.isComponentConfig) {
return false;
}

Expand Down
14 changes: 3 additions & 11 deletions packages/okam-build/lib/generator/FileOutput.js
Expand Up @@ -109,12 +109,7 @@ function getComponentOutputFilePath(partFile, owner, options) {
partFile.rext = componentPartExtname.tpl;
}

let outputPath = getOutputPath(owner.path, partFile, options);
if (!outputPath) {
return;
}

return outputPath;
return getOutputPath(owner.path, partFile, options);
}

function mergeComponentStyleFiles(styleFiles, rootDir) {
Expand Down Expand Up @@ -149,19 +144,16 @@ function addFileOutputTask(allTasks, options, file) {
return;
}

let {outputDir} = options;
let {outputDir, logger} = options;
let ownerFile = file.owner;
let outputRelPath = isComponentFile(ownerFile)
? getComponentOutputFilePath(file, ownerFile, options)
: getOutputPath(file.path, file, options);
if (!outputRelPath) {
logger.debug('skip file release', file.path);
return;
}

if (!file.compiled) {
this.logger.debug('file is not compiled:', file.path);
}

allTasks.push(
outputFile(file, path.join(outputDir, outputRelPath), options.logger)
);
Expand Down
12 changes: 8 additions & 4 deletions packages/okam-build/lib/processor/index.js
Expand Up @@ -85,8 +85,8 @@ function processComponentScript(buildManager, file, root) {
if (jsonFile) {
jsonFile.component = file;
jsonFile.isComponentConfig = true;
compile(jsonFile, buildManager);
}
compile(jsonFile, buildManager);
}

/**
Expand Down Expand Up @@ -118,10 +118,10 @@ function processFile(file, processor, buildManager) {

if (result.isComponent) {
compileComponent(result, file, buildManager);
result = {content: file.content};
}
else {
buildManager.updateFileCompileResult(file, result);
}

buildManager.updateFileCompileResult(file, result);
}

/**
Expand All @@ -135,6 +135,8 @@ function compile(file, buildManager) {
let processors = findMatchProcessor(file, rules, buildManager);
logger.debug('compile file:', file.path, processors.length);

// add flag that standard for this file is processed
file.processed = true;
for (let i = 0, len = processors.length; i < len; i++) {
processFile(file, processors[i], buildManager);
}
Expand All @@ -145,6 +147,8 @@ function compile(file, buildManager) {
else if (file.isPageScript || file.isComponentScript) {
processComponentScript(buildManager, file, root);
}

buildManager.emit('buildFileDone', file);
}

/**
Expand Down
19 changes: 17 additions & 2 deletions packages/okam-build/lib/processor/template/index.js
Expand Up @@ -7,6 +7,8 @@

/* eslint-disable fecs-min-vars-per-destructure */
/* eslint-disable fecs-prefer-destructure */
const path = require('path');
const {file: fileUtil} = require('../../util/index');
const {parse: parseDom} = require('./parser');
const serializeDom = require('./serializer');

Expand Down Expand Up @@ -155,24 +157,37 @@ function mergeVisitors(plugins) {
* @return {Object}
*/
function compileTpl(file, options) {
let {config} = options;
let {root, config, logger} = options;
let allowCache = !config || config.cache == null || config.cache;
let content = file.content.toString();
const ast = file.ast || parseDom(content);
allowCache && (file.ast = ast);

let plugins = mergeVisitors((config && config.plugins) || []);

let deps = [];
let addDep = function (filePath) {
let relativePath = fileUtil.relative(
path.join(path.dirname(file.fullPath), filePath),
root
);
if (!deps.includes(relativePath)) {
deps.push(relativePath);
}
logger.debug('find tpl dep file', relativePath);
};

transformAst(
ast, plugins,
Object.assign({}, options, {file})
Object.assign({}, options, {file, addDep})
);

// serialize by xml mode, close all elements
content = serializeDom(ast, {xmlMode: true});

return {
ast,
deps,
content
};
}
Expand Down
Expand Up @@ -14,11 +14,12 @@ const transformEnvElement = require('./env');
const {CONDITION_DIRECTIVES, ENV_ELEMENT_REGEXP} = require('./constant');

function transformIncludeImportElement(element, tplOpts) {
let {output: outputOpts} = tplOpts;
let {addDep, output: outputOpts} = tplOpts;
let {attribs: attrs} = element;
let src = attrs && attrs.src;
let tplExtname = outputOpts.componentPartExtname.tpl;
if (src) {
addDep(src);
// change included tpl file path extname to mini program template extname
attrs.src = src.replace(/\.\w+$/, '.' + tplExtname);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/okam-build/lib/processor/type.js
Expand Up @@ -12,7 +12,7 @@ const STYLE_EXT_NAMES = ['css', 'less', 'styl', 'sass', 'scss'];
const SCRIPT_EXT_NAMES = ['js', 'es', 'es6', 'ts', 'coffee'];
const TEMPLATE_EXT_NAMES = ['html', 'tpl', 'etpl', 'art', 'jade', 'pug'];
const JSON_EXT_NAMES = ['json', 'json5'];
const IMG_EXT_NAMES = ['png', 'gif', 'jpeg', 'jpg', 'webp'];
const IMG_EXT_NAMES = ['png', 'gif', 'jpeg', 'jpg', 'webp', 'svg'];

function getProcessorPath(type) {
return path.join(__dirname, type);
Expand Down
40 changes: 15 additions & 25 deletions packages/okam-build/lib/watch/watch-handler.js
Expand Up @@ -34,50 +34,40 @@ function compileFile(buildManager, file, releaseFiles) {
// TODO init dep map global to search file by dep effectively
let changeFiles = buildManager.getFilesByDep(file.path);
logger.debug(file.path, 'changeFiles:' + changeFiles.length);

if (changeFiles.length) {
changeFiles.forEach(item => {
compileFile(
buildManager, item, releaseFiles
);
});
changeFiles.forEach(
item => compileFile(buildManager, item, releaseFiles)
);
return;
}
}

let result = buildManager.compile(file);
if (!result) {
return;
if (result) {
buildManager.buildDependencies();
}

releaseFiles.add(file);

// process script deps
file.isScript && (file.deps || []).forEach(depPath => {
let depFile = buildManager.getFileByPath(depPath);
logger.debug('process dep', file.path, depPath, !!depFile, depFile && depFile.compiled);
if (!depFile || !depFile.compiled) {
compileFile(buildManager, depPath, releaseFiles);
}
});

(file.subFiles || []).forEach(
subFile => releaseFiles.add(subFile)
);
}

function rebuildFiles(file, buildManager) {
let timer = new Timer();
timer.start();

let outputFiles = [];
let addReleaseFile = function (file) {
if (!outputFiles.includes(file)) {
outputFiles.push(file);
}
};
let releaseFiles = {
processFileNum: 0,
add(file) {
outputFiles.push(file);
},
add: addReleaseFile,
processed: {}
};

buildManager.on('buildFileDone', addReleaseFile);
compileFile(buildManager, file, releaseFiles);
buildManager.removeListener('buildFileDone', addReleaseFile);

if (outputFiles.length) {
let logger = buildManager.logger;
Expand Down

0 comments on commit 0097649

Please sign in to comment.