|
1 | 1 | import { loadConfig } from 'c12';
|
| 2 | +import { resolve as esmResolve } from 'import-meta-resolve'; |
2 | 3 | import {
|
3 | 4 | InlineConfig,
|
4 | 5 | ResolvedConfig,
|
@@ -28,6 +29,7 @@ import { getEslintVersion } from './utils/eslint';
|
28 | 29 | import { safeStringToNumber } from './utils/number';
|
29 | 30 | import { loadEnv } from './utils/env';
|
30 | 31 | import { getPort } from 'get-port-please';
|
| 32 | +import { fileURLToPath, pathToFileURL } from 'node:url'; |
31 | 33 |
|
32 | 34 | /**
|
33 | 35 | * Given an inline config, discover the config file if necessary, merge the results, resolve any
|
@@ -84,7 +86,7 @@ export async function resolveConfig(
|
84 | 86 | inlineConfig.root ?? userConfig.root ?? process.cwd(),
|
85 | 87 | );
|
86 | 88 | const wxtDir = path.resolve(root, '.wxt');
|
87 |
| - const wxtModuleDir = await resolveWxtModuleDir(); |
| 89 | + const wxtModuleDir = resolveWxtModuleDir(); |
88 | 90 | const srcDir = path.resolve(root, mergedConfig.srcDir ?? root);
|
89 | 91 | const entrypointsDir = path.resolve(
|
90 | 92 | srcDir,
|
@@ -156,6 +158,7 @@ export async function resolveConfig(
|
156 | 158 | }
|
157 | 159 |
|
158 | 160 | const userModules = await resolveWxtUserModules(
|
| 161 | + root, |
159 | 162 | modulesDir,
|
160 | 163 | mergedConfig.modules,
|
161 | 164 | );
|
@@ -420,21 +423,22 @@ async function getUnimportEslintOptions(
|
420 | 423 | /**
|
421 | 424 | * Returns the path to `node_modules/wxt`.
|
422 | 425 | */
|
423 |
| -async function resolveWxtModuleDir() { |
424 |
| - // TODO: Use this once we're fully running in ESM, see https://github.com/wxt-dev/wxt/issues/277 |
425 |
| - // const url = import.meta.resolve('wxt', import.meta.url); |
426 |
| - // resolve() returns the "wxt/dist/index.mjs" file, not the package's root |
427 |
| - // directory, which we want to return from this function. |
428 |
| - // return path.resolve(fileURLToPath(url), '../..'); |
429 |
| - |
430 |
| - const requireResolve = |
431 |
| - globalThis.require?.resolve ?? |
432 |
| - (await import('node:module')).default.createRequire(import.meta.url) |
433 |
| - .resolve; |
434 |
| - |
435 |
| - // resolve() returns the "wxt/dist/index.mjs" file, not the package's root |
| 426 | +function resolveWxtModuleDir() { |
| 427 | + // TODO: Drop the __filename expression once we're fully running in ESM |
| 428 | + // (see https://github.com/wxt-dev/wxt/issues/277) |
| 429 | + const importer = |
| 430 | + typeof __filename === 'string' |
| 431 | + ? pathToFileURL(__filename).href |
| 432 | + : import.meta.url; |
| 433 | + |
| 434 | + // TODO: Switch to import.meta.resolve() once the parent argument is unflagged |
| 435 | + // (e.g. --experimental-import-meta-resolve) and all Node.js versions we support |
| 436 | + // have it. |
| 437 | + const url = esmResolve('wxt', importer); |
| 438 | + |
| 439 | + // esmResolve() returns the "wxt/dist/index.mjs" file, not the package's root |
436 | 440 | // directory, which we want to return from this function.
|
437 |
| - return path.resolve(requireResolve('wxt'), '../..'); |
| 441 | + return path.resolve(fileURLToPath(url), '../..'); |
438 | 442 | }
|
439 | 443 |
|
440 | 444 | async function isDirMissing(dir: string) {
|
@@ -479,14 +483,20 @@ export async function mergeBuilderConfig(
|
479 | 483 | }
|
480 | 484 |
|
481 | 485 | export async function resolveWxtUserModules(
|
| 486 | + root: string, |
482 | 487 | modulesDir: string,
|
483 | 488 | modules: string[] = [],
|
484 | 489 | ): Promise<WxtModuleWithMetadata<any>[]> {
|
| 490 | + const importer = pathToFileURL(path.join(root, 'index.js')).href; |
| 491 | + |
485 | 492 | // Resolve node_modules modules
|
486 | 493 | const npmModules = await Promise.all<WxtModuleWithMetadata<any>>(
|
487 | 494 | modules.map(async (moduleId) => {
|
| 495 | + // Resolve before importing to allow for a local WXT clone to be |
| 496 | + // symlinked into a project. |
| 497 | + const resolvedModulePath = esmResolve(moduleId, importer); |
488 | 498 | const mod: { default: WxtModule<any> } = await import(
|
489 |
| - /* @vite-ignore */ moduleId |
| 499 | + /* @vite-ignore */ resolvedModulePath |
490 | 500 | );
|
491 | 501 | if (mod.default == null) {
|
492 | 502 | throw Error('Module missing default export: ' + moduleId);
|
|
0 commit comments