Skip to content

Commit

Permalink
feat: Support 'packagejson' as a custom cwd option (#149)
Browse files Browse the repository at this point in the history
* Adds support for 'packagejson' cwd config.

* Updates readme.

* Moves 'packagejson' support to normalizeOptions.

* Change "describe" to "it" where appropriate
  • Loading branch information
cpsubrian authored and fatfisz committed Apr 13, 2017
1 parent e9f9bf6 commit 52600af
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Specify the plugin in your `.babelrc` with the custom root or alias. Here's an e
- `extensions`: Array of extensions used in the resolver. Override the default extensions (`['.js', '.jsx', '.es', '.es6']`).
- `cwd`: By default, the working directory is the one used for the resolver, but you can override it for your project.
- The custom value `babelrc` will make the plugin look for the closest babelrc configuration based on the file to parse.
- The custom value `packagejson` will make the plugin look for the closest `package.json` based on the file to parse.

### Regular expression alias

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"dependencies": {
"find-babel-config": "^1.0.1",
"glob": "^7.1.1",
"pkg-up": "^1.0.0",
"resolve": "^1.3.2"
},
"devDependencies": {
Expand Down
1 change: 0 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import normalizeOptions from './normalizeOptions';
import transformCall from './transformers/call';
import transformImport from './transformers/import';


const importVisitors = {
CallExpression: transformCall,
'ImportDeclaration|ExportDeclaration': transformImport,
Expand Down
13 changes: 11 additions & 2 deletions src/normalizeOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'path';

import findBabelConfig from 'find-babel-config';
import glob from 'glob';

import pkgUp from 'pkg-up';

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

Expand All @@ -22,8 +22,17 @@ function normalizeCwd(opts, file) {
opts.cwd = babelPath
? path.dirname(babelPath)
: null;
}
} else if (opts.cwd === 'packagejson') {
const startPath = (file.opts.filename === 'unknown')
? './'
: file.opts.filename;

const pkgPath = pkgUp.sync(startPath);

opts.cwd = pkgPath
? path.dirname(pkgPath)
: null;
}
if (!opts.cwd) {
opts.cwd = process.cwd();
}
Expand Down
91 changes: 91 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -597,4 +597,95 @@ describe('module-resolver', () => {
});
});
});

describe('packagejson', () => {
const transformerOpts = {
babelrc: false,
plugins: [
[plugin, {
root: './src',
alias: {
test: './test',
},
cwd: 'packagejson',
}],
],
filename: './test/testproject/src/app.js',
};

it('should resolve the sub file path', () => {
testWithImport(
'components/Root',
'./components/Root',
transformerOpts,
);
});

it('should alias the sub file path', () => {
testWithImport(
'test/tools',
'../test/tools',
transformerOpts,
);
});

describe('unknown filename', () => {
const unknownFileTransformerOpts = {
babelrc: false,
plugins: [
[plugin, {
root: './src',
cwd: 'packagejson',
}],
],
};
const cachedCwd = process.cwd();
const pkgJsonDir = 'test/testproject';

beforeEach(() => {
process.chdir(pkgJsonDir);
});

afterEach(() => {
process.chdir(cachedCwd);
});

it('should resolve the sub file path', () => {
testWithImport(
'components/Root',
'./src/components/Root',
unknownFileTransformerOpts,
);
});
});

describe('missing packagejson in path (uses cwd)', () => {
jest.mock('pkg-up', () => ({
sync: function pkgUpSync() {
return null;
},
}));
jest.resetModules();
const pluginWithMock = require.requireActual('../src').default;

const missingPkgJsonConfigTransformerOpts = {
babelrc: false,
plugins: [
[pluginWithMock, {
root: '.',
cwd: 'packagejson',
}],
],
filename: './test/testproject/src/app.js',
};

it('should resolve the sub file path', () => {
testWithImport(
'test/testproject/src/components/Root',
'./components/Root',
missingPkgJsonConfigTransformerOpts,
);
});
});
});
});
1 change: 1 addition & 0 deletions test/testproject/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}

0 comments on commit 52600af

Please sign in to comment.