diff --git a/ace-loader/src/read-json-plugin.js b/ace-loader/src/read-json-plugin.js new file mode 100644 index 0000000000000000000000000000000000000000..e76386dab62862427a200259925394a1a8d2b52e --- /dev/null +++ b/ace-loader/src/read-json-plugin.js @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const JSON5 = require("json5"); + +module.exports = class ReadJsonPlugin { + apply(resolver) { + if (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 { + data = JSON5.parse(content.toString("utf-8")); + } catch (e) { + return callback(e); + } + callback(null, data); + }); + }; + } + } +}; diff --git a/ace-loader/webpack.rich.config.js b/ace-loader/webpack.rich.config.js index f27966f50ccfd40325625936710e2eecdbfa2087..89253ba3d9e5db5eb2e6f87dd20b503637d27ced 100644 --- a/ace-loader/webpack.rich.config.js +++ b/ace-loader/webpack.rich.config.js @@ -21,6 +21,7 @@ var ResultStates = require('./lib/compile-plugin') var GenBinPlugin = require('./lib/genBin-plugin') var GenAbcPlugin = require('./lib/genAbc-plugin').GenAbcPlugin var AfterEmitPlugin = require('./lib/cardJson-plugin').AfterEmitPlugin +const ReadJsonPlugin = require('./lib/read-json-plugin') const { PLATFORM }= require('./lib/lite/lite-enum') const util = require('./lib/util') @@ -314,6 +315,7 @@ module.exports = (env) => { './oh_modules' ], descriptionFiles: ['package.json', 'oh-package.json5'], + plugins: [new ReadJsonPlugin()], } if (fs.existsSync(path.resolve(process.env.projectPath, 'i18n'))) { config.plugins.push(new CopyPlugin({