Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(virtual): Move to Typescript #578

Merged
merged 1 commit into from
Sep 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/virtual/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
"bugs": "https://github.com/rollup/rollup-plugin-virtual/issues",
"main": "dist/index.js",
"module": "dist/index.es.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 @@ -30,6 +33,7 @@
},
"files": [
"dist",
"types",
"README.md",
"LICENSE"
],
Expand All @@ -46,12 +50,20 @@
},
"devDependencies": {
"@rollup/plugin-node-resolve": "^8.4.0",
"@rollup/plugin-typescript": "^6.0.0",
"rollup": "^2.23.0"
},
"types": "types/index.d.ts",
"ava": {
"babel": {
"compileEnhancements": false
},
"extensions": [
"ts"
],
"require": [
"ts-node/register"
],
"files": [
"!**/fixtures/**",
"!**/helpers/**",
Expand Down
5 changes: 3 additions & 2 deletions packages/virtual/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import resolve from '@rollup/plugin-node-resolve';
import typescript from '@rollup/plugin-typescript';

import pkg from './package.json';

export default {
input: 'src/index.js',
plugins: [resolve()],
input: 'src/index.ts',
plugins: [resolve(), typescript()],
external: ['path'],
output: [
{ format: 'cjs', file: pkg.main, exports: 'auto' },
Expand Down
36 changes: 0 additions & 36 deletions packages/virtual/src/index.js

This file was deleted.

43 changes: 43 additions & 0 deletions packages/virtual/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import * as path from 'path';

import { Plugin } from 'rollup';

import { RollupVirtualOptions } from '../';

const PREFIX = `\0virtual:`;

export default function virtual(modules: RollupVirtualOptions): Plugin {
const resolvedIds = new Map<string, string>();

Object.keys(modules).forEach((id) => {
resolvedIds.set(path.resolve(id), modules[id]);
});

return {
name: 'virtual',

resolveId(id, importer) {
if (id in modules) return PREFIX + id;

if (importer) {
const importerNoPrefix = importer.startsWith(PREFIX)
? importer.slice(PREFIX.length)
: importer;
const resolved = path.resolve(path.dirname(importerNoPrefix), id);
if (resolvedIds.has(resolved)) return PREFIX + resolved;
}

return null;
},

load(id) {
if (id.startsWith(PREFIX)) {
const idNoPrefix = id.slice(PREFIX.length);

return idNoPrefix in modules ? modules[idNoPrefix] : resolvedIds.get(idNoPrefix);
}

return null;
}
};
}
4 changes: 4 additions & 0 deletions packages/virtual/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../tsconfig.base.json",
"include": ["src/**/*", "types/**/*"]
}
10 changes: 10 additions & 0 deletions packages/virtual/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Plugin } from 'rollup';

export interface RollupVirtualOptions {
[id: string]: string;
}

/**
* A Rollup plugin which loads virtual modules from memory.
*/
export default function virtual(modules: RollupVirtualOptions): Plugin;
18 changes: 18 additions & 0 deletions pnpm-lock.yaml

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