Skip to content

Commit

Permalink
build(cjs): adapt script for converting js to cjs files to run in mul…
Browse files Browse the repository at this point in the history
…tiple platform
  • Loading branch information
blephy committed Jul 1, 2022
1 parent 7a3d023 commit 7247b08
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"types": "./lib/types/index.d.ts",
"scripts": {
"build": "yarn clean && yarn build:cjs && yarn build:esm",
"build:cjs": "tsc -b tsconfig.build.cjs.json && ./scripts/convert-to-cjs.sh",
"build:cjs": "tsc -b tsconfig.build.cjs.json && ./scripts/convert_to_cjs.sh lib",
"build:esm": "tsc -b tsconfig.build.esm.json",
"clean": "rm -rfv reports coverage build dist lib .eslintcache",
"commit": "cz",
Expand Down
11 changes: 0 additions & 11 deletions scripts/convert-to-cjs.sh

This file was deleted.

38 changes: 38 additions & 0 deletions scripts/convert_to_cjs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
echo ""
echo "Converting .js files to .cjs"
echo ""

build_folder="$1"

if [ -z "${build_folder}" ]; then
echo "\033[0;31mYou must provide a path to aim your build folder"
exit 125
fi

if [ ! -d "${build_folder}" ]; then
echo "\033[0;31m${build_folder} folder does not exist"
exit 125
fi

files=$(find ${build_folder} -name "*.js")

if [ $? -ne 0 ]; then
echo "\033[0;31m find command failed"
exit 125
fi

for path in $files; do
echo "Converting $path"

if [[ "$OSTYPE" == "linux-gnu"* ]]; then
sed -r -i'' "s/(require ?[^\"]+\"([^\"]+))\.js/\1.cjs/" "$path"
elif [[ "$OSTYPE" == "darwin"* ]]; then
sed -r -i '' "s/(require ?[^\"]+\"([^\"]+))\.js/\1.cjs/" "$path"
fi

mv "$path" "${path/%.js/.cjs}"
done

echo ""
echo "Done converting files to .cjs"
echo ""
3 changes: 1 addition & 2 deletions tsconfig.build.cjs.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
"outDir": "./lib/cjs",
"target": "ES2015",
"module": "CommonJS",
"declaration": false,
"declarationMap": false
"declaration": false
},
"include": ["./src/**/*"]
}
2 changes: 1 addition & 1 deletion tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"compilerOptions": {
"baseUrl": ".",
"declaration": true,
"declarationMap": true,
"declarationMap": false,
"removeComments": false,
"sourceMap": false,
"noEmit": false,
Expand Down

0 comments on commit 7247b08

Please sign in to comment.