diff --git a/README.md b/README.md index 9147713..fc26b62 100644 --- a/README.md +++ b/README.md @@ -94,12 +94,10 @@ The paths `"src/js"` and `"./src/js"` behave the same. ### Custom rootPathPrefix -If you don't like the `~` syntax you can use your own symbol (for example an `#` symbol or `\`). Using +If you don't like the `~` syntax you can use your own symbol (for example an `#` symbol or `\` or anything you want). Using `@` is not recommended, as recent versions of NPM allow `@` in package names. `~` is the default since it's very unlikely to conflict with anything (and wouldn't be expanded to HOME anyway). -This **must** be 1 or 2 characters. Any additional characters are ignored. - ```javascript // // Waiting this change: https://github.com/entwicklerstube/babel-plugin-root-import/pull/97 @@ -203,6 +201,10 @@ Sometimes tooling might not be up to scratch, meaning you lose features such as ## Change Log +#### 6.2.0 - 2019-05-09 + +- Remove the 2 characters restriction + #### 6.1.0 - 2018-06-23 - Supports babel 7 diff --git a/package.json b/package.json index ddad906..510b765 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "babel-plugin-root-import", - "version": "6.1.0", + "version": "6.2.0", "description": "Babel Plugin to enable relative root-import", "author": "Michael J. Zoidl ", "license": "MIT", diff --git a/plugin/helper.js b/plugin/helper.js index 5624ebc..751c5b0 100644 --- a/plugin/helper.js +++ b/plugin/helper.js @@ -1,33 +1,14 @@ import slash from 'slash'; import path from 'path'; - const root = slash(global.rootPath || process.cwd()); export const hasRootPathPrefixInString = (importPath, rootPathPrefix = '~') => { - let containsRootPathPrefix = false; - - if (typeof importPath === 'string') { - if (importPath.substring(0, 1) === rootPathPrefix) { - containsRootPathPrefix = true; - } - - const firstTwoCharactersOfString = importPath.substring(0, 2); - if (firstTwoCharactersOfString === `${rootPathPrefix}/`) { - containsRootPathPrefix = true; - } - } - - return containsRootPathPrefix; + return !!(typeof importPath === 'string' && importPath.indexOf(rootPathPrefix) === 0); }; export const transformRelativeToRootPath = (importPath, rootPathSuffix, rootPathPrefix, sourceFile = '') => { - let withoutRootPathPrefix = ''; if (hasRootPathPrefixInString(importPath, rootPathPrefix)) { - if (importPath.substring(0, 1) === '/') { - withoutRootPathPrefix = importPath.substring(1, importPath.length); - } else { - withoutRootPathPrefix = importPath.substring(2, importPath.length); - } + const withoutRootPathPrefix = importPath.replace(rootPathPrefix, ''); const absolutePath = path.resolve(`${rootPathSuffix ? rootPathSuffix : './'}/${withoutRootPathPrefix}`); let sourcePath = sourceFile.substring(0, sourceFile.lastIndexOf('/')); diff --git a/test/helper.spec.js b/test/helper.spec.js index e1f1adf..89d3746 100644 --- a/test/helper.spec.js +++ b/test/helper.spec.js @@ -29,6 +29,12 @@ describe('helper#transformRelativeToRootPath', () => { expect(result).to.not.equal(`${path.resolve('../shared')}/util/test/../test/test.js`); }); + it('works with long prefix and special characters', () => { + const rootPath = slash('./path'); + const result = transformRelativeToRootPath('common$^plop/some/path', '', 'common$^plop', 'some/file.js'); + expect(result).to.equal(rootPath); + }); + it('throws error if no string is passed', () => { expect(() => { transformRelativeToRootPath(); @@ -49,6 +55,13 @@ describe('helper#hasRootPathPrefixInString', () => { expect(withRootPathPrefix).to.be.true; }); + it('check if "common" is at the beginning of the string', () => { + const withoutRootPathPrefix = hasRootPathPrefixInString('some/path', 'common'); + const withRootPathPrefix = hasRootPathPrefixInString('common/some/path', 'common'); + expect(withoutRootPathPrefix).to.be.false; + expect(withRootPathPrefix).to.be.true; + }); + it('returns false if no string is passed', () => { const nothingPassed = hasRootPathPrefixInString(); const wrongTypePassed = hasRootPathPrefixInString([]);