From 6e3b66e1871864d9a5278ad12ba9de11f420e81c Mon Sep 17 00:00:00 2001 From: MoeSattler Date: Fri, 26 Aug 2016 15:36:39 +0200 Subject: [PATCH] feat: add glob support --- README.md | 19 +++++++++++---- appveyor.yml | 21 +++++++++++++++++ package.json | 1 + src/index.js | 12 ++++++++++ test/index.js | 64 +++++++++++++++++++++++++++++++++------------------ 5 files changed, 89 insertions(+), 28 deletions(-) create mode 100644 appveyor.yml diff --git a/README.md b/README.md index 76360fe..ccdd67d 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # babel-plugin-module-resolver -[![npm][npm-version-image]][npm-url] [![Build Status][ci-image]][ci-url] [![Coverage Status][coverage-image]][coverage-url] +[![Maintenance Status][status-image]][status-url] [![NPM version][npm-image]][npm-url] [![Build Status Linux][circleci-image]][circleci-url] [![Build Status Windows][appveyor-image]][appveyor-url] [![Coverage Status][coverage-image]][coverage-url] A [babel](http://babeljs.io) plugin to add a new resolver for your modules when compiling your code using Babel. The plugin allows you to add new "root" directories that contains your modules. It also allows your to setup custom alias which can also be directories or specific files, or even other npm modules. @@ -55,12 +55,21 @@ If you're using ESLint, you should use the [eslint-plugin-import][eslint-plugin- MIT, see [LICENSE.md](/LICENSE.md) for details. -[ci-image]: https://circleci.com/gh/tleunen/babel-plugin-module-resolver.svg?style=shield -[ci-url]: https://circleci.com/gh/tleunen/babel-plugin-module-resolver +[status-image]: https://img.shields.io/badge/status-maintained-brightgreen.svg +[status-url]: https://github.com/tleunen/babel-plugin-module-resolver + +[npm-image]: https://img.shields.io/npm/v/babel-plugin-module-resolver.svg +[npm-url]: https://www.npmjs.com/package/babel-plugin-module-resolver + +[circleci-image]: https://img.shields.io/circleci/project/tleunen/babel-plugin-module-resolver/master.svg?logo=data%3Aimage%2Fsvg%2Bxml%3Bbase64%2CPHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB2aWV3Qm94PSItMTQyLjUgLTE0Mi41IDI4NSAyODUiPjxjaXJjbGUgcj0iMTQxLjciIGZpbGw9IiNERDQ4MTQiLz48ZyBpZD0iYSIgZmlsbD0iI0ZGRiI%2BPGNpcmNsZSBjeD0iLTk2LjQiIHI9IjE4LjkiLz48cGF0aCBkPSJNLTQ1LjYgNjguNGMtMTYuNi0xMS0yOS0yOC0zNC00Ny44IDYtNSA5LjgtMTIuMyA5LjgtMjAuNnMtMy44LTE1LjctOS44LTIwLjZjNS0xOS44IDE3LjQtMzYuNyAzNC00Ny44bDEzLjggMjMuMkMtNDYtMzUuMi01NS4zLTE4LjctNTUuMyAwYzAgMTguNyA5LjMgMzUuMiAyMy41IDQ1LjJ6Ii8%2BPC9nPjx1c2UgeGxpbms6aHJlZj0iI2EiIHRyYW5zZm9ybT0icm90YXRlKDEyMCkiLz48dXNlIHhsaW5rOmhyZWY9IiNhIiB0cmFuc2Zvcm09InJvdGF0ZSgyNDApIi8%2BPC9zdmc%2B +[circleci-url]: https://circleci.com/gh/tleunen/babel-plugin-module-resolver + +[appveyor-image]: https://img.shields.io/appveyor/ci/tleunen/babel-plugin-module-resolver/master.svg?logo=data%3Aimage%2Fsvg%2Bxml%3Bbase64%2CPHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZlcnNpb249IjEuMSIgd2lkdGg9IjEyOCIgaGVpZ2h0PSIxMjgiIHZpZXdCb3g9IjAgMCAxMjggMTI4Ij48ZyBmaWxsPSIjMUJBMUUyIiB0cmFuc2Zvcm09InNjYWxlKDgpIj48cGF0aCBkPSJNMCAyLjI2NWw2LjUzOS0uODg4LjAwMyA2LjI4OC02LjUzNi4wMzd6Ii8%2BPHBhdGggZD0iTTYuNTM2IDguMzlsLjAwNSA2LjI5My02LjUzNi0uODk2di01LjQ0eiIvPjxwYXRoIGQ9Ik03LjMyOCAxLjI2MWw4LjY3LTEuMjYxdjcuNTg1bC04LjY3LjA2OXoiLz48cGF0aCBkPSJNMTYgOC40NDlsLS4wMDIgNy41NTEtOC42Ny0xLjIyLS4wMTItNi4zNDV6Ii8%2BPC9nPjwvc3ZnPg== +[appveyor-url]: https://ci.appveyor.com/project/tleunen/babel-plugin-module-resolver + [coverage-image]: https://codecov.io/gh/tleunen/babel-plugin-module-resolver/branch/master/graph/badge.svg [coverage-url]: https://codecov.io/gh/tleunen/babel-plugin-module-resolver -[npm-version-image]: https://img.shields.io/npm/v/babel-plugin-module-resolver.svg -[npm-url]: https://www.npmjs.com/package/babel-plugin-module-resolver + [eslint-import-resolver-babel-module]: https://github.com/tleunen/eslint-import-resolver-babel-module [eslint-plugin-import]: https://github.com/benmosher/eslint-plugin-import [atom-autocomplete-modules]: https://github.com/nkt/atom-autocomplete-modules diff --git a/appveyor.yml b/appveyor.yml new file mode 100644 index 0000000..f1b05c7 --- /dev/null +++ b/appveyor.yml @@ -0,0 +1,21 @@ +environment: + matrix: + - nodejs_version: '4' + - nodejs_version: '5' + - nodejs_version: '6' +install: + - ps: Install-Product node $env:nodejs_version + - set CI=true + - npm -g install npm@latest + - set PATH=%APPDATA%\npm;%PATH% + - npm install +matrix: + fast_finish: true +build: off +version: '{build}' +shallow_clone: true +clone_depth: 1 +test_script: + - node --version + - npm --version + - npm run test:suite diff --git a/package.json b/package.json index 473d49d..5eb9333 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "import" ], "dependencies": { + "glob": "^7.0.6", "resolve": "^1.1.7" }, "devDependencies": { diff --git a/src/index.js b/src/index.js index 6511b16..942b0be 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,6 @@ import path from 'path'; import resolve from 'resolve'; +import glob from 'glob'; import mapToRelative from './mapToRelative'; function createAliasFileMap(pluginOpts) { @@ -110,6 +111,17 @@ export default ({ types: t }) => { } return { + manipulateOptions(babelOptions) { + const findPluginOptions = babelOptions.plugins.find(plugin => plugin[0] === this)[1]; + if (findPluginOptions.root) { + findPluginOptions.root = findPluginOptions.root.reduce((resolvedDirs, dirPath) => { + if (glob.hasMagic(dirPath)) { + return resolvedDirs.concat(glob.sync(dirPath)); + } + return resolvedDirs.concat(dirPath); + }, []); + } + }, visitor: { CallExpression: { exit(nodePath, state) { diff --git a/test/index.js b/test/index.js index 8fb0211..f36ff06 100644 --- a/test/index.js +++ b/test/index.js @@ -1,17 +1,19 @@ /* eslint-env mocha */ +/* eslint-disable prefer-arrow-callback */ +/* eslint-disable func-names */ import assert from 'assert'; import { transform } from 'babel-core'; // eslint-disable-line import/no-extraneous-dependencies import plugin from '../src'; function testRequireImport(source, output, transformerOpts) { - it('with a require statement', () => { + it('with a require statement', function () { const code = `var something = require("${source}");`; const result = transform(code, transformerOpts); assert.strictEqual(result.code, `var something = require("${output}");`); }); - it('with an import statement', () => { + it('with an import statement', function () { const code = `import something from "${source}";`; const result = transform(code, transformerOpts); @@ -19,7 +21,7 @@ function testRequireImport(source, output, transformerOpts) { }); } -describe('root', () => { +describe('root', function () { const transformerOpts = { plugins: [ [plugin, { @@ -28,7 +30,15 @@ describe('root', () => { ] }; - describe('should rewrite the file path inside a root directory', () => { + const transformerOptsGlob = { + plugins: [ + [plugin, { + root: ['./test/**/components'] + }] + ] + }; + + describe('should rewrite the file path inside a root directory', function () { testRequireImport( 'c1', './test/examples/components/c1', @@ -36,7 +46,7 @@ describe('root', () => { ); }); - describe('should rewrite the sub file path inside a root directory', () => { + describe('should rewrite the sub file path inside a root directory', function () { testRequireImport( 'sub/sub1', './test/examples/components/sub/sub1', @@ -44,7 +54,7 @@ describe('root', () => { ); }); - describe('should rewrite the file while keeping the extension', () => { + describe('should rewrite the file while keeping the extension', function () { testRequireImport( 'sub/sub1.css', './test/examples/components/sub/sub1.css', @@ -52,7 +62,7 @@ describe('root', () => { ); }); - describe('should rewrite the file with a filename containing a dot', () => { + describe('should rewrite the file with a filename containing a dot', function () { testRequireImport( 'sub/custom.modernizr3', './test/examples/components/sub/custom.modernizr3', @@ -60,16 +70,24 @@ describe('root', () => { ); }); - describe('should not rewrite a path outisde of the root directory', () => { + describe('should not rewrite a path outisde of the root directory', function () { testRequireImport( 'example-file', 'example-file', transformerOpts ); }); + + describe('should rewrite the file path inside a root directory according to glob', function () { + testRequireImport( + 'c1', + './test/examples/components/c1', + transformerOptsGlob + ); + }); }); -describe('alias', () => { +describe('alias', function () { const transformerOpts = { plugins: [ [plugin, { @@ -83,9 +101,9 @@ describe('alias', () => { ] }; - describe('should alias a known path', () => { - describe('using a simple exposed name', () => { - describe('when requiring the exact name', () => { + describe('should alias a known path', function () { + describe('using a simple exposed name', function () { + describe('when requiring the exact name', function () { testRequireImport( 'utils', './src/mylib/subfolder/utils', @@ -93,7 +111,7 @@ describe('alias', () => { ); }); - describe('when requiring a sub file of the exposed name', () => { + describe('when requiring a sub file of the exposed name', function () { testRequireImport( 'utils/my-util-file', './src/mylib/subfolder/utils/my-util-file', @@ -102,8 +120,8 @@ describe('alias', () => { }); }); - describe('using a "complex" exposed name', () => { - describe('when requiring the exact name', () => { + describe('using a "complex" exposed name', function () { + describe('when requiring the exact name', function () { testRequireImport( 'awesome/components', './src/components', @@ -111,7 +129,7 @@ describe('alias', () => { ); }); - describe('when requiring a sub file of the exposed name', () => { + describe('when requiring a sub file of the exposed name', function () { testRequireImport( 'awesome/components/my-comp', './src/components/my-comp', @@ -120,7 +138,7 @@ describe('alias', () => { }); }); - describe('with a dot in the filename', () => { + describe('with a dot in the filename', function () { testRequireImport( 'utils/custom.modernizr3', './src/mylib/subfolder/utils/custom.modernizr3', @@ -129,7 +147,7 @@ describe('alias', () => { }); }); - describe('should alias the path with its extension', () => { + describe('should alias the path with its extension', function () { testRequireImport( 'awesome/components/my-comp.css', './src/components/my-comp.css', @@ -137,8 +155,8 @@ describe('alias', () => { ); }); - describe('should not alias a unknown path', () => { - describe('when requiring a node module', () => { + describe('should not alias a unknown path', function () { + describe('when requiring a node module', function () { testRequireImport( 'other-lib', 'other-lib', @@ -146,7 +164,7 @@ describe('alias', () => { ); }); - describe('when requiring a specific un-mapped file', () => { + describe('when requiring a specific un-mapped file', function () { testRequireImport( './l/otherLib', './l/otherLib', @@ -155,7 +173,7 @@ describe('alias', () => { }); }); - describe('(legacy) should support aliasing a node module with "npm:"', () => { + describe('(legacy) should support aliasing a node module with "npm:"', function () { testRequireImport( 'abstract/thing', 'concrete/thing', @@ -163,7 +181,7 @@ describe('alias', () => { ); }); - describe('should support aliasing a node modules', () => { + describe('should support aliasing a node modules', function () { testRequireImport( 'underscore/map', 'lodash/map',