@rollup/plugin-alias
Advanced tools
Comparing version 2.2.0 to 3.0.0
# @rollup/plugin-alias Changelog | ||
## 3.0.0 | ||
### Breaking Changes | ||
- feat(alias): built-in resolving algorithm is replaced in favor of Rollup's `this.resolve()` (#34) | ||
## 2.2.0 | ||
*2019-10-21* | ||
* Support resolving `index.js` files in directories ([#64](https://github.com/rollup/rollup-plugin-alias/pull/64) by @jerriclynsjohn) | ||
_2019-10-21_ | ||
- Support resolving `index.js` files in directories ([#64](https://github.com/rollup/rollup-plugin-alias/pull/64) by @jerriclynsjohn) | ||
## 2.1.0 | ||
*2019-10-18* | ||
* Add support for object syntax ([#61](https://github.com/rollup/rollup-plugin-alias/pull/61) by @Andarist) | ||
_2019-10-18_ | ||
- Add support for object syntax ([#61](https://github.com/rollup/rollup-plugin-alias/pull/61) by @Andarist) | ||
## 2.0.1 | ||
*2019-09-27* | ||
* Update dependencies ([#59](https://github.com/rollup/rollup-plugin-alias/pull/59) by @lukastaegert) | ||
* Make volume letter regexp case independent ([#57](https://github.com/rollup/rollup-plugin-alias/pull/57) by @MarekLacoAXA) | ||
_2019-09-27_ | ||
- Update dependencies ([#59](https://github.com/rollup/rollup-plugin-alias/pull/59) by @lukastaegert) | ||
- Make volume letter regexp case independent ([#57](https://github.com/rollup/rollup-plugin-alias/pull/57) by @MarekLacoAXA) | ||
## 2.0.0 | ||
*2019-08-22* | ||
* Add RegExp support and strict order of entries ([#53](https://github.com/rollup/rollup-plugin-alias/pull/53) by @thiscantbeserious) | ||
_2019-08-22_ | ||
- Add RegExp support and strict order of entries ([#53](https://github.com/rollup/rollup-plugin-alias/pull/53) by @thiscantbeserious) | ||
### Breaking Changes | ||
Aliases always need to be provided as an array, see #53 | ||
@@ -25,16 +40,16 @@ | ||
* Update dependencies | ||
- Update dependencies | ||
## 1.5.1 | ||
* Update tests for Rollup@1.0 compatibility and tests ([#48](https://github.com/rollup/rollup-plugin-alias/pull/48)) | ||
- Update tests for Rollup@1.0 compatibility and tests ([#48](https://github.com/rollup/rollup-plugin-alias/pull/48)) | ||
## 1.4.0 | ||
* Various Windows fixes ([#22](https://github.com/rollup/rollup-plugin-alias/pull/22)) | ||
* Don't try to alias entry file ([#29](https://github.com/rollup/rollup-plugin-alias/pull/29)) | ||
- Various Windows fixes ([#22](https://github.com/rollup/rollup-plugin-alias/pull/22)) | ||
- Don't try to alias entry file ([#29](https://github.com/rollup/rollup-plugin-alias/pull/29)) | ||
## 1.3.0 | ||
* Start maintaining a changelog | ||
* Fix `isFilePath` on Windows ([#3](https://github.com/rollup/rollup-plugin-alias/issues/3)) | ||
- Start maintaining a changelog | ||
- Fix `isFilePath` on Windows ([#3](https://github.com/rollup/rollup-plugin-alias/issues/3)) |
@@ -1,4 +0,2 @@ | ||
import fs from 'fs'; | ||
import { platform } from 'os'; | ||
import path, { posix } from 'path'; | ||
import slash from 'slash'; | ||
@@ -25,11 +23,2 @@ | ||
}; | ||
const endsWith = (needle, haystack) => haystack.slice(-needle.length) === needle; | ||
const isFilePath = (id) => /^\.?\//.test(id); | ||
const exists = (uri) => { | ||
try { | ||
return fs.statSync(uri).isFile(); | ||
} catch (e) { | ||
return false; | ||
} | ||
}; | ||
@@ -58,3 +47,2 @@ const normalizeId = (id) => { | ||
function alias(options = {}) { | ||
const resolve = Array.isArray(options.resolve) ? options.resolve : ['.js']; | ||
const entries = getEntries(options); | ||
@@ -70,2 +58,3 @@ | ||
return { | ||
name: 'alias', | ||
resolveId(importee, importer) { | ||
@@ -81,37 +70,35 @@ const importeeId = normalizeId(importee); | ||
let updatedId = normalizeId(importeeId.replace(matchedEntry.find, matchedEntry.replacement)); | ||
const updatedId = normalizeId( | ||
importeeId.replace(matchedEntry.find, matchedEntry.replacement) | ||
); | ||
if (isFilePath(updatedId)) { | ||
const directory = posix.dirname(importerId); | ||
let customResolver = null; | ||
if (typeof matchedEntry.customResolver === 'function') { | ||
({ customResolver } = matchedEntry); | ||
} else if ( | ||
typeof matchedEntry.customResolver === 'object' && | ||
typeof matchedEntry.customResolver.resolveId === 'function' | ||
) { | ||
customResolver = matchedEntry.customResolver.resolveId; | ||
} else if (typeof options.customResolver === 'function') { | ||
({ customResolver } = options); | ||
} else if ( | ||
typeof options.customResolver === 'object' && | ||
typeof options.customResolver.resolveId === 'function' | ||
) { | ||
customResolver = options.customResolver.resolveId; | ||
} | ||
// Resolve file names | ||
const filePath = posix.resolve(directory, updatedId); | ||
const match = resolve | ||
.map((ext) => (endsWith(ext, filePath) ? filePath : `${filePath}${ext}`)) | ||
.find(exists); | ||
if (customResolver) { | ||
return customResolver(updatedId, importerId); | ||
} | ||
if (match) { | ||
updatedId = match; | ||
// To keep the previous behaviour we simply return the file path | ||
// with extension | ||
} else if (endsWith('.js', filePath)) { | ||
updatedId = filePath; | ||
} else { | ||
const indexFilePath = posix.resolve(directory, `${updatedId}/index`); | ||
const defaultMatch = resolve.map((ext) => `${indexFilePath}${ext}`).find(exists); | ||
if (defaultMatch) { | ||
updatedId = defaultMatch; | ||
} else { | ||
updatedId = `${filePath}.js`; | ||
} | ||
return this.resolve(updatedId, importer, { skipSelf: true }).then((resolved) => { | ||
let finalResult = resolved; | ||
if (!finalResult) { | ||
finalResult = { id: updatedId }; | ||
} | ||
} | ||
// if alias is windows absoulate path return resolved path or | ||
// rollup on windows will throw: | ||
// [TypeError: Cannot read property 'specifier' of undefined] | ||
if (VOLUME.test(matchedEntry.replacement)) { | ||
return path.resolve(updatedId); | ||
} | ||
return updatedId; | ||
return finalResult; | ||
}); | ||
} | ||
@@ -118,0 +105,0 @@ }; |
@@ -5,6 +5,3 @@ 'use strict'; | ||
var fs = _interopDefault(require('fs')); | ||
var os = require('os'); | ||
var path = require('path'); | ||
var path__default = _interopDefault(path); | ||
var slash = _interopDefault(require('slash')); | ||
@@ -31,11 +28,2 @@ | ||
}; | ||
const endsWith = (needle, haystack) => haystack.slice(-needle.length) === needle; | ||
const isFilePath = (id) => /^\.?\//.test(id); | ||
const exists = (uri) => { | ||
try { | ||
return fs.statSync(uri).isFile(); | ||
} catch (e) { | ||
return false; | ||
} | ||
}; | ||
@@ -64,3 +52,2 @@ const normalizeId = (id) => { | ||
function alias(options = {}) { | ||
const resolve = Array.isArray(options.resolve) ? options.resolve : ['.js']; | ||
const entries = getEntries(options); | ||
@@ -76,2 +63,3 @@ | ||
return { | ||
name: 'alias', | ||
resolveId(importee, importer) { | ||
@@ -87,37 +75,35 @@ const importeeId = normalizeId(importee); | ||
let updatedId = normalizeId(importeeId.replace(matchedEntry.find, matchedEntry.replacement)); | ||
const updatedId = normalizeId( | ||
importeeId.replace(matchedEntry.find, matchedEntry.replacement) | ||
); | ||
if (isFilePath(updatedId)) { | ||
const directory = path.posix.dirname(importerId); | ||
let customResolver = null; | ||
if (typeof matchedEntry.customResolver === 'function') { | ||
({ customResolver } = matchedEntry); | ||
} else if ( | ||
typeof matchedEntry.customResolver === 'object' && | ||
typeof matchedEntry.customResolver.resolveId === 'function' | ||
) { | ||
customResolver = matchedEntry.customResolver.resolveId; | ||
} else if (typeof options.customResolver === 'function') { | ||
({ customResolver } = options); | ||
} else if ( | ||
typeof options.customResolver === 'object' && | ||
typeof options.customResolver.resolveId === 'function' | ||
) { | ||
customResolver = options.customResolver.resolveId; | ||
} | ||
// Resolve file names | ||
const filePath = path.posix.resolve(directory, updatedId); | ||
const match = resolve | ||
.map((ext) => (endsWith(ext, filePath) ? filePath : `${filePath}${ext}`)) | ||
.find(exists); | ||
if (customResolver) { | ||
return customResolver(updatedId, importerId); | ||
} | ||
if (match) { | ||
updatedId = match; | ||
// To keep the previous behaviour we simply return the file path | ||
// with extension | ||
} else if (endsWith('.js', filePath)) { | ||
updatedId = filePath; | ||
} else { | ||
const indexFilePath = path.posix.resolve(directory, `${updatedId}/index`); | ||
const defaultMatch = resolve.map((ext) => `${indexFilePath}${ext}`).find(exists); | ||
if (defaultMatch) { | ||
updatedId = defaultMatch; | ||
} else { | ||
updatedId = `${filePath}.js`; | ||
} | ||
return this.resolve(updatedId, importer, { skipSelf: true }).then((resolved) => { | ||
let finalResult = resolved; | ||
if (!finalResult) { | ||
finalResult = { id: updatedId }; | ||
} | ||
} | ||
// if alias is windows absoulate path return resolved path or | ||
// rollup on windows will throw: | ||
// [TypeError: Cannot read property 'specifier' of undefined] | ||
if (VOLUME.test(matchedEntry.replacement)) { | ||
return path__default.resolve(updatedId); | ||
} | ||
return updatedId; | ||
return finalResult; | ||
}); | ||
} | ||
@@ -124,0 +110,0 @@ }; |
{ | ||
"name": "@rollup/plugin-alias", | ||
"version": "2.2.0", | ||
"version": "3.0.0", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"description": "Resolves aliases with Rollup", | ||
"description": "Define and resolve aliases for bundle dependencies", | ||
"license": "MIT", | ||
@@ -17,4 +17,3 @@ "repository": "rollup/plugins", | ||
"ci:coverage": "nyc pnpm run test && nyc report --reporter=text-lcov > coverage.lcov", | ||
"ci:coverage:submit": "curl -s https://codecov.io/bash | bash -s - -F alias", | ||
"ci:lint": "pnpm run build && pnpm run lint && pnpm run security", | ||
"ci:lint": "pnpm run build && pnpm run lint", | ||
"ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}", | ||
@@ -27,6 +26,5 @@ "ci:test": "pnpm run test -- --verbose", | ||
"prebuild": "del-cli dist", | ||
"prepare": "npm run build", | ||
"prepublishOnly": "npm run lint && npm run test", | ||
"pretest": "npm run build", | ||
"security": "echo 'pnpm needs `npm audit` support'", | ||
"prepare": "pnpm run build", | ||
"prepublishOnly": "pnpm run lint && pnpm run test", | ||
"pretest": "pnpm run build", | ||
"test": "ava" | ||
@@ -54,3 +52,4 @@ }, | ||
"del-cli": "^3.0.0", | ||
"rollup": "^1.20.0" | ||
"rollup": "^1.20.0", | ||
"rollup-plugin-node-resolve": "^5.2.0" | ||
}, | ||
@@ -65,4 +64,3 @@ "ava": { | ||
}, | ||
"jsnext:main": "dist/index.es2015.js", | ||
"module": "dist/index.es2015.js" | ||
} |
@@ -1,7 +0,7 @@ | ||
[cover]: https://codecov.io/gh/rollup/plugins/alias/branch/master/graph/badge.svg | ||
[cover-url]: https://codecov.io/gh/rollup/plugins/alias | ||
[npm]: https://img.shields.io/npm/v/@rollup/plugin-alias | ||
[npm-url]: https://www.npmjs.com/package/@rollup/plugin-alias | ||
[size]: https://packagephobia.now.sh/badge?p=@rollup/plugin-alias | ||
[size-url]: https://packagephobia.now.sh/result?p=@rollup/plugin-alias | ||
[![cover][cover]][cover-url] | ||
[![npm][npm]][npm-url] | ||
[![size][size]][size-url] | ||
@@ -40,2 +40,4 @@ [![libera manifesto](https://img.shields.io/badge/libera-manifesto-lightgrey.svg)](https://liberamanifesto.com) | ||
npm install @rollup/plugin-alias --save-dev | ||
# or | ||
yarn add -D @rollup/plugin-alias | ||
``` | ||
@@ -56,3 +58,10 @@ | ||
}, | ||
plugins: [alias({ resolve: ['.jsx', '.js'] })] | ||
plugins: [ | ||
alias({ | ||
entries: [ | ||
{ find: 'utils', replacement: '../../../utils' }, | ||
{ find: 'batman-1.0.0', replacement: './joker-1.5.0' } | ||
] | ||
}) | ||
] | ||
}; | ||
@@ -65,2 +74,9 @@ ``` | ||
### `customResolver` | ||
Type: `Function | Object`<br> | ||
Default: `null` | ||
Instructs the plugin to use an alternative resolving algorithm, rather than the Rollup's resolver. Please refer to the [Rollup documentation](https://rollupjs.org/guide/en/#hooks) for more information about the `resolveId` hook. For a detailed example, see: [Custom Resolvers](#custom-resolvers). | ||
### `entries` | ||
@@ -97,13 +113,2 @@ | ||
### `resolve` | ||
Type: `Array[String]`<br> | ||
Default: `['.js']` | ||
Specifies an array of file extensions to use when attempting to resolve an `import` (or `require`). The extensions will be tried in the order they are specified. By default, this option is configured to resolve only files that have the `.js` extension. For example; to resolve both `JSX` and `JS` files: | ||
```js | ||
alias({ resolve: ['.jsx', '.js'] }); | ||
``` | ||
## Regular Expression Aliases | ||
@@ -124,11 +129,51 @@ | ||
```js | ||
{ find:/^(.*)\.js$/, replacement: '$1.wasm' } | ||
{ find:/^(.*)\.js$/, replacement: '$1.alias' } | ||
``` | ||
This would replace the file extension for all imports ending with `.js` to `.wasm`. | ||
This would replace the file extension for all imports ending with `.js` to `.alias`. | ||
## Resolving algorithm | ||
This plugin uses resolver plugins specified for Rollup and eventually Rollup default algorithm. If you rely on Node specific features, you probably want [rollup-plugin-node-resolve](https://www.npmjs.com/package/rollup-plugin-node-resolve) in your setup. | ||
## Custom Resolvers | ||
The `customResolver` option can be leveraged to provide separate module resolution for an individual alias. | ||
Example: | ||
```javascript | ||
// rollup.config.js | ||
import alias from '@rollup/plugin-alias'; | ||
import resolve from 'rollup-plugin-node-resolve'; | ||
const customResolver = resolve({ | ||
extensions: ['.mjs', '.js', '.jsx', '.json', '.sass', '.scss'] | ||
}); | ||
const projectRootDir = path.resolve(__dirname); | ||
export default { | ||
// ... | ||
plugins: [ | ||
alias({ | ||
entries: [ | ||
{ | ||
find: 'src', | ||
replacement: path.resolve(projectRootDir, 'src') | ||
// OR place `customResolver` here. See explanation below. | ||
} | ||
], | ||
customResolver | ||
}), | ||
resolve() | ||
] | ||
}; | ||
``` | ||
In the example above the alias `src` is used, which uses the `node-resolve` algorithm for files _aliased_ with `src`, by passing the `customResolver` option. The `resolve()` plugin is kept separate in the plugins list for other files which are not _aliased_ with `src`. The `customResolver` option can be passed inside each `entries` item for granular control over resolving allowing each alias a preferred resolver. | ||
## Meta | ||
[CONTRIBUTING](./.github/CONTRIBUTING.md) | ||
[CONTRIBUTING](/.github/CONTRIBUTING.md) | ||
[LICENSE (MIT)](./LICENSE) | ||
[LICENSE (MIT)](/LICENSE) |
@@ -1,4 +0,2 @@ | ||
import fs from 'fs'; | ||
import { platform } from 'os'; | ||
import path, { posix } from 'path'; | ||
@@ -26,11 +24,2 @@ import slash from 'slash'; | ||
}; | ||
const endsWith = (needle, haystack) => haystack.slice(-needle.length) === needle; | ||
const isFilePath = (id) => /^\.?\//.test(id); | ||
const exists = (uri) => { | ||
try { | ||
return fs.statSync(uri).isFile(); | ||
} catch (e) { | ||
return false; | ||
} | ||
}; | ||
@@ -59,3 +48,2 @@ const normalizeId = (id) => { | ||
export default function alias(options = {}) { | ||
const resolve = Array.isArray(options.resolve) ? options.resolve : ['.js']; | ||
const entries = getEntries(options); | ||
@@ -71,2 +59,3 @@ | ||
return { | ||
name: 'alias', | ||
resolveId(importee, importer) { | ||
@@ -82,39 +71,37 @@ const importeeId = normalizeId(importee); | ||
let updatedId = normalizeId(importeeId.replace(matchedEntry.find, matchedEntry.replacement)); | ||
const updatedId = normalizeId( | ||
importeeId.replace(matchedEntry.find, matchedEntry.replacement) | ||
); | ||
if (isFilePath(updatedId)) { | ||
const directory = posix.dirname(importerId); | ||
let customResolver = null; | ||
if (typeof matchedEntry.customResolver === 'function') { | ||
({ customResolver } = matchedEntry); | ||
} else if ( | ||
typeof matchedEntry.customResolver === 'object' && | ||
typeof matchedEntry.customResolver.resolveId === 'function' | ||
) { | ||
customResolver = matchedEntry.customResolver.resolveId; | ||
} else if (typeof options.customResolver === 'function') { | ||
({ customResolver } = options); | ||
} else if ( | ||
typeof options.customResolver === 'object' && | ||
typeof options.customResolver.resolveId === 'function' | ||
) { | ||
customResolver = options.customResolver.resolveId; | ||
} | ||
// Resolve file names | ||
const filePath = posix.resolve(directory, updatedId); | ||
const match = resolve | ||
.map((ext) => (endsWith(ext, filePath) ? filePath : `${filePath}${ext}`)) | ||
.find(exists); | ||
if (customResolver) { | ||
return customResolver(updatedId, importerId); | ||
} | ||
if (match) { | ||
updatedId = match; | ||
// To keep the previous behaviour we simply return the file path | ||
// with extension | ||
} else if (endsWith('.js', filePath)) { | ||
updatedId = filePath; | ||
} else { | ||
const indexFilePath = posix.resolve(directory, `${updatedId}/index`); | ||
const defaultMatch = resolve.map((ext) => `${indexFilePath}${ext}`).find(exists); | ||
if (defaultMatch) { | ||
updatedId = defaultMatch; | ||
} else { | ||
updatedId = `${filePath}.js`; | ||
} | ||
return this.resolve(updatedId, importer, { skipSelf: true }).then((resolved) => { | ||
let finalResult = resolved; | ||
if (!finalResult) { | ||
finalResult = { id: updatedId }; | ||
} | ||
} | ||
// if alias is windows absoulate path return resolved path or | ||
// rollup on windows will throw: | ||
// [TypeError: Cannot read property 'specifier' of undefined] | ||
if (VOLUME.test(matchedEntry.replacement)) { | ||
return path.resolve(updatedId); | ||
} | ||
return updatedId; | ||
return finalResult; | ||
}); | ||
} | ||
}; | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
175
1
18382
3
265