From a6ff567c5ca3cdd21d92c1d0a5edb3fbf0506397 Mon Sep 17 00:00:00 2001 From: Tiger Oakes Date: Thu, 17 Sep 2020 18:52:31 -0700 Subject: [PATCH] feat(virtual): Move to Typescript (#578) --- packages/virtual/package.json | 12 +++++++++ packages/virtual/rollup.config.js | 5 ++-- packages/virtual/src/index.js | 36 -------------------------- packages/virtual/src/index.ts | 43 +++++++++++++++++++++++++++++++ packages/virtual/tsconfig.json | 4 +++ packages/virtual/types/index.d.ts | 10 +++++++ pnpm-lock.yaml | 18 +++++++++++++ 7 files changed, 90 insertions(+), 38 deletions(-) delete mode 100755 packages/virtual/src/index.js create mode 100644 packages/virtual/src/index.ts create mode 100644 packages/virtual/tsconfig.json create mode 100644 packages/virtual/types/index.d.ts diff --git a/packages/virtual/package.json b/packages/virtual/package.json index da8cabf62..e23794548 100755 --- a/packages/virtual/package.json +++ b/packages/virtual/package.json @@ -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", @@ -30,6 +33,7 @@ }, "files": [ "dist", + "types", "README.md", "LICENSE" ], @@ -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/**", diff --git a/packages/virtual/rollup.config.js b/packages/virtual/rollup.config.js index 2fcf30b21..e6d7e67b6 100755 --- a/packages/virtual/rollup.config.js +++ b/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' }, diff --git a/packages/virtual/src/index.js b/packages/virtual/src/index.js deleted file mode 100755 index bb25c8921..000000000 --- a/packages/virtual/src/index.js +++ /dev/null @@ -1,36 +0,0 @@ -/* eslint-disable consistent-return */ -import path from 'path'; - -const PREFIX = `\0virtual:`; - -export default function virtual(modules) { - const resolvedIds = new Map(); - - 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) { - // eslint-disable-next-line no-param-reassign - if (importer.startsWith(PREFIX)) importer = importer.slice(PREFIX.length); - const resolved = path.resolve(path.dirname(importer), id); - if (resolvedIds.has(resolved)) return PREFIX + resolved; - } - }, - - load(id) { - if (id.startsWith(PREFIX)) { - // eslint-disable-next-line no-param-reassign - id = id.slice(PREFIX.length); - - return id in modules ? modules[id] : resolvedIds.get(id); - } - } - }; -} diff --git a/packages/virtual/src/index.ts b/packages/virtual/src/index.ts new file mode 100644 index 000000000..f4b25c189 --- /dev/null +++ b/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(); + + 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; + } + }; +} diff --git a/packages/virtual/tsconfig.json b/packages/virtual/tsconfig.json new file mode 100644 index 000000000..0e6f93f42 --- /dev/null +++ b/packages/virtual/tsconfig.json @@ -0,0 +1,4 @@ +{ + "extends": "../../tsconfig.base.json", + "include": ["src/**/*", "types/**/*"] +} diff --git a/packages/virtual/types/index.d.ts b/packages/virtual/types/index.d.ts new file mode 100644 index 000000000..1853ce84e --- /dev/null +++ b/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; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a797f2f4e..d996e6600 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -439,9 +439,11 @@ importers: packages/virtual: devDependencies: '@rollup/plugin-node-resolve': 8.4.0_rollup@2.23.0 + '@rollup/plugin-typescript': 6.0.0_rollup@2.23.0 rollup: 2.23.0 specifiers: '@rollup/plugin-node-resolve': ^8.4.0 + '@rollup/plugin-typescript': ^6.0.0 rollup: ^2.23.0 packages/wasm: devDependencies: @@ -1620,6 +1622,20 @@ packages: typescript: '>=3.4.0' resolution: integrity: sha512-CkS028Itwjqm1uLbFVfpJgtVtnNvZ+og/m6UlNRR5wOOnNTWPcVQzOu5xGdEX+WWJxdvWIqUq2uR/RBt2ZipWg== + /@rollup/plugin-typescript/6.0.0_rollup@2.23.0: + dependencies: + '@rollup/pluginutils': 3.1.0_rollup@2.23.0 + resolve: 1.17.0 + rollup: 2.23.0 + dev: true + engines: + node: '>=8.0.0' + peerDependencies: + rollup: ^2.14.0 + tslib: '*' + typescript: '>=3.4.0' + resolution: + integrity: sha512-Y5U2L4eaF3wUSgCZRMdvNmuzWkKMyN3OwvhAdbzAi5sUqedaBk/XbzO4T7RlViDJ78MOPhwAIv2FtId/jhMtbg== /@rollup/plugin-virtual/2.0.3_rollup@2.23.0: dependencies: rollup: 2.23.0 @@ -3745,6 +3761,7 @@ packages: resolution: integrity: sha1-FQStJSMVjKpA20onh8sBQRmU6k8= /fsevents/2.1.3: + dev: true engines: node: ^8.16.0 || ^10.6.0 || >=11.0.0 optional: true @@ -6375,6 +6392,7 @@ packages: resolution: integrity: sha512-EEp9NhnUkwY8aif6bxgovPHMoMoNr2FulJziTndpt5H9RdwC47GSGuII9XxpSdzVGM0GWrNPHV6ie1LTNJPaLQ== /rollup/2.23.0: + dev: true engines: node: '>=10.0.0' hasBin: true