Skip to content

Commit

Permalink
fix: Fix double plugin bug (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
fatfisz committed Apr 1, 2017
2 parents 554fc97 + d6ed2df commit 944a002
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 48 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Expand Up @@ -6,6 +6,7 @@ module.exports = {
'airbnb-base',
],
rules: {
'no-param-reassign': [2, { props: false }],
'import/extensions': 0,
'import/no-extraneous-dependencies': 0,
'import/no-unresolved': 0,
Expand Down
4 changes: 2 additions & 2 deletions src/getRealPath.js
Expand Up @@ -90,7 +90,7 @@ function getRealPathFromRegExpConfig(sourcePath, regExps) {
return aliasedSourceFile;
}

export default function getRealPath(sourcePath, { file, opts, cwd }) {
export default function getRealPath(sourcePath, { file, opts }) {
if (sourcePath[0] === '.') {
return sourcePath;
}
Expand All @@ -100,7 +100,7 @@ export default function getRealPath(sourcePath, { file, opts, cwd }) {
const currentFile = file.opts.filename;
const absCurrentFile = path.resolve(currentFile);

const { root, extensions, alias, regExps } = opts;
const { cwd, root, extensions, alias, regExps } = opts;

const sourceFileFromRoot = getRealPathFromRootConfig(
sourcePath, absCurrentFile, root, cwd, extensions,
Expand Down
89 changes: 43 additions & 46 deletions src/index.js
@@ -1,5 +1,5 @@
import fs from 'fs';
import path from 'path';
import { dirname } from 'path';

import findBabelConfig from 'find-babel-config';
import glob from 'glob';
Expand All @@ -8,37 +8,58 @@ import transformCall from 'transformers/call';
import transformImport from 'transformers/import';


const defaultBabelExtensions = ['.js', '.jsx', '.es', '.es6'];
export const defaultExtensions = defaultBabelExtensions;
const defaultExtensions = ['.js', '.jsx', '.es', '.es6'];

function isRegExp(string) {
return string.startsWith('^') || string.endsWith('$');
}

export function manipulatePluginOptions(pluginOpts) {
if (pluginOpts.root) {
// eslint-disable-next-line no-param-reassign
pluginOpts.root = pluginOpts.root.reduce((resolvedDirs, dirPath) => {
function normalizeCwd(file) {
const { opts } = this;

if (opts.cwd === 'babelrc') {
const startPath = (file.opts.filename === 'unknown')
? './'
: file.opts.filename;

const { file: babelPath } = findBabelConfig.sync(startPath);

opts.cwd = babelPath
? dirname(babelPath)
: null;
}

if (!opts.cwd) {
opts.cwd = process.cwd();
}
}

function normalizePluginOptions(file) {
const { opts } = this;

normalizeCwd.call(this, file);

if (opts.root) {
opts.root = opts.root.reduce((resolvedDirs, dirPath) => {
if (glob.hasMagic(dirPath)) {
return resolvedDirs.concat(
glob.sync(dirPath).filter(p => fs.lstatSync(p).isDirectory()),
glob.sync(dirPath)
.filter(path => fs.lstatSync(path).isDirectory()),
);
}
return resolvedDirs.concat(dirPath);
}, []);
} else {
// eslint-disable-next-line no-param-reassign
pluginOpts.root = [];
opts.root = [];
}

// eslint-disable-next-line no-param-reassign
pluginOpts.regExps = [];
opts.regExps = [];

if (pluginOpts.alias) {
Object.keys(pluginOpts.alias)
if (opts.alias) {
Object.keys(opts.alias)
.filter(isRegExp)
.forEach((key) => {
const parts = pluginOpts.alias[key].split('\\\\');
const parts = opts.alias[key].split('\\\\');

function substitute(execResult) {
return parts
Expand All @@ -48,22 +69,19 @@ export function manipulatePluginOptions(pluginOpts) {
.join('\\');
}

pluginOpts.regExps.push([new RegExp(key), substitute]);
opts.regExps.push([new RegExp(key), substitute]);

// eslint-disable-next-line no-param-reassign
delete pluginOpts.alias[key];
delete opts.alias[key];
});
} else {
// eslint-disable-next-line no-param-reassign
pluginOpts.alias = {};
opts.alias = {};
}

if (!pluginOpts.extensions) {
// eslint-disable-next-line no-param-reassign
pluginOpts.extensions = defaultExtensions;
if (!opts.extensions) {
opts.extensions = defaultExtensions;
}

return pluginOpts;
return opts;
}

export default ({ types }) => {
Expand All @@ -80,28 +98,7 @@ export default ({ types }) => {
};

return {
manipulateOptions(babelOptions) {
let findPluginOptions = babelOptions.plugins.find(plugin => plugin[0] === this)[1];
findPluginOptions = manipulatePluginOptions(findPluginOptions);

this.customCWD = findPluginOptions.cwd;
},

pre(file) {
let { customCWD } = this.plugin;
if (customCWD === 'babelrc') {
const startPath = (file.opts.filename === 'unknown')
? './'
: file.opts.filename;

const { file: babelFile } = findBabelConfig.sync(startPath);
customCWD = babelFile
? path.dirname(babelFile)
: null;
}

this.cwd = customCWD || process.cwd();
},
pre: normalizePluginOptions,

visitor: {
Program: {
Expand Down
21 changes: 21 additions & 0 deletions test/index.test.js
Expand Up @@ -361,6 +361,27 @@ describe('module-resolver', () => {
);
});
});

describe('with the plugin applied twice', () => {
const doubleAliasTransformerOpts = {
plugins: [
[plugin, { root: ['.'] }],
[plugin, {
alias: {
'^@namespace/foo-(.+)': 'packages/\\1',
},
}],
],
};

it('should support replacing parts of a path', () => {
testWithImport(
'@namespace/foo-bar',
'packages/bar',
doubleAliasTransformerOpts,
);
});
});
});

describe('with custom cwd', () => {
Expand Down

0 comments on commit 944a002

Please sign in to comment.