Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve wxt modules from the root #1417

Merged
merged 4 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/wxt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
"get-port-please": "^3.1.2",
"giget": "^1.2.3",
"hookable": "^5.5.3",
"import-meta-resolve": "^4.1.0",
"is-wsl": "^3.1.0",
"jiti": "^1.21.6",
"json5": "^2.2.3",
Expand Down
42 changes: 23 additions & 19 deletions packages/wxt/src/core/resolve-config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { loadConfig } from 'c12';
import { createRequire } from 'node:module';
import { resolve as esmResolve } from 'import-meta-resolve';
import {
InlineConfig,
ResolvedConfig,
Expand Down Expand Up @@ -29,6 +29,7 @@ import { getEslintVersion } from './utils/eslint';
import { safeStringToNumber } from './utils/number';
import { loadEnv } from './utils/env';
import { getPort } from 'get-port-please';
import { fileURLToPath, pathToFileURL } from 'node:url';

/**
* Given an inline config, discover the config file if necessary, merge the results, resolve any
Expand Down Expand Up @@ -157,10 +158,7 @@ export async function resolveConfig(
}

const userModules = await resolveWxtUserModules(
path.resolve(
root,
inlineConfig.configFile ? path.dirname(inlineConfig.configFile) : '.',
),
root,
modulesDir,
mergedConfig.modules,
);
Expand Down Expand Up @@ -426,17 +424,21 @@ async function getUnimportEslintOptions(
* Returns the path to `node_modules/wxt`.
*/
function resolveWxtModuleDir() {
// TODO: Use this once we're fully running in ESM, see https://github.com/wxt-dev/wxt/issues/277
// const url = import.meta.resolve('wxt', import.meta.url);
// resolve() returns the "wxt/dist/index.mjs" file, not the package's root
// directory, which we want to return from this function.
// return path.resolve(fileURLToPath(url), '../..');

const nodeRequire = globalThis.require ?? createRequire(import.meta.url);

// resolve() returns the "wxt/dist/index.mjs" file, not the package's root
// TODO: Drop the __filename expression once we're fully running in ESM
// (see https://github.com/wxt-dev/wxt/issues/277)
const importer =
typeof __filename === 'string'
? pathToFileURL(__filename).href
: import.meta.url;

// TODO: Switch to import.meta.resolve() once the parent argument is unflagged
// (e.g. --experimental-import-meta-resolve) and all Node.js versions we support
// have it.
const url = esmResolve('wxt', importer);

// esmResolve() returns the "wxt/dist/index.mjs" file, not the package's root
// directory, which we want to return from this function.
return path.resolve(nodeRequire.resolve('wxt'), '../..');
return path.resolve(fileURLToPath(url), '../..');
}

async function isDirMissing(dir: string) {
Expand Down Expand Up @@ -481,18 +483,20 @@ export async function mergeBuilderConfig(
}

export async function resolveWxtUserModules(
rootDir: string,
root: string,
modulesDir: string,
modules: string[] = [],
): Promise<WxtModuleWithMetadata<any>[]> {
const nodeRequire = createRequire(path.join(rootDir, 'index.js'));
const importer = pathToFileURL(path.join(root, 'index.js')).href;

// Resolve node_modules modules
const npmModules = await Promise.all<WxtModuleWithMetadata<any>>(
modules.map(async (moduleId) => {
const resolvedModuleId = nodeRequire.resolve(moduleId);
// Resolve before importing to allow for a local WXT clone to be
// symlinked into a project.
const resolvedModulePath = esmResolve(moduleId, importer);
const mod: { default: WxtModule<any> } = await import(
/* @vite-ignore */ resolvedModuleId
/* @vite-ignore */ resolvedModulePath
);
if (mod.default == null) {
throw Error('Module missing default export: ' + moduleId);
Expand Down
24 changes: 16 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.