Skip to content

Instantly share code, notes, and snippets.

@framp
Created August 26, 2016 13:48
Show Gist options
  • Save framp/c31e981a35d817a9ea10cdc4ea9c4d99 to your computer and use it in GitHub Desktop.
Save framp/c31e981a35d817a9ea10cdc4ea9c4d99 to your computer and use it in GitHub Desktop.
Sets of regex replacements to get rid of ES6 imports/exports
#import
find . -name 'node_modules' -prune -o -type f -name '*.js' \
| xargs perl -i.bak -wpe 's/import(.*)from\s*'\''(.*)'\''/const$1= require('\''$2'\'')/g'
#export default
find . -name 'node_modules' -prune -o -type f -name '*.js' \
| xargs perl -i.bak -wpe 's/export default/module.exports =/g'
#export const
find . -name 'node_modules' -prune -o -type f -name '*.js' \
| xargs perl -i.bak -wpe 's/export const\s*([^\s]*)/const $1 = exports.$1/g'
#export function
find . -name 'node_modules' -prune -o -type f -name '*.js' \
| xargs perl -i.bak -wpe 's/export function\s*([^\s]*)/const $1 = exports.$1 = function $1/g'
#Fix { a as b }
find . -name 'node_modules' -prune -o -type f -name '*.js' \
| xargs perl -i.bak -wpe 's/const {(.*)([^\s])\s+as\s+([^\s}]*)/const {$1$2: $3/g'
find . -name 'node_modules' -prune -o -name "*.bak" -print0 | xargs -0 rm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment