diff --git a/packages/okam-build/lib/build/BuildManager.js b/packages/okam-build/lib/build/BuildManager.js index 8e0722c1..96afd480 100644 --- a/packages/okam-build/lib/build/BuildManager.js +++ b/packages/okam-build/lib/build/BuildManager.js @@ -43,9 +43,8 @@ class BuildManager extends EventEmitter { this.runningTasks = []; this.doneTasks = []; - this.reBuildFiles = []; + this.waitingBuildFiles = []; this.cache = new CacheManager({cacheDir: buildConf.cacheDir}); - this.generator = new FileOutput(this, buildConf.output); } initProcessor(buildConf) { @@ -119,34 +118,56 @@ class BuildManager extends EventEmitter { let { root, sourceDir, - files + files, + buildFiles } = loadProcessFiles(this.buildConf, this.logger); this.files = files; this.root = root; this.sourceDir = sourceDir; this.babelConfig = babelUtil.readBabelConfig(root); + this.waitingBuildFiles = buildFiles; let {output} = this.buildConf; this.compileContext = { cache: this.cache, resolve: npm.resolve.bind(null, this), - addFile: this.files.addFile.bind(this.files), + addFile: this.addNewFile.bind(this), getFileByFullPath: this.getFileByFullPath.bind(this), appType: this.appType, logger: this.logger, root, output }; + + this.generator = new FileOutput(this, this.buildConf.output); + } + + /** + * Add new file to process + * + * @param {string} fullPath the new file full path to add + * @return {Object} + */ + addNewFile(fullPath) { + let result = this.files.addFile(fullPath); + this.addNeedBuildFile(result); + return result; } /** - * Add need to recompile file + * Add file that need to build * * @param {Object} file the file need to recompile + * @param {boolean=} force whether force recompile if the file is compiled, + * by default false */ - addNeedRecompileFile(file) { - let reBuilds = this.reBuildFiles; + addNeedBuildFile(file, force = false) { + if (!force && file.compiled) { + return; + } + + let reBuilds = this.waitingBuildFiles; if (reBuilds.indexOf(file) === -1) { reBuilds.push(file); } @@ -225,25 +246,14 @@ class BuildManager extends EventEmitter { build(timer) { let logger = this.logger; - let fileList = this.files.getFileList(); - let processIdx = 0; let t = new Timer(); let buildFail = false; - while (processIdx < fileList.length) { - let f = fileList[processIdx]; - if (!this.compile(f, t)) { - buildFail = true; - break; - } - processIdx++; - } - - // rebuild need to recompile files - let reBuildFiles = this.reBuildFiles; - while (reBuildFiles.length) { - let f = reBuildFiles.shift(); + // build files that need to compile + let waitingBuildFiles = this.waitingBuildFiles; + while (waitingBuildFiles.length) { + let f = waitingBuildFiles.shift(); if (!this.compile(f, t)) { buildFail = true; break; @@ -328,6 +338,7 @@ class BuildManager extends EventEmitter { let depFile = this.files.getByPath(item); depFile || (depFile = this.files.addFile({path: item})); file.addDeps(depFile.path); + this.addNeedBuildFile(depFile); }); sourceMap && (file.sourceMap = sourceMap); diff --git a/packages/okam-build/lib/build/index.js b/packages/okam-build/lib/build/index.js index 4daf5b89..e101eb63 100644 --- a/packages/okam-build/lib/build/index.js +++ b/packages/okam-build/lib/build/index.js @@ -72,7 +72,13 @@ function main(appType, options) { }); } - startBuild(buildConf); + try { + startBuild(buildConf); + } + catch (ex) { + logger.error(ex.message || ex.toString()); + logger.error(ex.stack); + } } module.exports = exports = main; diff --git a/packages/okam-build/lib/build/load-process-files.js b/packages/okam-build/lib/build/load-process-files.js index 553baa44..170588cc 100644 --- a/packages/okam-build/lib/build/load-process-files.js +++ b/packages/okam-build/lib/build/load-process-files.js @@ -78,6 +78,7 @@ function loadProcessFiles(options, logger) { return; } + let initBuildFiles = []; let componentExtname = options.component.extname; let {style: entryStyle, script: entryScript, projectConfig} = options.entry; entryStyle = resolvePath(entryStyle, rootDir); @@ -128,12 +129,20 @@ function loadProcessFiles(options, logger) { }); // ensure the entry app script can be compiled at first - if (toProcessFile.isEntryScript) { - processFiles.unshift(toProcessFile); + if (toProcessFile.isEntryScript + || toProcessFile.isEntryStyle + || toProcessFile.isProjectConfig + ) { + initBuildFiles.unshift(toProcessFile); } - else { - processFiles.push(toProcessFile); + else if (!toProcessFile.isScript + && !toProcessFile.isStyle + && !toProcessFile.isJson + ) { + initBuildFiles.push(toProcessFile); } + + processFiles.push(toProcessFile); }; logger.debug('load files', rootDir, sourceDir); @@ -142,7 +151,8 @@ function loadProcessFiles(options, logger) { return { root: rootDir, sourceDir, - files: processFiles + files: processFiles, + buildFiles: initBuildFiles }; } diff --git a/packages/okam-build/lib/generator/FileOutput.js b/packages/okam-build/lib/generator/FileOutput.js index 82395526..0997c485 100644 --- a/packages/okam-build/lib/generator/FileOutput.js +++ b/packages/okam-build/lib/generator/FileOutput.js @@ -142,6 +142,10 @@ function addFileOutputTask(allTasks, options, file) { 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/helper/init-babel.js b/packages/okam-build/lib/processor/helper/init-babel.js index 8e5d3d53..7346c373 100644 --- a/packages/okam-build/lib/processor/helper/init-babel.js +++ b/packages/okam-build/lib/processor/helper/init-babel.js @@ -27,7 +27,6 @@ function initMixinFile(mixins, buildManager, file) { let mixinFile = moduleFullPath && buildManager.getFileByFullPath(moduleFullPath); if (mixinFile && !mixinFile.isBehavior) { mixinFile.isBehavior = true; - mixinFile.compiled && buildManager.addNeedRecompileFile(mixinFile); } }); } diff --git a/packages/okam-build/lib/processor/helper/npm.js b/packages/okam-build/lib/processor/helper/npm.js index 650b9029..438739e9 100644 --- a/packages/okam-build/lib/processor/helper/npm.js +++ b/packages/okam-build/lib/processor/helper/npm.js @@ -62,8 +62,11 @@ exports.resolve = function (buildManager, file, requireModId) { let {files: fileFactory, logger} = buildManager; logger.debug('resolve', file.path, requireModId); + let depFile = fileFactory.addFile(depFileFullPath); + buildManager.addNeedBuildFile(depFile); file.addDeps(depFile.path); + file.isWxCompScript && (depFile.isWxCompScript = true); file.isSwanCompScript && (depFile.isSwanCompScript = true); file.isAntCompScript && (depFile.isAntCompScript = true); diff --git a/packages/okam-build/lib/processor/index.js b/packages/okam-build/lib/processor/index.js index 60b418b1..0acc6d84 100644 --- a/packages/okam-build/lib/processor/index.js +++ b/packages/okam-build/lib/processor/index.js @@ -32,7 +32,8 @@ function processConfigInfo(file, root, owner) { return jsonFile; } -function processEntryScript(file, allFiles, root, componentExtname) { +function processEntryScript(file, buildManager) { + let {root, files: allFiles, componentExtname} = buildManager; let appConfig = file.config || {}; file.config = appConfig; @@ -60,14 +61,18 @@ function processEntryScript(file, allFiles, root, componentExtname) { let pageFullPath = path.resolve(file.dirname, p) + '.' + componentExtname; let pageFile = allFiles.getByFullPath(pageFullPath); - pageFile && (pageFile.isPageComponent = true); + if (pageFile) { + pageFile.isPageComponent = true; + buildManager.addNeedBuildFile(pageFile); + } } ); let jsonFile = processConfigInfo(file, root, file); jsonFile.isAppConfig = true; - allFiles.push(jsonFile); + + buildManager.addNeedBuildFile(jsonFile); } function processComponentScript(buildManager, file, root) { @@ -123,7 +128,7 @@ function processFile(file, processor, buildManager) { * @param {BuildManager} buildManager the build manager */ function compile(file, buildManager) { - let {logger, root, rules, files: allFiles} = buildManager; + let {logger, root, rules} = buildManager; let processors = findMatchProcessor(file, rules, buildManager); logger.debug('compile file:', file.path, processors.length); @@ -132,7 +137,7 @@ function compile(file, buildManager) { } if (file.isEntryScript) { - processEntryScript(file, allFiles, root, buildManager.componentExtname); + processEntryScript(file, buildManager); } else if (file.isPageScript || file.isComponentScript) { processComponentScript(buildManager, file, root); @@ -142,6 +147,7 @@ function compile(file, buildManager) { /** * Get custom component tags * + * @inner * @param {Object} config the component config * @return {?Array.} */ diff --git a/packages/okam-build/lib/processor/json/component-json.js b/packages/okam-build/lib/processor/json/component-json.js index 1204e8d6..28410275 100644 --- a/packages/okam-build/lib/processor/json/component-json.js +++ b/packages/okam-build/lib/processor/json/component-json.js @@ -12,6 +12,22 @@ const {getFileName, getFileState} = fileUtil; const {toHyphen} = strUtil; const USING_COMPONENT_KEY = 'usingComponents'; +/** + * The native component file ext names + * + * @const + * @type {Object} + */ +const COMPONENT_FILE_EXT_NAMES = { + wxml: 'isWxCompScript', + swan: 'isSwanCompScript', + axml: 'isAntCompScript', + acss: false, + wxss: false, + css: false, + js: false +}; + /** * Initialize the file list information in the given directory. * Return the file info structure: @@ -62,7 +78,6 @@ function initDirFiles(dir, cache) { * @inner * @param {Object} scriptFile the component script file * @param {Object} options the process options - * @return {?Object} */ function addComponentSameNameFiles(scriptFile, options) { const {dirname: currDir, path: relPath} = scriptFile; @@ -70,21 +85,9 @@ function addComponentSameNameFiles(scriptFile, options) { const {cache, addFile, getFileByFullPath} = options; let cacheDirFiles = cache.getDirFileListInfo(currDir); - let hasWxmlFile; - let hasSwanFile; - let hasAxmlFile; let jsonFile; - if (!scriptFile.isNpm) { - let filePathBase = `${currDir}/${scriptFileName}`; - let jsonFilePath = `${filePathBase}.json`; - jsonFile = getFileByFullPath(jsonFilePath); - if (jsonFile && !jsonFile.owner) { - hasWxmlFile = !!getFileByFullPath(`${filePathBase}.wxml`); - hasSwanFile = !!getFileByFullPath(`${filePathBase}.swan`); - hasAxmlFile = !!getFileByFullPath(`${filePathBase}.axml`); - } - } - else { + let toAddFiles = []; + if (scriptFile.isNpm) { if (!cacheDirFiles) { cacheDirFiles = initDirFiles(currDir, cache); } @@ -95,33 +98,41 @@ function addComponentSameNameFiles(scriptFile, options) { let fileObj = addFile(sameNameFiles[i]); let extname = fileObj.extname; - if (extname === 'wxml') { - hasWxmlFile = true; - } - else if (extname === 'swan') { - hasSwanFile = true; - } - else if (extname === 'axml') { - hasAxmlFile = true; + let flagKey = COMPONENT_FILE_EXT_NAMES[extname]; + if (typeof flagKey === 'string') { + // add flag for native component script + scriptFile[flagKey] = true; } else if (fileObj.isJson) { jsonFile = fileObj; } } } + else { + let filePathBase = `${currDir}/${scriptFileName}`; + jsonFile = getFileByFullPath(`${filePathBase}.json`); - // add flag for npm component script - if (hasWxmlFile) { - scriptFile.isWxCompScript = true; - } - else if (hasSwanFile) { - scriptFile.isSwanCompScript = true; + if (jsonFile && !jsonFile.owner) { + Object.keys(COMPONENT_FILE_EXT_NAMES).forEach(k => { + let f = getFileByFullPath(`${filePathBase}.${k}`); + if (f) { + toAddFiles.push(f.fullPath); + let flagKey = COMPONENT_FILE_EXT_NAMES[k]; + // add flag for native component script + typeof flagKey === 'string' && (scriptFile[flagKey] = true); + } + }); + } } - else if (hasAxmlFile) { - scriptFile.isAntCompScript = true; + + if (jsonFile) { + jsonFile.isComponentConfig = true; + jsonFile.component = scriptFile; + + toAddFiles.push(jsonFile.fullPath); } - return jsonFile; + toAddFiles.forEach(item => addFile(item)); } /** @@ -139,11 +150,7 @@ function analyseNativeComponent(scriptFile, options) { scriptFile.isAnalysedComponents = true; // add native component definition files - let jsonFile = addComponentSameNameFiles(scriptFile, options); - if (jsonFile) { - jsonFile.isComponentConfig = true; - jsonFile.component = scriptFile; - } + addComponentSameNameFiles(scriptFile, options); } /**