Skip to content

Commit a6ff567

Browse files
authoredSep 18, 2020
feat(virtual): Move to Typescript (rollup#578)
1 parent 6cd15b9 commit a6ff567

File tree

7 files changed

+90
-38
lines changed

7 files changed

+90
-38
lines changed
 

‎packages/virtual/package.json

+12
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
"bugs": "https://github.com/rollup/rollup-plugin-virtual/issues",
1313
"main": "dist/index.js",
1414
"module": "dist/index.es.js",
15+
"engines": {
16+
"node": ">=8.0.0"
17+
},
1518
"scripts": {
1619
"build": "rollup -c",
1720
"ci:coverage": "nyc pnpm run test && nyc report --reporter=text-lcov > coverage.lcov",
@@ -30,6 +33,7 @@
3033
},
3134
"files": [
3235
"dist",
36+
"types",
3337
"README.md",
3438
"LICENSE"
3539
],
@@ -46,12 +50,20 @@
4650
},
4751
"devDependencies": {
4852
"@rollup/plugin-node-resolve": "^8.4.0",
53+
"@rollup/plugin-typescript": "^6.0.0",
4954
"rollup": "^2.23.0"
5055
},
56+
"types": "types/index.d.ts",
5157
"ava": {
5258
"babel": {
5359
"compileEnhancements": false
5460
},
61+
"extensions": [
62+
"ts"
63+
],
64+
"require": [
65+
"ts-node/register"
66+
],
5567
"files": [
5668
"!**/fixtures/**",
5769
"!**/helpers/**",

‎packages/virtual/rollup.config.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import resolve from '@rollup/plugin-node-resolve';
2+
import typescript from '@rollup/plugin-typescript';
23

34
import pkg from './package.json';
45

56
export default {
6-
input: 'src/index.js',
7-
plugins: [resolve()],
7+
input: 'src/index.ts',
8+
plugins: [resolve(), typescript()],
89
external: ['path'],
910
output: [
1011
{ format: 'cjs', file: pkg.main, exports: 'auto' },

‎packages/virtual/src/index.js

-36
This file was deleted.

‎packages/virtual/src/index.ts

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import * as path from 'path';
2+
3+
import { Plugin } from 'rollup';
4+
5+
import { RollupVirtualOptions } from '../';
6+
7+
const PREFIX = `\0virtual:`;
8+
9+
export default function virtual(modules: RollupVirtualOptions): Plugin {
10+
const resolvedIds = new Map<string, string>();
11+
12+
Object.keys(modules).forEach((id) => {
13+
resolvedIds.set(path.resolve(id), modules[id]);
14+
});
15+
16+
return {
17+
name: 'virtual',
18+
19+
resolveId(id, importer) {
20+
if (id in modules) return PREFIX + id;
21+
22+
if (importer) {
23+
const importerNoPrefix = importer.startsWith(PREFIX)
24+
? importer.slice(PREFIX.length)
25+
: importer;
26+
const resolved = path.resolve(path.dirname(importerNoPrefix), id);
27+
if (resolvedIds.has(resolved)) return PREFIX + resolved;
28+
}
29+
30+
return null;
31+
},
32+
33+
load(id) {
34+
if (id.startsWith(PREFIX)) {
35+
const idNoPrefix = id.slice(PREFIX.length);
36+
37+
return idNoPrefix in modules ? modules[idNoPrefix] : resolvedIds.get(idNoPrefix);
38+
}
39+
40+
return null;
41+
}
42+
};
43+
}

‎packages/virtual/tsconfig.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "../../tsconfig.base.json",
3+
"include": ["src/**/*", "types/**/*"]
4+
}

‎packages/virtual/types/index.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Plugin } from 'rollup';
2+
3+
export interface RollupVirtualOptions {
4+
[id: string]: string;
5+
}
6+
7+
/**
8+
* A Rollup plugin which loads virtual modules from memory.
9+
*/
10+
export default function virtual(modules: RollupVirtualOptions): Plugin;

‎pnpm-lock.yaml

+18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)