This simple Python module will try to resolve your Javascript relative import
statements with an package-ish one.
For examples:
If your project's structure is like this:
app/
logging/
logger.js
index.js
customers/
processes/
onboarding/
registration.js
and in registration.js
you want to use the logger
module.
import { Logger } from '../../../logging';
This looks ugly. So this script will try to replace it with
import { Logger } from 'app/logging';
Of course, after this replacement, you have to configure you transpiler (i.e: Babel) to resolve the path.
For example, in your package.json
:
// package.json
{
"babel": {
"plugins": [
["module-resolver", {
"root": ["./app"],
"alias": {
"app": "./app"
}
}]
]
}
}
./replace_relative_import.py your/app/then/some/javascript/module.js
This gist is shipped with a Shell script to find and replace all *.js
files.
# Default mode
# ./bulk_change.sh . *.js
./bulk_change.sh
# or
./bulk_change.sh app '*customer*.js'