From 705ef1d5e2d6cb00206b41ab7ac9e71425de344a Mon Sep 17 00:00:00 2001 From: literat Date: Wed, 21 Feb 2024 12:54:00 +0100 Subject: [PATCH] Fix(web-react): Replace usage of `html-react-parser` in CommonJS files * @see: https://www.npmjs.com/package/html-react-parser#usage * according to docs the `html-react-parser` should be required in CommonJS as `require('html-react-parser').default` but our build process creates the files without the `.default` export * this replacement post process is the workaround to fix this issue --- packages/web-react/package.json | 3 +- packages/web-react/scripts/postProcess.js | 35 +++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 packages/web-react/scripts/postProcess.js 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);