Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion packages/wxt-demo/eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import autoImports from './.wxt/eslintrc-auto-import.js';
import autoImports from './.wxt/eslintrc-auto-import.json';

export default [
{
Expand Down
7 changes: 7 additions & 0 deletions packages/wxt-demo/wxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { defineConfig } from 'wxt';
import { presetUno } from 'unocss';
import path from 'node:path';

export default defineConfig({
srcDir: 'src',
Expand Down Expand Up @@ -56,4 +57,10 @@ export default defineConfig({
presets: [presetUno()],
},
},
imports: {
eslintrc: {
enabled: true,
filePath: path.resolve('.wxt/eslintrc-auto-import.json'),
},
},
Comment on lines +60 to +65
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of changing the demo project, please write tests to test different config. ESLint tests are here:

describe('eslintrc', () => {

});
45 changes: 34 additions & 11 deletions packages/wxt/src/builtin-modules/unimport.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from 'node:path';
import { addViteConfig, defineWxtModule } from '../modules';
import type {
EslintGlobalsPropValue,
Expand Down Expand Up @@ -49,16 +50,16 @@ export default defineWxtModule({
// Only create global types when user has enabled auto-imports
entries.push(await getImportsDeclarationEntry(unimport));

// Only generate ESLint config if that feature is enabled
if (wxt.config.imports.eslintrc.enabled === false) return;

// Only generate ESLint config if that feature is enabled
entries.push(
await getEslintConfigEntry(
unimport,
wxt.config.imports.eslintrc.enabled,
wxt.config.imports,
),
const eslintConfigEntries = await getEslintConfigEntry(
unimport,
wxt.config.imports.eslintrc.enabled,
wxt.config.imports,
);

entries.push(...eslintConfigEntries);
});

// Add vite plugin
Expand Down Expand Up @@ -105,7 +106,7 @@ async function getEslintConfigEntry(
unimport: Unimport,
version: 8 | 9,
options: WxtResolvedUnimportOptions,
): Promise<WxtDirFileEntry> {
): Promise<WxtDirFileEntry[]> {
const globals = (await unimport.getImports())
.map((i) => i.as ?? i.name)
.filter(Boolean)
Expand All @@ -115,7 +116,7 @@ async function getEslintConfigEntry(
return globals;
}, {});

if (version <= 8) return getEslint8ConfigEntry(options, globals);
if (version <= 8) return [getEslint8ConfigEntry(options, globals)];
else return getEslint9ConfigEntry(options, globals);
}

Expand All @@ -132,8 +133,8 @@ export function getEslint8ConfigEntry(
export function getEslint9ConfigEntry(
options: WxtResolvedUnimportOptions,
globals: Record<string, EslintGlobalsPropValue>,
): WxtDirFileEntry {
return {
): WxtDirFileEntry[] {
const javaScriptFileEntry: WxtDirFileEntry = {
path: options.eslintrc.filePath,
text: `const globals = ${JSON.stringify(globals, null, 2)}

Expand All @@ -147,4 +148,26 @@ export default {
};
`,
};

const javaScriptFileDirname = path.dirname(options.eslintrc.filePath);
const javaScriptFileExtension = path.extname(options.eslintrc.filePath);
const javaScriptFileBasename = path.basename(
options.eslintrc.filePath,
javaScriptFileExtension,
);

const typeScriptFilePath = path.join(
javaScriptFileDirname,
`${javaScriptFileBasename}.d.ts`,
);
Comment on lines +152 to +162
Copy link
Member

@aklinker1 aklinker1 Dec 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be simplified a little bit

Suggested change
const javaScriptFileDirname = path.dirname(options.eslintrc.filePath);
const javaScriptFileExtension = path.extname(options.eslintrc.filePath);
const javaScriptFileBasename = path.basename(
options.eslintrc.filePath,
javaScriptFileExtension,
);
const typeScriptFilePath = path.join(
javaScriptFileDirname,
`${javaScriptFileBasename}.d.ts`,
);
const typeScriptFilePath = options.eslintrc.filePath.slice(0, -extname(options.eslintrc.filePath)) + ".d.ts"

The second slice argument might be off by one, I can never remember if extname returns the extension with or without the dot. Or just remove the first dot from ".d.ts".


const typeScriptFileEntry: WxtDirFileEntry = {
path: typeScriptFilePath,
text: `import type { ConfigObject } from "@eslint/core";
declare const config: ConfigObject;
export default config;
`,
};

return [javaScriptFileEntry, typeScriptFileEntry];
}
Loading