From 021c38a50fd7a18cac6adcf1aeeb453951a28d0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=A1=E5=93=A5?= Date: Fri, 27 Jun 2025 10:06:55 +0800 Subject: [PATCH] lizhouze@huawei.com MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 卡哥 --- src/loader.js | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) diff --git a/src/loader.js b/src/loader.js index 55094ee..94c7014 100644 --- a/src/loader.js +++ b/src/loader.js @@ -51,6 +51,12 @@ const defaultLoaders = { resourceReferenceScript: path.resolve(loaderPath, 'resource-reference-script.js') } +/** + * Generate parser for compiler. + * @param {String} type The type of parser to be generated. + * @param {Object} config Configuration information of the parser to be generated. + * @returns {String} + */ function getLoaderString (type, config) { config = config || {} const customLoader = loadCustomLoader(config) @@ -73,6 +79,11 @@ function getLoaderString (type, config) { } } +/** + * Generate custom parsing information. + * @param {Object} config Configuration information of the parser to be generated. + * @returns {Object} + */ function loadCustomLoader (config) { if (config.lang && config.customLang[config.lang]) { return loadBabelModule(config.customLang[config.lang][0]) @@ -96,6 +107,13 @@ function elementLoaderString (loaders, config) { return stringifyLoaders(loaders) } +/** + * Generate template parsing infomation for HML file. + * @param {Object} loaders Parser to be loaded. + * @param {Object} config Configuration information of the parser to be generated. + * @param {Object} customLoader custom parsing information. + * @returns {String} + */ function templateLoaderString (loaders, config, customLoader) { loaders = [{ name: defaultLoaders.json @@ -108,6 +126,13 @@ function templateLoaderString (loaders, config, customLoader) { return stringifyLoaders(loaders) } +/** + * Generate style parsing infomation for css/less/sass file. + * @param {Object} loaders Parser to be loaded. + * @param {Object} config Configuration information of the parser to be generated. + * @param {Object} customLoader custom parsing information. + * @returns {String} + */ function styleLoaderString (loaders, config, customLoader) { loaders = [{ name: defaultLoaders.json @@ -120,6 +145,13 @@ function styleLoaderString (loaders, config, customLoader) { return stringifyLoaders(loaders) } +/** + * Generate script parsing infomation for js file. + * @param {Object} loaders Parser to be loaded. + * @param {Object} config Configuration information of the parser to be generated. + * @param {Object} customLoader custom parsing information. + * @returns {String} + */ function scriptLoaderString (loaders, config, customLoader) { loaders = [{ name: defaultLoaders.script @@ -154,6 +186,12 @@ function scriptLoaderString (loaders, config, customLoader) { return stringifyLoaders(loaders) } +/** + * Generate configuration parsing information. + * @param {Object} loaders Parser to be loaded. + * @param {Object} config Configuration information of the parser to be generated. + * @returns {String} + */ function configLoaderString (loaders, config) { loaders = [{ name: defaultLoaders.json @@ -161,6 +199,12 @@ function configLoaderString (loaders, config) { return stringifyLoaders(loaders) } +/** + * Generate information for data configuration parsing. + * @param {Object} loaders Parser to be loaded. + * @param {Object} config Configuration information of the parser to be generated. + * @returns {String} + */ function dataLoaderString (loaders, config) { loaders = [{ name: defaultLoaders.json @@ -168,6 +212,11 @@ function dataLoaderString (loaders, config) { return stringifyLoaders(loaders) } +/** + * Compiled entry function,generate all parsing configuration information. + * @param {Object} source Compile information passed in from webpack. + * @returns {Object} + */ function loader (source) { this.cacheable && this.cacheable() @@ -208,6 +257,11 @@ function loader (source) { return output } +/** + * Check if this analysis is in test mode. + * @param {Object} _this Context for this analysis. + * @returns {Boolean} + */ function checkApp(_this) { if (process.env.abilityType === 'testrunner') { return true; @@ -216,6 +270,15 @@ function checkApp(_this) { process.env.abilityType === 'page' ? 'app.js' : `${process.env.abilityType}.js`) } +/** + * If it is currently in test mode, the parsing content of the test mode will be generated. + * @param {Object} _this Context for this analysis. + * @param {String} name Name of the page. + * @param {Boolean} isEntry Is entry page. + * @param {String} customLang The language of the content to be compiled. + * @param {Object} source Compile information passed in from webpack. + * @returns {Object} + */ function loadApp (_this, name, isEntry, customLang, source) { let output = '' let extcss = false @@ -277,6 +340,17 @@ function loadApp (_this, name, isEntry, customLang, source) { } } +/** + * + * Analyze a single page. + * @param {Object} _this Context for this analysis. + * @param {String} name Name of the page. + * @param {Boolean} isEntry Is entry page. + * @param {String} customLang The language of the content to be compiled. + * @param {Object} source Compile information passed in from webpack. + * @param {String} parentPath parent of this page. + * @returns + */ function loadPage (_this, name, isEntry, customLang, source, parentPath) { let output = '' if (path.extname(_this.resourcePath).match(/\.hml/)) { @@ -315,6 +389,18 @@ function loadPage (_this, name, isEntry, customLang, source, parentPath) { return output } +/** + * + * Analyze the HML file of a single page. + * @param {Object} _this Context for this analysis. + * @param {Number} elementLength element length. + * @param {Object} frag frag infomation. + * @param {Array} elementNames element names. + * @param {String} resourcePath path of this hml file. + * @param {String} customLang The language of the content to be compiled. + * @param {String} parentPath parent of this hml file. + * @returns {Object} + */ function loadPageCheckElementLength (_this, elementLength, frag, elementNames, resourcePath, customLang, parentPath) { let output = '' @@ -366,6 +452,14 @@ function loadPageCheckElementLength (_this, elementLength, frag, elementNames, r return output } +/** + * + * Analyze the CSS file of a single page + * @param {Object} _this Context for this analysis. + * @param {String} filename Css file name. + * @param {String} customLang The language of the content to be compiled. + * @returns {Object} + */ function loadPageFindCss (_this, filename, customLang) { let output = '' let extcss = false @@ -431,6 +525,14 @@ function loadPageFindCss (_this, filename, customLang) { } } +/** + * + * Analyze the JS file of a single page + * @param {Object} _this Context for this analysis. + * @param {String} filename Css file name. + * @param {String} customLang The language of the content to be compiled. + * @returns {Object} + */ function loadPageFindJs (_this, filename, customLang) { let output = '' let extscript = false @@ -455,6 +557,14 @@ function loadPageFindJs (_this, filename, customLang) { } } +/** + * Determine whether it is a rich mode compilation and generate the corresponding parser. + * @param {String} name Current page name. + * @param {String} extscript The parsing information of JS to be passed to webpack. + * @param {String} extcss The parsing information of CSS to be passed to webpack. + * @param {Boolean} isEntry Is entry page. + * @returns {Object} + */ function loadPageCheckRich (name, extscript, extcss, isEntry) { let output = '' output += ` @@ -477,6 +587,12 @@ $app_module$.exports.style = $app_style$ return output } +/** + * Determine whether it is a lite mode compilation and generate the corresponding parser. + * @param {String} extscript The parsing information of JS to be passed to webpack. + * @param {String} extcss The parsing information of CSS to be passed to webpack. + * @returns {Object} + */ function loadPageCheckLite (extscript, extcss) { return (extscript ? `var options=$app_script$\n if ($app_script$.__esModule) {\n options = $app_script$.default;\n }\n` : `var options={}\n`) + @@ -488,6 +604,12 @@ for (const key in legacy) { loader[key] = legacy[key] } +/** + * Check if this page is the entrance. + * @param {Object} _this Context for this analysis. + * @param {String} filePath The file path to be determined. + * @param {String} elementSrc The src infomation in config.json. + */ function checkEntry(_this, filePath, elementSrc) { if (_this._compilation.entries) { for (var key of _this._compilation.entries.keys()) { -- Gitee