Product
Introducing Java Support in Socket
We're excited to announce that Socket now supports the Java programming language.
babel-plugin-module-resolver
Advanced tools
The babel-plugin-module-resolver npm package allows you to customize the resolution of modules in your Babel projects. It can be used to alias paths, so you don't have to deal with relative paths, and it can also help in organizing the project's structure more logically from a developer's perspective.
Custom Aliases
This feature allows you to define custom aliases for directories within your project. Instead of using relative paths, you can use the aliases to require modules, making the code cleaner and easier to maintain.
{
"plugins": [
["module-resolver", {
"alias": {
"@app": "./src/app",
"@models": "./src/models"
}
}]
]
}
Extension Resolution
This feature enables you to specify which file extensions should be resolved by the plugin. This can be useful when you are working with different types of JavaScript files and want to import them without specifying the extension.
{
"plugins": [
["module-resolver", {
"extensions": [
".js",
".jsx"
]
}]
]
}
Root Directory Configuration
By setting a root directory, you can simplify the import statements in your project. This feature allows you to set a base directory from which all the modules will be resolved.
{
"plugins": [
["module-resolver", {
"root": ["./src"]
}]
]
}
This package is used with webpack to automatically resolve modules based on the paths defined in tsconfig.json. It is similar to babel-plugin-module-resolver but is specifically tailored for TypeScript projects using webpack.
This package is an ESLint resolver that allows you to configure aliases for module paths, similar to babel-plugin-module-resolver. It is used to help ESLint understand the custom path aliases defined in your project.
Aliasify is a Browserify transform that provides similar aliasing functionality to babel-plugin-module-resolver. It allows you to rewrite module calls in your Browserify bundles, but it is specific to the Browserify ecosystem.
A Babel plugin to add a new resolver for your modules when compiling your code using Babel. This plugin allows you to add new "root" directories that contain your modules. It also allows you to setup a custom alias for directories, specific files, or even other npm modules.
This plugin can simplify the require/import paths in your project. For example, instead of using complex relative paths like ../../../../utils/my-utils
, you can write utils/my-utils
. It will allow you to work faster since you won't need to calculate how many levels of directory you have to go up before accessing the file.
// Use this:
import MyUtilFn from 'utils/MyUtilFn';
// Instead of that:
import MyUtilFn from '../../../../utils/MyUtilFn';
// And it also work with require calls
// Use this:
const MyUtilFn = require('utils/MyUtilFn');
// Instead of that:
const MyUtilFn = require('../../../../utils/MyUtilFn');
Install the plugin
npm install --save-dev babel-plugin-module-resolver
or
yarn add --dev babel-plugin-module-resolver
Specify the plugin in your .babelrc
with the custom root or alias. Here's an example:
{
"plugins": [
["module-resolver", {
"root": ["./src"],
"alias": {
"test": "./test",
"underscore": "lodash"
}
}]
]
}
.babelrc.js version
Specify the plugin in your .babelrc.js
file with the custom root or alias. Here's an example:
const plugins = [
[
require.resolve('babel-plugin-module-resolver'),
{
root: ["./src/"],
alias: {
"test": "./test"
}
}
]
];
Good example: // https://gist.github.com/nodkz/41e189ff22325a27fe6a5ca81df2cb91
babel-plugin-module-resolver can be configured and controlled easily, check the documentation for more details
Are you a plugin author (e.g. IDE integration)? We have documented the exposed functions for use in your plugins!
If you're using ESLint, you should use eslint-plugin-import, and eslint-import-resolver-babel-module to remove falsy unresolved modules. If you want to have warnings when aliased modules are being imported by their relative paths, you can use eslint-plugin-module-resolver.
babel-plugin-module-resolver
option.jsconfig.json
(tsconfig.json
for TypeScript), e.g.:{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"*": ["src/*"],
"test/*": ["test/*"],
"underscore": ["lodash"]
}
}
}
../../../utils/MyUtilFn
you can mark
../../../utils
as "resources root". This has the problem that your alias also has to be named utils
. The second option is to add
a webpack.config.js
to your project and use it under File->Settings->Languages&Frameworks->JavaScript->Webpack. This will trick webstorm
into resolving the paths and you can use any alias you want e.g.:var path = require('path');
module.exports = {
resolve: {
extensions: ['.js', '.json', '.vue'],
alias: {
utils: path.resolve(__dirname, '../../../utils/MyUtilFn'),
},
},
};
MIT, see LICENSE.md for details.
Are you also using it? Send a PR!
FAQs
Module resolver plugin for Babel
The npm package babel-plugin-module-resolver receives a total of 1,911,857 weekly downloads. As such, babel-plugin-module-resolver popularity was classified as popular.
We found that babel-plugin-module-resolver demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers 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.
Product
We're excited to announce that Socket now supports the Java programming language.
Security News
Socket detected a malicious Python package impersonating a popular browser cookie library to steal passwords, screenshots, webcam images, and Discord tokens.
Security News
Deno 2.0 is now available with enhanced package management, full Node.js and npm compatibility, improved performance, and support for major JavaScript frameworks.