fde488ac82
BREAKING CHANGE: `defineContentScript` and `defineBackground` are now exported from `wxt/sandbox` instead of `wxt/client`. If you use auto-imports, no changes are required. If you have disabled auto-imports, you'll need to manually update your import statements.
43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
import { Plugin } from 'vite';
|
|
import { InternalConfig, VirtualEntrypointType } from '~/types';
|
|
import fs from 'fs-extra';
|
|
import { resolve } from 'path';
|
|
import { normalizePath } from '~/core/utils/paths';
|
|
|
|
/**
|
|
* Wraps a user's entrypoint with a vitual version with additional logic.
|
|
*/
|
|
export function virtualEntrypoint(
|
|
type: VirtualEntrypointType,
|
|
config: Omit<InternalConfig, 'builder'>,
|
|
): Plugin {
|
|
const virtualId = `virtual:wxt-${type}?`;
|
|
const resolvedVirtualId = `\0${virtualId}`;
|
|
|
|
return {
|
|
name: `wxt:virtual-entrypoint`,
|
|
resolveId(id) {
|
|
// Id doesn't start with prefix, it looks like this:
|
|
// /path/to/project/virtual:background?/path/to/project/entrypoints/background.ts
|
|
const index = id.indexOf(virtualId);
|
|
if (index === -1) return;
|
|
|
|
const inputPath = normalizePath(id.substring(index + virtualId.length));
|
|
return resolvedVirtualId + inputPath;
|
|
},
|
|
async load(id) {
|
|
if (!id.startsWith(resolvedVirtualId)) return;
|
|
|
|
const inputPath = id.replace(resolvedVirtualId, '');
|
|
const template = await fs.readFile(
|
|
resolve(
|
|
config.root,
|
|
`node_modules/wxt/dist/virtual/${type}-entrypoint.js`,
|
|
),
|
|
'utf-8',
|
|
);
|
|
return template.replace(`virtual:user-${type}`, inputPath);
|
|
},
|
|
};
|
|
}
|