From 69491b88fe8df8ad40ea5eaf383c6ab9b82563ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=A1=E5=93=A5?= Date: Fri, 27 Jun 2025 15:18:40 +0800 Subject: [PATCH] lizhouze@huawei.com MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 卡哥 --- ace-loader/src/extgen.js | 52 ++++++++++++++++---------- ace-loader/src/manifest-loader.js | 26 ++++++++++--- ace-loader/src/manifest-plugin.js | 27 +++++++++----- ace-loader/src/module-script.js | 26 ++++++++----- ace-loader/src/read-json-plugin.js | 59 +++++++++++++++++------------- 5 files changed, 121 insertions(+), 69 deletions(-) diff --git a/ace-loader/src/extgen.js b/ace-loader/src/extgen.js index 441f425..7b33557 100644 --- a/ace-loader/src/extgen.js +++ b/ace-loader/src/extgen.js @@ -14,28 +14,40 @@ */ import loaderUtils from 'loader-utils'; +import codegen from './codegen/index.js'; -const codegen = require("./codegen/index.js"); +/** + * Webpack loader that processes HML/CSS/JSON content + * @param {string} source - Source file content + * @param {object} [map] - Optional source map + * @returns {void} Uses webpack callback for async processing + */ +export default function hmlLoader(source, map) { + // Enable caching if available + this.cacheable?.(); -module.exports = function (source, map) { - this.cacheable && this.cacheable(); const callback = this.async(); - + const loaderOptions = loaderUtils.getOptions(this) || {}; const parsed = codegen.genHmlAndCss(source); - const loaderQuery = loaderUtils.getOptions(this) || {}; - if (parsed.errorType && parsed.errorType !== '') { - callback(parsed.errorType + ' : ' + parsed.errorMessage, ''); - } else { - switch (loaderQuery.type) { - case 'template': - callback(null, parsed.hmlCss.hml, map); - break; - case 'style': - callback(null, parsed.hmlCss.css, map); - break; - case 'json': - callback(null, parsed.hmlCss.json, map); - break; - } + + // Handle parsing errors + if (parsed.errorType) { + callback(new Error(`${parsed.errorType}: ${parsed.errorMessage}`)); + return; + } + + // Return appropriate content based on loader type + switch (loaderOptions.type) { + case 'template': + callback(null, parsed.hmlCss.hml, map); + break; + case 'style': + callback(null, parsed.hmlCss.css, map); + break; + case 'json': + callback(null, parsed.hmlCss.json, map); + break; + default: + callback(new Error(`Unknown loader type: ${loaderOptions.type}`)); } -}; +} diff --git a/ace-loader/src/manifest-loader.js b/ace-loader/src/manifest-loader.js index d4c51c2..4cf8630 100644 --- a/ace-loader/src/manifest-loader.js +++ b/ace-loader/src/manifest-loader.js @@ -16,14 +16,28 @@ const utils = require('./util'); const path = require('path'); -let projectPath = process.env.aceModuleRoot || process.cwd(); +// Resolve project paths with fallbacks +const projectPath = process.env.aceModuleRoot || process.cwd(); const manifestFilePath = process.env.aceManifestPath || path.resolve(projectPath, 'manifest.json'); +/** + * Webpack loader that injects manifest configuration into the module exports + * @param {string} source - Original module source code + * @returns {string} Enhanced source code with manifest injection + */ function manifestLoader(source) { - const manifestPlugin = utils.stringifyLoaders([{ name: path.resolve(__dirname, 'manifest-plugin.js') }]); - const queryString = utils.getRequireString(this, manifestPlugin, manifestFilePath); - source += ';(exports.default || module.exports).manifest = ' + queryString; - return source; + // Prepare manifest plugin loader string + const manifestPlugin = utils.stringifyLoaders([ + { + name: path.resolve(__dirname, 'manifest-plugin.js') + } + ]); + + // Generate require statement for manifest + const manifestRequire = utils.getRequireString(this, manifestPlugin, manifestFilePath); + + // Safely inject manifest into both ES6 and CommonJS exports + return `${source};(exports.default || module.exports).manifest = ${manifestRequire}`; } -module.exports = manifestLoader; +module.exports = manifestLoader; \ No newline at end of file diff --git a/ace-loader/src/manifest-plugin.js b/ace-loader/src/manifest-plugin.js index 3f939e8..399c5c2 100644 --- a/ace-loader/src/manifest-plugin.js +++ b/ace-loader/src/manifest-plugin.js @@ -14,13 +14,22 @@ */ const { DEVICE_LEVEL } = require('./lite/lite-enum'); -module.exports = function(source, map) { - this.cacheable && this.cacheable() - const callback = this.async() - if (process.env.DEVICE_LEVEL === DEVICE_LEVEL.LITE) { - callback(null, JSON.stringify({ 'manifest.json': 'content' }), map) - } else { - callback(null, source, map) - } -}; +/** + * Webpack loader that conditionally processes content based on device level + * @param {string} source - Original source content + * @param {object} [map] - Optional source map + * @returns {void} Uses webpack callback for async processing + */ +module.exports = function deviceLevelLoader(source, map) { + // Enable caching if available + this.cacheable?.(); + const callback = this.async(); + + // Return mock manifest for lite devices, original source otherwise + const output = process.env.DEVICE_LEVEL === DEVICE_LEVEL.LITE + ? JSON.stringify({ 'manifest.json': 'content' }) + : source; + + callback(null, output, map); +}; \ No newline at end of file diff --git a/ace-loader/src/module-script.js b/ace-loader/src/module-script.js index 75f0f04..49c2128 100644 --- a/ace-loader/src/module-script.js +++ b/ace-loader/src/module-script.js @@ -13,13 +13,21 @@ * limitations under the License. */ -import { - parseRequireModule -} from './util'; +import { parseRequireModule } from './util'; -module.exports = function(source, map) { - source = parseRequireModule(source, this.resourcePath); - this.cacheable && this.cacheable() - const callback = this.async() - callback(null, source, map) -}; +/** + * Webpack loader that processes module require statements + * @param {string} source - Original source content + * @param {object} [map] - Optional source map + * @returns {void} Uses webpack callback for async processing + */ +export default function requireModuleLoader(source, map) { + // Process require statements in source + const processedSource = parseRequireModule(source, this.resourcePath); + + // Enable caching if available + this.cacheable?.(); + + // Return processed source asynchronously + this.async()(null, processedSource, map); +} \ No newline at end of file diff --git a/ace-loader/src/read-json-plugin.js b/ace-loader/src/read-json-plugin.js index fd4862c..91a6c8b 100644 --- a/ace-loader/src/read-json-plugin.js +++ b/ace-loader/src/read-json-plugin.js @@ -13,32 +13,41 @@ * limitations under the License. */ -const JSON5 = require("json5"); +const JSON5 = require('json5'); -module.exports = class ReadJsonPlugin { +/** + * Webpack resolver plugin that adds JSON5 support + */ +class ReadJsonPlugin { + /** + * Apply plugin to resolver + * @param {object} resolver - Webpack resolver instance + */ apply(resolver) { - if (resolver && resolver.fileSystem && resolver.fileSystem.readFile) { - resolver.fileSystem.readJson = (filepath, callback) => { - resolver.fileSystem.readFile(filepath, (error, content) => { - if (error) { - return callback(error); - } - if (!content || content.length === 0) { - return callback(new Error("No file content")); - } - let data; - try { - if (/\.json5$/.test(filepath)) { - data = JSON5.parse(content.toString("utf-8")); - } else { - data = JSON.parse(content.toString("utf-8")); - } - } catch (e) { - return callback(e); - } + if (!resolver?.fileSystem?.readFile) return; + + // Add custom readJson method to filesystem + resolver.fileSystem.readJson = (filepath, callback) => { + resolver.fileSystem.readFile(filepath, (error, content) => { + // Handle read errors + if (error) { + return callback(error); + } + if (!content?.length) { + return callback(new Error('No file content')); + } + + try { + // Parse based on file extension + const parse = filepath.endsWith('.json5') ? JSON5.parse : JSON.parse; + const data = parse(content.toString('utf-8')); callback(null, data); - }); - }; - } + } catch (e) { + callback(e); + } + }); + }; } -}; +} + +module.exports = ReadJsonPlugin; \ No newline at end of file -- Gitee