From 4f60a84a40ba91a47f7fd3678deec7fe3ff76e51 Mon Sep 17 00:00:00 2001 From: lihong Date: Tue, 23 May 2023 16:05:28 +0800 Subject: [PATCH] fixed a4547e9 from https://gitee.com/lihong67/developtools_ace-js2bundle/pulls/575 lihong67@huawei.com support parse json5. Signed-off-by: lihong Change-Id: I78c9c1d7991edc32dbc26c7afa01f858598ca635 --- ace-loader/src/read-json-plugin.js | 37 ++++++++++++++++++++++++++++++ ace-loader/webpack.rich.config.js | 2 ++ 2 files changed, 39 insertions(+) create mode 100644 ace-loader/src/read-json-plugin.js diff --git a/ace-loader/src/read-json-plugin.js b/ace-loader/src/read-json-plugin.js new file mode 100644 index 0000000..e76386d --- /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 f27966f..89253ba 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({ -- Gitee