diff --git a/packages/okam-build/lib/build/BuildManager.js b/packages/okam-build/lib/build/BuildManager.js index 139b60a4..38a12e1f 100644 --- a/packages/okam-build/lib/build/BuildManager.js +++ b/packages/okam-build/lib/build/BuildManager.js @@ -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) { @@ -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'); } diff --git a/packages/okam-build/lib/build/load-process-files.js b/packages/okam-build/lib/build/load-process-files.js index 170588cc..83c16e78 100644 --- a/packages/okam-build/lib/build/load-process-files.js +++ b/packages/okam-build/lib/build/load-process-files.js @@ -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); } diff --git a/packages/okam-build/lib/config/base.js b/packages/okam-build/lib/config/base.js index 965ce030..f1a6aa18 100644 --- a/packages/okam-build/lib/config/base.js +++ b/packages/okam-build/lib/config/base.js @@ -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; } @@ -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; } diff --git a/packages/okam-build/lib/config/quick.js b/packages/okam-build/lib/config/quick.js index 43fbf6bc..0bbcc67f 100644 --- a/packages/okam-build/lib/config/quick.js +++ b/packages/okam-build/lib/config/quick.js @@ -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; } diff --git a/packages/okam-build/lib/generator/FileOutput.js b/packages/okam-build/lib/generator/FileOutput.js index 9d736d18..4ec41ce3 100644 --- a/packages/okam-build/lib/generator/FileOutput.js +++ b/packages/okam-build/lib/generator/FileOutput.js @@ -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) { @@ -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) ); diff --git a/packages/okam-build/lib/processor/index.js b/packages/okam-build/lib/processor/index.js index 97cf7e2a..ff39dd98 100644 --- a/packages/okam-build/lib/processor/index.js +++ b/packages/okam-build/lib/processor/index.js @@ -85,8 +85,8 @@ function processComponentScript(buildManager, file, root) { if (jsonFile) { jsonFile.component = file; jsonFile.isComponentConfig = true; + compile(jsonFile, buildManager); } - compile(jsonFile, buildManager); } /** @@ -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); } /** @@ -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); } @@ -145,6 +147,8 @@ function compile(file, buildManager) { else if (file.isPageScript || file.isComponentScript) { processComponentScript(buildManager, file, root); } + + buildManager.emit('buildFileDone', file); } /** diff --git a/packages/okam-build/lib/processor/template/index.js b/packages/okam-build/lib/processor/template/index.js index 06212025..6389afa2 100644 --- a/packages/okam-build/lib/processor/template/index.js +++ b/packages/okam-build/lib/processor/template/index.js @@ -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'); @@ -155,7 +157,7 @@ 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); @@ -163,9 +165,21 @@ function compileTpl(file, options) { 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 @@ -173,6 +187,7 @@ function compileTpl(file, options) { return { ast, + deps, content }; } diff --git a/packages/okam-build/lib/processor/template/transform/base/element.js b/packages/okam-build/lib/processor/template/transform/base/element.js index 0522c91d..ef9bae49 100644 --- a/packages/okam-build/lib/processor/template/transform/base/element.js +++ b/packages/okam-build/lib/processor/template/transform/base/element.js @@ -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); } diff --git a/packages/okam-build/lib/processor/type.js b/packages/okam-build/lib/processor/type.js index aa261db5..9dc5eefe 100644 --- a/packages/okam-build/lib/processor/type.js +++ b/packages/okam-build/lib/processor/type.js @@ -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); diff --git a/packages/okam-build/lib/watch/watch-handler.js b/packages/okam-build/lib/watch/watch-handler.js index 71ed5a61..60037ba2 100644 --- a/packages/okam-build/lib/watch/watch-handler.js +++ b/packages/okam-build/lib/watch/watch-handler.js @@ -34,35 +34,19 @@ 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) { @@ -70,14 +54,20 @@ function rebuildFiles(file, buildManager) { 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;