Skip to content

Commit

Permalink
feat(virtual): Move to Typescript (#578)
Browse files Browse the repository at this point in the history
  • Loading branch information
NotWoods committed Sep 18, 2020
1 parent 6cd15b9 commit a6ff567
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 38 deletions.
12 changes: 12 additions & 0 deletions packages/virtual/package.json
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
@@ -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
@@ -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
@@ -0,0 +1,4 @@
{
"extends": "../../tsconfig.base.json",
"include": ["src/**/*", "types/**/*"]
}
10 changes: 10 additions & 0 deletions packages/virtual/types/index.d.ts
@@ -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.

4 comments on commit a6ff567

@Gaubee
Copy link

@Gaubee Gaubee commented on a6ff567 Dec 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need publish to npm.

@shellscape
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait. what's npm?

@Gaubee
Copy link

@Gaubee Gaubee commented on a6ff567 Dec 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait. what's npm?

the 'files' field in 'package.json' changed, so need change the version and publish to npm: 'npm publ'

@shellscape
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still confused. What's npm?

Please sign in to comment.