Skip to content

Commit

Permalink
feat(alias): Move to Typescript (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
NotWoods committed Feb 28, 2020
1 parent 00cdfed commit d67cdff
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 47 deletions.
19 changes: 16 additions & 3 deletions packages/alias/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"homepage": "https://github.com/rollup/plugins/tree/master/packages/alias#readme",
"bugs": "https://github.com/rollup/plugins/issues",
"main": "dist/index.js",
"engines": {
"node": ">=8.0.0"
},
"scripts": {
"build": "rollup -c",
"ci:coverage": "nyc pnpm run test && nyc report --reporter=text-lcov > coverage.lcov",
Expand All @@ -19,7 +22,7 @@
"ci:test": "pnpm run test -- --verbose",
"lint": "pnpm run lint:js && pnpm run lint:docs && pnpm run lint:package",
"lint:docs": "prettier --single-quote --write README.md",
"lint:js": "eslint --fix --cache src test",
"lint:js": "eslint --fix --cache src test --ext .js,.ts",
"lint:package": "prettier --write package.json --plugin=prettier-plugin-package",
"prebuild": "del-cli dist",
"prepare": "pnpm run build",
Expand All @@ -29,7 +32,7 @@
},
"files": [
"dist",
"src",
"types",
"README.md",
"LICENSE"
],
Expand All @@ -47,16 +50,26 @@
},
"devDependencies": {
"@rollup/plugin-node-resolve": "^7.0.0",
"@rollup/plugin-typescript": "^3.0.0",
"del-cli": "^3.0.0",
"rollup": "^1.27.14"
},
"ava": {
"compileEnhancements": false,
"extensions": [
"ts"
],
"require": [
"ts-node/register"
],
"files": [
"!**/fixtures/**",
"!**/output/**",
"!**/helpers/**",
"!**/recipes/**",
"!**/types.ts"
]
},
"module": "dist/index.es2015.js"
"module": "dist/index.es.js",
"types": "types/index.d.ts"
}
9 changes: 6 additions & 3 deletions packages/alias/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import typescript from '@rollup/plugin-typescript';

import pkg from './package.json';

export default {
input: 'src/index.js',
external: [...Object.keys(pkg.dependencies), 'path', 'fs', 'os'],
input: 'src/index.ts',
external: [...Object.keys(pkg.dependencies), 'os'],
output: [
{ file: pkg.main, format: 'cjs' },
{ file: pkg.module, format: 'es' }
]
],
plugins: [typescript()]
};
40 changes: 23 additions & 17 deletions packages/alias/src/index.js → packages/alias/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { platform } from 'os';

import { PartialResolvedId, Plugin } from 'rollup';
import slash from 'slash';

import { Alias, ResolverFunction, RollupAliasOptions } from '../types';

const VOLUME = /^([A-Z]:)/i;
const IS_WINDOWS = platform() === 'win32';

// Helper functions
const noop = () => null;
const matches = (pattern, importee) => {
function matches(pattern: string | RegExp, importee: string) {
if (pattern instanceof RegExp) {
return pattern.test(importee);
}
Expand All @@ -20,16 +23,18 @@ const matches = (pattern, importee) => {
const importeeStartsWithKey = importee.indexOf(pattern) === 0;
const importeeHasSlashAfterKey = importee.substring(pattern.length)[0] === '/';
return importeeStartsWithKey && importeeHasSlashAfterKey;
};
}

const normalizeId = (id) => {
if ((IS_WINDOWS && typeof id === 'string') || VOLUME.test(id)) {
function normalizeId(id: string): string;
function normalizeId(id: string | undefined): string | undefined;
function normalizeId(id: string | undefined) {
if (typeof id === 'string' && (IS_WINDOWS || VOLUME.test(id))) {
return slash(id.replace(VOLUME, ''));
}
return id;
};
}

const getEntries = ({ entries }) => {
function getEntries({ entries }: RollupAliasOptions): Alias[] {
if (!entries) {
return [];
}
Expand All @@ -38,17 +43,18 @@ const getEntries = ({ entries }) => {
return entries;
}

return Object.keys(entries).map((key) => {
return { find: key, replacement: entries[key] };
return Object.entries(entries).map(([key, value]) => {
return { find: key, replacement: value };
});
};
}

export default function alias(options = {}) {
export default function alias(options: RollupAliasOptions = {}): Plugin {
const entries = getEntries(options);

// No aliases?
if (entries.length === 0) {
return {
name: 'alias',
resolveId: noop
};
}
Expand All @@ -69,29 +75,29 @@ export default function alias(options = {}) {
importeeId.replace(matchedEntry.find, matchedEntry.replacement)
);

let customResolver = null;
let customResolver: ResolverFunction | null = null;
if (typeof matchedEntry.customResolver === 'function') {
({ customResolver } = matchedEntry);
} else if (
typeof matchedEntry.customResolver === 'object' &&
typeof matchedEntry.customResolver.resolveId === 'function'
typeof matchedEntry.customResolver!.resolveId === 'function'
) {
customResolver = matchedEntry.customResolver.resolveId;
customResolver = matchedEntry.customResolver!.resolveId;
} else if (typeof options.customResolver === 'function') {
({ customResolver } = options);
} else if (
typeof options.customResolver === 'object' &&
typeof options.customResolver.resolveId === 'function'
typeof options.customResolver!.resolveId === 'function'
) {
customResolver = options.customResolver.resolveId;
customResolver = options.customResolver!.resolveId;
}

if (customResolver) {
return customResolver(updatedId, importerId);
}

return this.resolve(updatedId, importer, { skipSelf: true }).then((resolved) => {
let finalResult = resolved;
return this.resolve(updatedId, importer!, { skipSelf: true }).then((resolved) => {
let finalResult: PartialResolvedId | null = resolved;
if (!finalResult) {
finalResult = { id: updatedId };
}
Expand Down
11 changes: 11 additions & 0 deletions packages/alias/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"lib": ["es6"],
"module": "esnext"
},
"include": [
"src/**/*",
"types/**/*"
]
}
38 changes: 38 additions & 0 deletions packages/alias/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Plugin, ResolveIdResult } from 'rollup';

export interface Alias {
find: string | RegExp;
replacement: string;
customResolver?: ResolverFunction | ResolverObject | null;
}

export type ResolverFunction = (
source: string,
importer: string | undefined
) => Promise<ResolveIdResult> | ResolveIdResult;

export interface ResolverObject {
resolveId: ResolverFunction;
}

export interface RollupAliasOptions {
/**
* Instructs the plugin to use an alternative resolving algorithm,
* rather than the Rollup's resolver.
* @default null
*/
customResolver?: ResolverFunction | ResolverObject | null;

/**
* Specifies an `Object`, or an `Array` of `Object`,
* which defines aliases used to replace values in `import` or `require` statements.
* With either format, the order of the entries is important,
* in that the first defined rules are applied first.
*/
entries?: readonly Alias[] | { [find: string]: string };
}

/**
* 🍣 A Rollup plugin for defining aliases when bundling packages.
*/
export default function alias(options?: RollupAliasOptions): Plugin;
43 changes: 19 additions & 24 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d67cdff

Please sign in to comment.