fix: Root path customization now works
This commit is contained in:
+1
-1
@@ -34,7 +34,7 @@
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"dev": "tsx src/cli.ts",
|
||||
"exvite": "tsx src/cli/index.ts",
|
||||
"build": "run-s -s build:*",
|
||||
"build:js": "tsup src/index.ts --sourcemap --dts --format esm,cjs",
|
||||
"build:cli": "tsup src/cli/index.ts -d dist/cli --sourcemap",
|
||||
|
||||
@@ -158,7 +158,7 @@ async function getBackgroundEntrypoint(
|
||||
path: string,
|
||||
): Promise<BackgroundEntrypoint> {
|
||||
const { main: _, ...options } =
|
||||
await importTsFile<BackgroundScriptDefintition>(path);
|
||||
await importTsFile<BackgroundScriptDefintition>(config.root, path);
|
||||
if (options == null) {
|
||||
throw Error('Background script does not have a default export');
|
||||
}
|
||||
@@ -180,6 +180,7 @@ async function getContentScriptEntrypoint(
|
||||
path: string,
|
||||
): Promise<ContentScriptEntrypoint> {
|
||||
const { main: _, ...options } = await importTsFile<ContentScriptDefinition>(
|
||||
config.root,
|
||||
path,
|
||||
);
|
||||
if (options == null) {
|
||||
@@ -238,5 +239,5 @@ const PATH_GLOB_TO_TYPE_MAP: Record<string, Entrypoint['type'] | 'ignored'> = {
|
||||
|
||||
export type FindEntrypointsConfig = Pick<
|
||||
InternalConfig,
|
||||
'entrypointsDir' | 'outDir' | 'logger' | 'mode' | 'command'
|
||||
'root' | 'entrypointsDir' | 'outDir' | 'logger' | 'mode' | 'command'
|
||||
>;
|
||||
|
||||
@@ -43,11 +43,12 @@ export async function getInternalConfig(
|
||||
|
||||
// Load user config from file
|
||||
let userConfig: UserConfig = {
|
||||
mode: config.mode,
|
||||
mode,
|
||||
};
|
||||
if (config.configFile !== false) {
|
||||
userConfig = await importTsFile<UserConfig>(
|
||||
path.resolve(config.configFile ?? 'exvite.config.ts'),
|
||||
root,
|
||||
path.resolve(root, config.configFile ?? 'exvite.config.ts'),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -92,8 +93,12 @@ export async function getInternalConfig(
|
||||
finalConfig.vite.plugins.push(plugins.download(finalConfig));
|
||||
finalConfig.vite.plugins.push(plugins.devHtmlPrerender(finalConfig));
|
||||
finalConfig.vite.plugins.push(plugins.unimport(finalConfig));
|
||||
finalConfig.vite.plugins.push(plugins.virtualEntrypoin('background'));
|
||||
finalConfig.vite.plugins.push(plugins.virtualEntrypoin('content-script'));
|
||||
finalConfig.vite.plugins.push(
|
||||
plugins.virtualEntrypoin('background', finalConfig),
|
||||
);
|
||||
finalConfig.vite.plugins.push(
|
||||
plugins.virtualEntrypoin('content-script', finalConfig),
|
||||
);
|
||||
|
||||
finalConfig.vite.define ??= {};
|
||||
getGlobals(finalConfig).forEach((global) => {
|
||||
|
||||
@@ -4,9 +4,9 @@ import transform from 'jiti/dist/babel';
|
||||
import { resolve } from 'path';
|
||||
import { scanExports } from 'unimport';
|
||||
|
||||
export async function importTsFile<T>(path: string): Promise<T> {
|
||||
export async function importTsFile<T>(root: string, path: string): Promise<T> {
|
||||
const clientImports = await scanExports(
|
||||
resolve('node_modules/exvite/dist/client/index.js'),
|
||||
resolve(root, 'node_modules/exvite/dist/client/index.js'),
|
||||
);
|
||||
const jiti = createJITI(__filename, {
|
||||
cache: false,
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
import p from 'node:path';
|
||||
import fs from 'fs-extra';
|
||||
|
||||
/**
|
||||
* Given a root and path, resolve absolute path to the file.
|
||||
*
|
||||
* - If the path is absolute, return the path.
|
||||
* - If the file exists at `path.resolve(root, ...path)`, return that path
|
||||
* - If the file exists at `path.resolve(...path)`, return that path
|
||||
* - If the file doesn't exist, return `undefined`.
|
||||
*
|
||||
* Any files in the root directory with the specified path take precidence over files in the CWD
|
||||
* with the same path.
|
||||
*/
|
||||
export async function resolveFile(
|
||||
root: string,
|
||||
path: string,
|
||||
): Promise<string | undefined> {
|
||||
const rootPath = p.resolve(root, path);
|
||||
if (await fs.exists(rootPath)) return rootPath;
|
||||
|
||||
const cwdPath = p.resolve(process.cwd(), path);
|
||||
if (await fs.exists(cwdPath)) return cwdPath;
|
||||
}
|
||||
@@ -13,7 +13,7 @@ export function unimport(config: InternalConfig): Plugin {
|
||||
return {
|
||||
name: 'exvite:unimport',
|
||||
async config() {
|
||||
await unimport.scanImportsFromDir();
|
||||
await unimport.scanImportsFromDir(undefined, { cwd: config.srcDir });
|
||||
},
|
||||
async transform(code, id) {
|
||||
return unimport.injectImports(code, id);
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
import { Plugin } from 'vite';
|
||||
import { Entrypoint } from '../types';
|
||||
import { Entrypoint, InternalConfig } from '../types';
|
||||
import fs from 'fs-extra';
|
||||
import { resolve } from 'path';
|
||||
|
||||
/**
|
||||
* Wraps a user's entrypoint with a vitual version with additional logic.
|
||||
*/
|
||||
export function virtualEntrypoin(type: Entrypoint['type']): Plugin {
|
||||
export function virtualEntrypoin(
|
||||
type: Entrypoint['type'],
|
||||
config: InternalConfig,
|
||||
): Plugin {
|
||||
const virtualId = `virtual:exvite-${type}?`;
|
||||
const resolvedVirtualId = `\0${virtualId}`;
|
||||
|
||||
@@ -26,7 +29,10 @@ export function virtualEntrypoin(type: Entrypoint['type']): Plugin {
|
||||
|
||||
const inputPath = id.replace(resolvedVirtualId, '');
|
||||
const template = await fs.readFile(
|
||||
resolve(`node_modules/exvite/templates/virtual-${type}.ts`),
|
||||
resolve(
|
||||
config.root,
|
||||
`node_modules/exvite/templates/virtual-${type}.ts`,
|
||||
),
|
||||
'utf-8',
|
||||
);
|
||||
return template.replaceAll('{{moduleId}}', inputPath);
|
||||
|
||||
Reference in New Issue
Block a user