diff --git a/packages/web-react/package.json b/packages/web-react/package.json index 17824b9918..db8524be57 100644 --- a/packages/web-react/package.json +++ b/packages/web-react/package.json @@ -95,8 +95,9 @@ "build:umd": "npm-run-all --serial webpack:dev webpack:prod", "build:es2020": "tsc --module es2020 --outDir dist --project ./config/tsconfig.prod.json", "build:esNext": "echo tsc --module esNext --outDir dist/_esNext --project ./config/tsconfig.prod.json", - "postbuild": "npm-run-all prepdist resolve build:cjs", + "postbuild": "npm-run-all prepdist resolve build:cjs postprocess", "prepdist": "node ./scripts/prepareDist.js", + "postprocess": "node ./scripts/postProcess.js", "types": "tsc", "webpack:dev": "webpack --mode development --config ./config/webpack.js --progress", "webpack:prod": "webpack --mode production --config ./config/webpack.js --progress", diff --git a/packages/web-react/scripts/postProcess.js b/packages/web-react/scripts/postProcess.js new file mode 100644 index 0000000000..bbae24d4e3 --- /dev/null +++ b/packages/web-react/scripts/postProcess.js @@ -0,0 +1,35 @@ +const fs = require('fs'); +const path = require('path'); + +const directoryPath = path.join(__dirname, '../dist'); + +/** + * Replace the require statement in all files in the given directory. + * + * @param directory string + */ +function replaceInDirs(directory) { + fs.readdir(directory, (error, files) => { + if (error) { + // eslint-disable-next-line no-console -- This is a CLI script + return console.log(`Unable to scan directory: ${error}`); + } + + files.forEach((file) => { + const filePath = path.join(directory, file); + if (fs.statSync(filePath).isDirectory()) { + // If the path is a directory, call this function recursively + replaceInDirs(filePath); + } else { + let fileContent = fs.readFileSync(filePath, 'utf8'); + + // eslint-disable-next-line quotes -- Two conflicting rules, we do not won't to escape the quotes + fileContent = fileContent.replace("require('html-react-parser')", "require('html-react-parser').default"); + + fs.writeFileSync(filePath, fileContent, 'utf8'); + } + }); + }); +} + +replaceInDirs(directoryPath);