Skip to content
This repository was archived by the owner on Aug 4, 2021. It is now read-only.

Commit 31fb414

Browse files
committed
Initial version
1 parent 3b5cbca commit 31fb414

File tree

8 files changed

+111
-0
lines changed

8 files changed

+111
-0
lines changed

package.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"name": "rollup-plugin-alias",
3+
"version": "1.0.0",
4+
"description": "Resolves index.js files with Rollup",
5+
"main": "dist/rollup-plugin-alias.js",
6+
"jsnext:main": "dist/rollup-plugin-alias.es2015.js",
7+
"scripts": {
8+
"prebuild": "rimraf dist",
9+
"build": "rollup-babel-lib-bundler -f cjs,es6 src/index.js",
10+
"prepublish": "npm run build",
11+
"lint": "eslint .",
12+
"pretest": "npm-run-all -p build lint",
13+
"test": "ava test/index.js",
14+
"coverage": "nyc npm test"
15+
},
16+
"repository": {
17+
"type": "git",
18+
"url": "git+https://github.com/frostney/rollup-plugin-alias.git"
19+
},
20+
"keywords": [
21+
"rollup",
22+
"rollup-plugin",
23+
"resolve",
24+
"alias"
25+
],
26+
"files": [
27+
"dist",
28+
"src"
29+
],
30+
"author": "Johannes Stein",
31+
"license": "MIT",
32+
"bugs": {
33+
"url": "https://github.com/frostney/rollup-plugin-alias/issues"
34+
},
35+
"homepage": "https://github.com/frostney/rollup-plugin-aliar#readme",
36+
"devDependencies": {
37+
"ava": "^0.12.0",
38+
"babel-preset-es2015-loose-rollup": "^7.0.0",
39+
"coveralls": "^2.11.8",
40+
"eslint": "^2.2.0",
41+
"eslint-config-airbnb": "^6.0.2",
42+
"npm-run-all": "^1.5.1",
43+
"nyc": "^5.6.0",
44+
"rimraf": "^2.5.2",
45+
"rollup": "^0.25.4",
46+
"rollup-babel-lib-bundler": "^2.2.4"
47+
}
48+
}

src/.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["es2015-loose-rollup"]
3+
}

src/index.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import path from 'path';
2+
3+
export default function alias(options = {}) {
4+
return {
5+
resolveId(importee, importer) {
6+
if (Object.keys(options).length === 0) {
7+
return null;
8+
}
9+
10+
const aliasKeys = Object.keys(options);
11+
const aliasIndex = aliasKeys.indexOf(importee);
12+
13+
if (aliasIndex >= 0) {
14+
const entry = options[aliasKeys[aliasIndex]];
15+
16+
if (entry.indexOf('./') === 0) {
17+
const basename = path.basename(importer);
18+
const directory = importer.split(basename)[0];
19+
20+
// TODO: Is there a way not to have the extension being defined explicitly?
21+
return path.resolve(directory, entry) + '.js';
22+
}
23+
24+
return entry;
25+
}
26+
27+
return null;
28+
},
29+
};
30+
}

test/files/aliasMe.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 42;

test/files/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import nonAliased from './nonAliased';
2+
import fancyNumber from 'fancyNumber';
3+
import anotherFancyNumber from './anotherFancyNumber';
4+
5+
export default fancyNumber + anotherFancyNumber + nonAliased;

test/files/localAliasMe.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 42;

test/files/nonAliased.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 24;

test/index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import test from 'ava';
2+
3+
import path from 'path';
4+
5+
import { rollup } from 'rollup';
6+
import alias from '../dist/rollup-plugin-alias';
7+
8+
test(t =>
9+
rollup({
10+
entry: './files/index.js',
11+
plugins: [alias({
12+
fancyNumber: './aliasMe',
13+
'./anotherFancyNumber': './localAliasMe',
14+
})],
15+
}).then(stats => {
16+
t.is(path.basename(stats.modules[0].id), 'nonAliased.js');
17+
t.is(path.basename(stats.modules[1].id), 'aliasMe.js');
18+
t.is(path.basename(stats.modules[2].id), 'localAliasMe.js');
19+
t.is(path.basename(stats.modules[3].id), 'index.js');
20+
t.is(stats.modules.length, 4);
21+
})
22+
);

0 commit comments

Comments
 (0)