chore: Replace require.resolve with import.meta.resolve (#1221)

This commit is contained in:
Aaron
2024-11-28 00:00:01 -06:00
parent 0ad7115b04
commit 7f092e77a6
6 changed files with 29 additions and 17 deletions
+8
View File
@@ -6,6 +6,10 @@ import {
virtualModuleNames,
} from './src/core/utils/virtual-modules';
const replace = {
__vite_ssr_import_meta__: 'undefined',
};
export default defineBuildConfig([
// Non-virtual modules can be transpiled with mkdist
{
@@ -15,8 +19,12 @@ export default defineBuildConfig([
input: 'src',
pattern: ['**/*', '!**/__tests__', '!**/*.md', '!virtual', '!@types'],
declaration: true,
esbuild: {
define: replace,
},
},
],
replace,
hooks: {
async 'build:done'() {
// Replace any template variables in output files
+1
View File
@@ -63,6 +63,7 @@ describe('Zipping', () => {
"name": "test",
"description": "Example description",
"version": "1.0.0",
"type": "module",
"dependencies": {
"wxt": "../../../../..",
"flatten": "1.0.3"
+1
View File
@@ -39,6 +39,7 @@ export class TestProject {
name: 'E2E Extension',
description: 'Example description',
version: '0.0.0',
type: 'module',
dependencies: {
wxt: '../../..',
},
+12 -12
View File
@@ -28,6 +28,7 @@ import { getEslintVersion } from './utils/eslint';
import { safeStringToNumber } from './utils/number';
import { loadEnv } from './utils/env';
import { getPort } from 'get-port-please';
import { fileURLToPath } from 'node:url';
/**
* Given an inline config, discover the config file if necessary, merge the results, resolve any
@@ -403,20 +404,10 @@ async function getUnimportEslintOptions(
* Returns the path to `node_modules/wxt`.
*/
async 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);
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 requireResolve =
globalThis.require?.resolve ??
(await import('node:module')).default.createRequire(import.meta.url)
.resolve;
// 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(requireResolve('wxt'), '../..');
return path.resolve(fileURLToPath(url), '../..');
}
async function isDirMissing(dir: string) {
@@ -514,3 +505,12 @@ export async function resolveWxtUserModules(
);
return [...npmModules, ...localModules];
}
// Mock `import.meta.resolve` in tests
// @ts-expect-error
if (typeof __vite_ssr_import_meta__ !== 'undefined') {
// @ts-expect-error: Untyped global defined by Vitest
__vite_ssr_import_meta__.resolve = (path: string) =>
// @ts-expect-error: vitestCreateRequire defined in vitest.setup.ts
'file://' + vitestCreateRequire(import.meta.url).resolve(path);
}
+4 -5
View File
@@ -1,10 +1,9 @@
const ESLINT_PACKAGE_NAME = 'eslint';
export async function getEslintVersion(): Promise<string[]> {
try {
const require = (await import('node:module')).default.createRequire(
import.meta.url,
);
const { ESLint } = require('eslint');
return ESLint.version?.split('.') ?? [];
const { version } = await import(ESLINT_PACKAGE_NAME);
return version.split('.') ?? [];
} catch {
// Return an empty version when there's an error importing ESLint
return [];
+3
View File
@@ -1,5 +1,8 @@
import { fakeBrowser } from '@webext-core/fake-browser';
import { vi } from 'vitest';
import { createRequire } from 'node:module';
vi.stubGlobal('chrome', fakeBrowser);
vi.stubGlobal('browser', fakeBrowser);
vi.stubGlobal('vitestCreateRequire', createRequire);