diff --git a/src/plugins/helpers.js b/src/plugins/helpers.js index ef1d2ce..4b2c6fc 100644 --- a/src/plugins/helpers.js +++ b/src/plugins/helpers.js @@ -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 '/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); /** @@ -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; diff --git a/src/plugins/setup.js b/src/plugins/setup.js index 5a33ebb..4d16c84 100644 --- a/src/plugins/setup.js +++ b/src/plugins/setup.js @@ -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 { @@ -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({ @@ -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 () {