-
-
Notifications
You must be signed in to change notification settings - Fork 591
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(virtual): Move to Typescript (#578)
- Loading branch information
Showing
7 changed files
with
90 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"include": ["src/**/*", "types/**/*"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
a6ff567
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need publish to npm.
a6ff567
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wait. what's npm?
a6ff567
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the 'files' field in 'package.json' changed, so need change the version and publish to npm: 'npm publ'
a6ff567
There was a problem hiding this comment.
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?