Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

patch(): pass filename to transform func #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ module.exports = {
plugins: [
['transform-imports', {
'my-library': {
transform: function(importName, matches) {
transform: function(importName, matches, filename) {
return `my-library/etc/${importName.toUpperCase()}`;
},
preventFullImport: true,
Expand Down
30 changes: 16 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function barf(msg) {
throw new Error('babel-plugin-transform-imports: ' + msg);
}

function transform(transformOption, importName, matches) {
function transform(transformOption, importName, matches, filename) {
var isFunction = typeof transformOption === 'function';
if (/\.js$/i.test(transformOption) || isFunction) {
var transformFn;
Expand All @@ -62,7 +62,7 @@ function transform(transformOption, importName, matches) {
barf('expected transform function to be exported from ' + transformOption);
}

return transformFn(importName, matches);
return transformFn(importName, matches, filename);
}

return transformOption.replace(/\$\{\s?([\w\d]*)\s?\}/ig, function(str, g1) {
Expand All @@ -71,10 +71,17 @@ function transform(transformOption, importName, matches) {
});
}

const replacements = [];

module.exports = function() {
return {
visitor: {
ImportDeclaration: function (path, state) {
// skip transforming imports that were already transformed
if (replacements.indexOf(path.node) > -1) {
return;
}

// https://github.com/babel/babel/tree/master/packages/babel-types#timportdeclarationspecifiers-source

// path.node has properties 'source' and 'specifiers' attached.
Expand All @@ -88,14 +95,16 @@ module.exports = function() {
var opts = state.opts[opt];
var hasOpts = !!opts;

var matches = isRegexp ? getMatchesFromSource(opt, source) : [];

if (hasOpts) {
if (!opts.transform) {
barf('transform option is required for module ' + source);
}

var transforms = [];

var fullImports = path.node.specifiers.filter(function(specifier) { return specifier.type !== 'ImportSpecifier' });
var fullImports = path.node.specifiers.filter(function (specifier) { return specifier.type !== 'ImportSpecifier' })
var memberImports = path.node.specifiers.filter(function(specifier) { return specifier.type === 'ImportSpecifier' });

if (fullImports.length > 0) {
Expand All @@ -107,18 +116,10 @@ module.exports = function() {
barf('import of entire module ' + source + ' not allowed due to preventFullImport setting');
}

if (memberImports.length > 0) {
// Swap out the import with one that doesn't include member imports. Member imports should each get their own import line
// transform this:
// import Bootstrap, { Grid } from 'react-bootstrap';
// into this:
// import Bootstrap from 'react-bootstrap';
transforms.push(types.importDeclaration(fullImports, types.stringLiteral(source)));
}
var replace = transform(opts.transform, undefined, matches, state.filename);
transforms.push(types.importDeclaration(fullImports, types.stringLiteral(replace)));
}

var matches = isRegexp ? getMatchesFromSource(opt, source) : [];

memberImports.forEach(function(memberImport) {
// Examples of member imports:
// import { member } from 'module'; (ImportSpecifier)
Expand All @@ -138,7 +139,7 @@ module.exports = function() {
else if (opts.memberConverter === 'snake') importName = snake(importName);
else if (typeof(opts.memberConverter) === 'function') importName = opts.memberConverter(importName);

var replace = transform(opts.transform, importName, matches);
var replace = transform(opts.transform, importName, matches, state.filename);

var newImportSpecifier = (opts.skipDefaultConversion)
? memberImport
Expand All @@ -165,6 +166,7 @@ module.exports = function() {
});

if (transforms.length > 0) {
replacements.push(...transforms);
path.replaceWithMultiple(transforms);
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"homepage": "https://bitbucket.org/amctheatres/babel-transform-imports",
"scripts": {
"test": "node_modules/.bin/mocha --compilers js:babel-register"
"test": "mocha --compilers js:babel-register"
},
"author": "AMC Theatres",
"license": "ISC",
Expand Down