Skip to content

Commit

Permalink
Fix(web-react): Replace usage of html-react-parser in CommonJS files
Browse files Browse the repository at this point in the history
  * @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
  • Loading branch information
literat committed Feb 23, 2024
1 parent 18c3650 commit 705ef1d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/web-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
35 changes: 35 additions & 0 deletions packages/web-react/scripts/postProcess.js
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 705ef1d

Please sign in to comment.