Skip to content

Commit 52600af

Browse files
cpsubrianfatfisz
authored andcommitted
feat: Support 'packagejson' as a custom cwd option (#149)
* Adds support for 'packagejson' cwd config. * Updates readme. * Moves 'packagejson' support to normalizeOptions. * Change "describe" to "it" where appropriate
1 parent e9f9bf6 commit 52600af

File tree

6 files changed

+105
-3
lines changed

6 files changed

+105
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Specify the plugin in your `.babelrc` with the custom root or alias. Here's an e
5252
- `extensions`: Array of extensions used in the resolver. Override the default extensions (`['.js', '.jsx', '.es', '.es6']`).
5353
- `cwd`: By default, the working directory is the one used for the resolver, but you can override it for your project.
5454
- The custom value `babelrc` will make the plugin look for the closest babelrc configuration based on the file to parse.
55+
- The custom value `packagejson` will make the plugin look for the closest `package.json` based on the file to parse.
5556

5657
### Regular expression alias
5758

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"dependencies": {
3333
"find-babel-config": "^1.0.1",
3434
"glob": "^7.1.1",
35+
"pkg-up": "^1.0.0",
3536
"resolve": "^1.3.2"
3637
},
3738
"devDependencies": {

src/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import normalizeOptions from './normalizeOptions';
22
import transformCall from './transformers/call';
33
import transformImport from './transformers/import';
44

5-
65
const importVisitors = {
76
CallExpression: transformCall,
87
'ImportDeclaration|ExportDeclaration': transformImport,

src/normalizeOptions.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from 'path';
33

44
import findBabelConfig from 'find-babel-config';
55
import glob from 'glob';
6-
6+
import pkgUp from 'pkg-up';
77

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

@@ -22,8 +22,17 @@ function normalizeCwd(opts, file) {
2222
opts.cwd = babelPath
2323
? path.dirname(babelPath)
2424
: null;
25-
}
25+
} else if (opts.cwd === 'packagejson') {
26+
const startPath = (file.opts.filename === 'unknown')
27+
? './'
28+
: file.opts.filename;
29+
30+
const pkgPath = pkgUp.sync(startPath);
2631

32+
opts.cwd = pkgPath
33+
? path.dirname(pkgPath)
34+
: null;
35+
}
2736
if (!opts.cwd) {
2837
opts.cwd = process.cwd();
2938
}

test/index.test.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,4 +597,95 @@ describe('module-resolver', () => {
597597
});
598598
});
599599
});
600+
601+
describe('packagejson', () => {
602+
const transformerOpts = {
603+
babelrc: false,
604+
plugins: [
605+
[plugin, {
606+
root: './src',
607+
alias: {
608+
test: './test',
609+
},
610+
cwd: 'packagejson',
611+
}],
612+
],
613+
filename: './test/testproject/src/app.js',
614+
};
615+
616+
it('should resolve the sub file path', () => {
617+
testWithImport(
618+
'components/Root',
619+
'./components/Root',
620+
transformerOpts,
621+
);
622+
});
623+
624+
it('should alias the sub file path', () => {
625+
testWithImport(
626+
'test/tools',
627+
'../test/tools',
628+
transformerOpts,
629+
);
630+
});
631+
632+
describe('unknown filename', () => {
633+
const unknownFileTransformerOpts = {
634+
babelrc: false,
635+
plugins: [
636+
[plugin, {
637+
root: './src',
638+
cwd: 'packagejson',
639+
}],
640+
],
641+
};
642+
const cachedCwd = process.cwd();
643+
const pkgJsonDir = 'test/testproject';
644+
645+
beforeEach(() => {
646+
process.chdir(pkgJsonDir);
647+
});
648+
649+
afterEach(() => {
650+
process.chdir(cachedCwd);
651+
});
652+
653+
it('should resolve the sub file path', () => {
654+
testWithImport(
655+
'components/Root',
656+
'./src/components/Root',
657+
unknownFileTransformerOpts,
658+
);
659+
});
660+
});
661+
662+
describe('missing packagejson in path (uses cwd)', () => {
663+
jest.mock('pkg-up', () => ({
664+
sync: function pkgUpSync() {
665+
return null;
666+
},
667+
}));
668+
jest.resetModules();
669+
const pluginWithMock = require.requireActual('../src').default;
670+
671+
const missingPkgJsonConfigTransformerOpts = {
672+
babelrc: false,
673+
plugins: [
674+
[pluginWithMock, {
675+
root: '.',
676+
cwd: 'packagejson',
677+
}],
678+
],
679+
filename: './test/testproject/src/app.js',
680+
};
681+
682+
it('should resolve the sub file path', () => {
683+
testWithImport(
684+
'test/testproject/src/components/Root',
685+
'./components/Root',
686+
missingPkgJsonConfigTransformerOpts,
687+
);
688+
});
689+
});
690+
});
600691
});

test/testproject/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

0 commit comments

Comments
 (0)