diff --git a/README.md b/README.md index 2b1de53..d2dd553 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/package.json b/package.json index 5f1a027..0411459 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/index.js b/src/index.js index 8ed62d1..dd18ab9 100644 --- a/src/index.js +++ b/src/index.js @@ -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, diff --git a/src/normalizeOptions.js b/src/normalizeOptions.js index 592430e..5bd073f 100644 --- a/src/normalizeOptions.js +++ b/src/normalizeOptions.js @@ -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']; @@ -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(); } diff --git a/test/index.test.js b/test/index.test.js index 4d7e079..b91148f 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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, + ); + }); + }); + }); }); diff --git a/test/testproject/package.json b/test/testproject/package.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/test/testproject/package.json @@ -0,0 +1 @@ +{} \ No newline at end of file