Skip to content

Commit

Permalink
Use INTERNAL_PREFIX directly in the virtual file to avoid re-prefixing
Browse files Browse the repository at this point in the history
  • Loading branch information
NullVoxPopuli committed Mar 5, 2024
1 parent b2f9cf9 commit 0e2b46c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
25 changes: 20 additions & 5 deletions src/plugins/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ import { dirname } from 'node:path';

import { packageUp } from 'package-up';

/**
* I used to use \0 for the prefix, but when explicitly
* using the prefix in our own virtual modules (importing other virtual modules),
* we get an error:
* Module not found:
* TypeError [ERR_INVALID_ARG_VALUE]:
* The argument 'path' must be a string, Uint8Array, or URL without null bytes.
* Received '<consuming-project-path>/node_modules/\x00kolay/package.json'
*
* I had also tried using `virtual:` for a prefix, but webpack doesn't allow that
* Webpack supports "data:" and "file:" URIs by default
* You may need an additional plugin to handle "virtual:" URIs.
*/
export const INTERNAL_PREFIX = `[virtual:kolay]`;

const require = createRequire(import.meta.url);

/**
Expand Down Expand Up @@ -79,21 +94,21 @@ export function virtualFile(options) {
resolveId(id) {
if (allowed.has(id)) {
return {
id: `\0${id}`,
id: `${INTERNAL_PREFIX}${id}`,
};
}

return;
},
loadInclude(id) {
if (!id.startsWith('\0')) return false;
if (!id.startsWith(INTERNAL_PREFIX)) return false;

return allowed.has(id.slice(1));
return allowed.has(id.slice(INTERNAL_PREFIX.length));
},
load(id) {
if (!id.startsWith('\0')) return;
if (!id.startsWith(INTERNAL_PREFIX)) return;

let importPath = id.slice(1);
let importPath = id.slice(INTERNAL_PREFIX.length);

if (!allowed.has(importPath)) return;

Expand Down
8 changes: 4 additions & 4 deletions src/plugins/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { stripIndent } from 'common-tags';
import { createUnplugin } from 'unplugin';

import { virtualFile } from './helpers.js';
import { INTERNAL_PREFIX, virtualFile } from './helpers.js';

export const setup = createUnplugin(() => {
return {
Expand Down Expand Up @@ -39,8 +39,8 @@ export const setup = createUnplugin(() => {
// If you find yourself reading this comment,
// be sure to have both plugins setup in your plugins array.
let [apiDocs, manifest] = await Promise.all([
import('kolay/api-docs:virtual'),
import('kolay/manifest:virtual'),
import('${INTERNAL_PREFIX}kolay/api-docs:virtual'),
import('${INTERNAL_PREFIX}kolay/manifest:virtual'),
]);
await docs.setup({
Expand All @@ -56,7 +56,7 @@ export const setup = createUnplugin(() => {
{
importPath: 'kolay/test-support',
content: stripIndent`
import { setupKolay as setup } from 'kolay/setup';
import { setupKolay as setup } from '${INTERNAL_PREFIX}kolay/setup';
export function setupKolay(hooks, config) {
hooks.beforeEach(async function () {
Expand Down

0 comments on commit 0e2b46c

Please sign in to comment.