Skip to content

Commit

Permalink
fix: add ESM version
Browse files Browse the repository at this point in the history
  • Loading branch information
homer0 committed Jul 22, 2020
1 parent 06a9bd5 commit 9168f0c
Show file tree
Hide file tree
Showing 5 changed files with 1,041 additions and 25 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ coverage/*
documentation/*
node_modules/*
docs/*
esm/*
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ coverage
npm-debug.log
yarn-error.log
/docs
/esm
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
],
"dependencies": {},
"devDependencies": {
"@babel/core": "^7.10.5",
"@babel/preset-env": "^7.10.4",
"5to6-codemod": "1.8.0",
"docdash": "homer0/docdash#semver:^2.0.0",
"eslint": "^7.5.0",
"eslint-plugin-homer0": "^5.0.1",
"husky": "^4.2.5",
"jest": "^26.1.0",
"jscodeshift": "0.10.0",
"jsdoc": "^3.6.4",
"jsdoc-ts-utils": "^1.0.0",
"leasot": "^11.1.0",
Expand All @@ -32,6 +36,7 @@
},
"main": "src/index.js",
"scripts": {
"prepare": "./utils/scripts/prepare",
"test": "./utils/scripts/test",
"lint": "./utils/scripts/lint",
"lint:all": "./utils/scripts/lint-all",
Expand Down
46 changes: 46 additions & 0 deletions utils/scripts/prepare
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash -e

##############################
# CONFIG
##############################

# The path where the ESM build will be generated.
OUTPUT_PATH="./esm"
# The directory that needs to be copied and transpiled.
SOURCE="./src"
# The path to the transformations jscodeshift will use; this variable exists just to
# avoid writing the long path on each transformation.
CODEMODS_PATH="node_modules/5to6-codemod/transforms"
# The list of transformations needed in order to make the code ESM compatible.
#
# - cjs: changes all 'require' statements to 'import'.
# - exports: changes all `module.exports` statements to `export` .
TRANSFORMATIONS=("cjs" "exports")

##############################
# EXECUTION
##############################

# Delete the output path if it already exists and create it again.
rm -rf $OUTPUT_PATH && mkdir $OUTPUT_PATH;

# Copy all the files that need transpilation. jscodeshift is a tool for codemods,
# so it doesn't transpile from one place to another, so we first need to move the files
# and then transform them.
cp -R "$SOURCE/" "$OUTPUT_PATH";
# for SOURCE in "${SOURCES[@]}";
# do cp -R "./$SOURCE" "$OUTPUT_PATH/$SOURCE";
# done

# Delete any hidden/configuration file that was copied during the previous step.
find $OUTPUT_PATH -maxdepth 2 -type f -name ".*" -depth -delete;

# jscodeshift doesn't allow for multiple transformations at once, that's why it needs
# too iterate the list and apply one by one.
for TRANSFORMATION in "${TRANSFORMATIONS[@]}";
do jscodeshift -t "$CODEMODS_PATH/$TRANSFORMATION.js" $OUTPUT_PATH;
done

# Add a `package.json` with the `type: 'module'` on the output directory so imports
# from Node native ESM modules won't cause errors.
echo "{ \"type\": \"module\" }" > "$OUTPUT_PATH/package.json";
Loading

0 comments on commit 9168f0c

Please sign in to comment.