Skip to content

Commit

Permalink
feat(okam-build): build file by dependence
Browse files Browse the repository at this point in the history
  • Loading branch information
wuhy committed Nov 15, 2018
1 parent 9bc800b commit 3f44454
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 71 deletions.
55 changes: 33 additions & 22 deletions packages/okam-build/lib/build/BuildManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 7 additions & 1 deletion packages/okam-build/lib/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
20 changes: 15 additions & 5 deletions packages/okam-build/lib/build/load-process-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -142,7 +151,8 @@ function loadProcessFiles(options, logger) {
return {
root: rootDir,
sourceDir,
files: processFiles
files: processFiles,
buildFiles: initBuildFiles
};
}

Expand Down
4 changes: 4 additions & 0 deletions packages/okam-build/lib/generator/FileOutput.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
Expand Down
1 change: 0 additions & 1 deletion packages/okam-build/lib/processor/helper/init-babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
}
Expand Down
3 changes: 3 additions & 0 deletions packages/okam-build/lib/processor/helper/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 11 additions & 5 deletions packages/okam-build/lib/processor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);

Expand All @@ -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);
Expand All @@ -142,6 +147,7 @@ function compile(file, buildManager) {
/**
* Get custom component tags
*
* @inner
* @param {Object} config the component config
* @return {?Array.<string>}
*/
Expand Down
81 changes: 44 additions & 37 deletions packages/okam-build/lib/processor/json/component-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -62,29 +78,16 @@ 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;
const scriptFileName = getFileName(relPath);
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);
}
Expand All @@ -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));
}

/**
Expand All @@ -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);
}

/**
Expand Down

0 comments on commit 3f44454

Please sign in to comment.