![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
babel-root-babel-plugin-root-import
Advanced tools
Babel plugin to add the opportunity to use import
and require
with root based paths.
// Usually
import SomeExample from '../../../some/example.js';
const OtherExample = require('../../../other/example.js');
// With Babel-Root-Importer
import SomeExample from '~/some/example.js';
const OtherExample = require('~/other/example.js');
npm
npm install babel-plugin-root-import --save-dev
yarn
yarn add babel-plugin-root-import --dev
Add a .babelrc
file and write:
{
"plugins": [
["babel-plugin-root-import"]
]
}
or pass the plugin with the plugins-flag on CLI
babel-node myfile.js --plugins babel-plugin-root-import
If you want a custom root because for example all your files are in the src/js folder you can define this in your .babelrc
file
{
"plugins": [
["babel-plugin-root-import", {
"rootPathSuffix": "src/js"
}]
]
}
If you don't like the ~
syntax you can just use your own symbol (for example a @
symbol or \
)
{
"plugins": [
["babel-plugin-root-import", {
"rootPathPrefix": "@"
}]
]
}
// Now you can use the plugin like:
import foo from '@/my-file';
You can supply an array of the above. The plugin will try each prefix/suffix pair in the order they are defined.
{
"plugins": [
["babel-plugin-root-import", [{
"rootPathPrefix": "~", // `~` is the default so you can remove this if you want
"rootPathSuffix": "src/js"
}, {
"rootPathPrefix": "@",
"rootPathSuffix": "other-src/js"
}, {
"rootPathPrefix": "#",
"rootPathSuffix": "../../src/in/parent" // since we suport relative paths you can also go into a parent directory
}]]
]
}
// Now you can use the plugin like:
import foo from '~/my-file';
const bar = require('@/my-file');
If you use eslint-plugin-import to validate imports it may be necessary to instruct ESLint to parse root imports. You can use eslint-import-resolver-babel-plugin-root-import
"import/resolver": {
"babel-plugin-root-import": {}
}
If you use Facebook's Flow for type-checking it is necessary to instruct it on how to map your chosen prefix to the root directory. Add the following to your .flowconfig file, replacing ~
with your chosen prefix.
[options]
module.name_mapper='^~\/\(.*\)$' -> '<PROJECT_ROOT>/\1'
Webpack delivers a similar feature, if you just want to prevent end-less import strings you can also define aliases
in the resolve
module, at the moment it doesn't support custom/different symbols and multiple/custom suffixes.
READ MORE
/
in generated paths~
you can use whatever you like)Breaking Change to Babel 5
FAQs
Babel Plugin to enable relative root-import
We found that babel-root-babel-plugin-root-import demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.