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): accept a function resolver for the id option #1722

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions packages/virtual/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,57 @@ import * as path from 'path';

import type { Plugin } from 'rollup';

import type { RollupVirtualOptions } from '../types';
import type { RollupVirtualOptions, VirtualIdResolver } from '../types';

const PREFIX = `\0virtual:`;
const IMPORTER_SEP = `::`;

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

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

function getImporterSuffix(id: string, importer: string | undefined) {
const module = modules[id] ?? resolvedIds.get(id);
if (typeof module === 'function') {
const importerSuffix = importer ? `${IMPORTER_SEP}${importer}` : '';
return importerSuffix;
}

return '';
}

return {
name: 'virtual',

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

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;
if (resolvedIds.has(resolved))
return PREFIX + resolved + getImporterSuffix(resolved, importer);
}

return null;
},

load(id) {
if (id.startsWith(PREFIX)) {
const idNoPrefix = id.slice(PREFIX.length);
const [idNoPrefix, importer] = id.slice(PREFIX.length).split(IMPORTER_SEP);

const module = idNoPrefix in modules ? modules[idNoPrefix] : resolvedIds.get(idNoPrefix);

if (typeof module === 'function') {
return module(importer);
}

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

return null;
Expand Down
16 changes: 16 additions & 0 deletions packages/virtual/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,19 @@ test('loads an absolute path from memory', (t) => {
t.is(resolved, `\0virtual:${path.resolve('src/foo.js')}`);
t.is(plugin.load(resolved), 'export default 42');
});

test('use function that takes a module ID', (t) => {
const plugin = virtual({
routeName: (importer) => {
const routeRoot = 'src/views/';
const routeName = importer.replace(routeRoot, '').replace(/\.\w+$/, '');

return `export const routeName = '${routeName}'`;
}
});

const resolved = plugin.resolveId('routeName', 'src/views/user/index.vue');

t.is(resolved, `\0virtual:routeName::src/views/user/index.vue`);
t.is(plugin.load(resolved), "export const routeName = 'user/index'");
});
3 changes: 2 additions & 1 deletion packages/virtual/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { Plugin } from 'rollup';

export type VirtualIdResolver = string | ((importer: string | undefined) => string);
export interface RollupVirtualOptions {
[id: string]: string;
[id: string]: VirtualIdResolver;
}

/**
Expand Down