Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-import-resolver

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-import-resolver - npm Package Compare versions

Comparing version 0.1.0 to 0.1.1

test/fixtures/invalid-multi-import/actual.js

2

package.json
{
"name": "babel-plugin-import-resolver",
"version": "0.1.0",
"version": "0.1.1",
"description": "resolve import to specific file",

@@ -5,0 +5,0 @@ "main": "src/index.js",

# babel-plugin-import-resolver
[![Build Status](https://travis-ci.org/springuper/babel-plugin-import-resolver.svg?branch=master)](https://travis-ci.org/springuper/babel-plugin-import-resolver)
Resolve import to specific file.

@@ -29,2 +31,24 @@

#### Via `.babelrc` (Recommended)
```js
{
"plugins": [
["import-resolver", {
"condition": "^(\\.|\\/).*\\/components$",
"template": "{source}/{name}/{name}"
}]
]
}
```
There are two options:
- `condition` (`String` or `[String]`)
One or multiple string format regular expressions, if the source of ImportDeclaration matches any of them, it will be replaced by the following `template`.
- `template` (`String`)
Used to replace hit source with a simple variable placeholder presentation. There are mainly two variable placeholders: `{source}` represents the source of ImportDeclaration, and `{name}` represents imported name.
#### Via Node API

@@ -35,9 +59,5 @@

plugins: ['import-resolver', {
locate: (name, source) => {
if (source.match(/^(\.|\/).*\/components$/)) {
return `${source}/${name}/${name}`;
}
return source;
},
condition: '^(\\.|\\/).*\\/components$', // string format regular expression
template: '{source}/{name}/{name}', // template variables (wrapped in braces) will be replaced
}],
});
const { dirname } = require('path');
const regCache = {};
const getReg = (str) => {
if (!regCache[str]) {
regCache[str] = new RegExp(str);
}
return regCache[str];
};
const regTplVariable = /\{([^}]+)\}/g;
const genContent = (tpl, data) => {
return tpl.replace(regTplVariable, (match, variable) => data[variable] || '');
};
function transformImportResolve({ types: t }) {

@@ -8,3 +20,3 @@ function resolver(

file: { opts: { filename } },
opts: { locate },
opts: { condition, template },
}

@@ -16,3 +28,3 @@ ) {

const { value } = node.source;
const source = node.source.value;
const specificImports = [];

@@ -22,7 +34,17 @@ node.specifiers.forEach(spec => {

const location = locate(spec.imported.name, value, filename);
specificImports.push(t.importDeclaration(
[t.importDefaultSpecifier(t.identifier(spec.local.name))],
t.stringLiteral(location))
);
const conditions = Array.isArray(condition) ? condition : [condition];
const isHit = conditions.some(cond => source.match(getReg(cond)));
if (!isHit) return;
const location = genContent(template, {
name: spec.imported.name,
source,
filename,
});
if (location && location !== source) {
specificImports.push(t.importDeclaration(
[t.importDefaultSpecifier(t.identifier(spec.local.name))],
t.stringLiteral(location))
);
}
});

@@ -29,0 +51,0 @@

@@ -14,3 +14,3 @@ const path = require('path');

fs.readdirSync(fixturesDir).map((caseName) => {
it(`should ${caseName.split('-').join(' ')}`, () => {
it(`should handle "${caseName.split('-').join(' ')}"`, () => {
const fixtureDir = path.join(fixturesDir, caseName);

@@ -23,8 +23,4 @@ const actualPath = path.join(fixtureDir, 'actual.js');

{
locate: (name, source, filename) => {
if (source.match(/^(\.|\/).*\/components$/)) {
return `${source}/${name}/${name}`;
}
return source;
},
condition: '^(\\.|\\/).*\\/components$',
template: '{source}/{name}/{name}',
},

@@ -31,0 +27,0 @@ ],

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc