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
228 changes: 121 additions & 107 deletions packages/wxt/e2e/tests/__snapshots__/auto-imports.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,62 +1,6 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`Auto Imports > eslintrc > "enabled: 8" should output a JSON config file compatible with ESlint 8 1`] = `
".wxt/eslintrc-auto-import.json
----------------------------------------
{
"globals": {
"AutoMount": true,
"AutoMountOptions": true,
"Browser": true,
"ContentScriptAnchoredOptions": true,
"ContentScriptAppendMode": true,
"ContentScriptContext": true,
"ContentScriptInlinePositioningOptions": true,
"ContentScriptModalPositioningOptions": true,
"ContentScriptOverlayAlignment": true,
"ContentScriptOverlayPositioningOptions": true,
"ContentScriptPositioningOptions": true,
"ContentScriptUi": true,
"ContentScriptUiOptions": true,
"IframeContentScriptUi": true,
"IframeContentScriptUiOptions": true,
"InjectScriptOptions": true,
"IntegratedContentScriptUi": true,
"IntegratedContentScriptUiOptions": true,
"InvalidMatchPattern": true,
"MatchPattern": true,
"MigrationError": true,
"ScriptPublicPath": true,
"ShadowRootContentScriptUi": true,
"ShadowRootContentScriptUiOptions": true,
"StopAutoMount": true,
"StorageArea": true,
"StorageAreaChanges": true,
"StorageItemKey": true,
"WxtAppConfig": true,
"WxtStorage": true,
"WxtStorageItem": true,
"WxtWindowEventMap": true,
"browser": true,
"createIframeUi": true,
"createIntegratedUi": true,
"createShadowRootUi": true,
"defineAppConfig": true,
"defineBackground": true,
"defineContentScript": true,
"defineUnlistedScript": true,
"defineWxtPlugin": true,
"fakeBrowser": true,
"getAppConfig": true,
"injectScript": true,
"storage": true,
"useAppConfig": true
}
}
"
`;

exports[`Auto Imports > eslintrc > "enabled: 9" should output a flat config file compatible with ESlint 9 1`] = `
exports[`Auto Imports > eslintrc > "enabled: "flat" should output a flat config file compatible with ESlint >= 9 1`] = `
".wxt/eslint-auto-imports.mjs
----------------------------------------
const globals = {
Expand Down Expand Up @@ -119,7 +63,7 @@ export default {
"
`;

exports[`Auto Imports > eslintrc > "enabled: true" should output a JSON config file compatible with ESlint 8 1`] = `
exports[`Auto Imports > eslintrc > "enabled: "old" should output a JSON config file compatible with ESlint <= 8 1`] = `
".wxt/eslintrc-auto-import.json
----------------------------------------
{
Expand Down Expand Up @@ -175,58 +119,128 @@ exports[`Auto Imports > eslintrc > "enabled: true" should output a JSON config f
"
`;

exports[`Auto Imports > eslintrc > "enabled: true" should output a JSON config file compatible with ESlint <= 8 1`] = `
".wxt/eslintrc-auto-import.json
----------------------------------------
const globals = {
"AutoMount": true,
"AutoMountOptions": true,
"Browser": true,
"ContentScriptAnchoredOptions": true,
"ContentScriptAppendMode": true,
"ContentScriptContext": true,
"ContentScriptInlinePositioningOptions": true,
"ContentScriptModalPositioningOptions": true,
"ContentScriptOverlayAlignment": true,
"ContentScriptOverlayPositioningOptions": true,
"ContentScriptPositioningOptions": true,
"ContentScriptUi": true,
"ContentScriptUiOptions": true,
"IframeContentScriptUi": true,
"IframeContentScriptUiOptions": true,
"InjectScriptOptions": true,
"IntegratedContentScriptUi": true,
"IntegratedContentScriptUiOptions": true,
"InvalidMatchPattern": true,
"MatchPattern": true,
"MigrationError": true,
"ScriptPublicPath": true,
"ShadowRootContentScriptUi": true,
"ShadowRootContentScriptUiOptions": true,
"StopAutoMount": true,
"StorageArea": true,
"StorageAreaChanges": true,
"StorageItemKey": true,
"WxtAppConfig": true,
"WxtStorage": true,
"WxtStorageItem": true,
"WxtWindowEventMap": true,
"browser": true,
"createIframeUi": true,
"createIntegratedUi": true,
"createShadowRootUi": true,
"defineAppConfig": true,
"defineBackground": true,
"defineContentScript": true,
"defineUnlistedScript": true,
"defineWxtPlugin": true,
"fakeBrowser": true,
"getAppConfig": true,
"injectScript": true,
"storage": true,
"useAppConfig": true
}

export default {
name: "wxt/auto-imports",
languageOptions: {
globals,
/** @type {import('eslint').Linter.SourceType} */
sourceType: "module",
},
};
"
`;

exports[`Auto Imports > eslintrc > should allow customizing the output 1`] = `
"example.json
----------------------------------------
{
"globals": {
"AutoMount": "readonly",
"AutoMountOptions": "readonly",
"Browser": "readonly",
"ContentScriptAnchoredOptions": "readonly",
"ContentScriptAppendMode": "readonly",
"ContentScriptContext": "readonly",
"ContentScriptInlinePositioningOptions": "readonly",
"ContentScriptModalPositioningOptions": "readonly",
"ContentScriptOverlayAlignment": "readonly",
"ContentScriptOverlayPositioningOptions": "readonly",
"ContentScriptPositioningOptions": "readonly",
"ContentScriptUi": "readonly",
"ContentScriptUiOptions": "readonly",
"IframeContentScriptUi": "readonly",
"IframeContentScriptUiOptions": "readonly",
"InjectScriptOptions": "readonly",
"IntegratedContentScriptUi": "readonly",
"IntegratedContentScriptUiOptions": "readonly",
"InvalidMatchPattern": "readonly",
"MatchPattern": "readonly",
"MigrationError": "readonly",
"ScriptPublicPath": "readonly",
"ShadowRootContentScriptUi": "readonly",
"ShadowRootContentScriptUiOptions": "readonly",
"StopAutoMount": "readonly",
"StorageArea": "readonly",
"StorageAreaChanges": "readonly",
"StorageItemKey": "readonly",
"WxtAppConfig": "readonly",
"WxtStorage": "readonly",
"WxtStorageItem": "readonly",
"WxtWindowEventMap": "readonly",
"browser": "readonly",
"createIframeUi": "readonly",
"createIntegratedUi": "readonly",
"createShadowRootUi": "readonly",
"defineAppConfig": "readonly",
"defineBackground": "readonly",
"defineContentScript": "readonly",
"defineUnlistedScript": "readonly",
"defineWxtPlugin": "readonly",
"fakeBrowser": "readonly",
"getAppConfig": "readonly",
"injectScript": "readonly",
"storage": "readonly",
"useAppConfig": "readonly"
}
const globals = {
"AutoMount": "readonly",
"AutoMountOptions": "readonly",
"Browser": "readonly",
"ContentScriptAnchoredOptions": "readonly",
"ContentScriptAppendMode": "readonly",
"ContentScriptContext": "readonly",
"ContentScriptInlinePositioningOptions": "readonly",
"ContentScriptModalPositioningOptions": "readonly",
"ContentScriptOverlayAlignment": "readonly",
"ContentScriptOverlayPositioningOptions": "readonly",
"ContentScriptPositioningOptions": "readonly",
"ContentScriptUi": "readonly",
"ContentScriptUiOptions": "readonly",
"IframeContentScriptUi": "readonly",
"IframeContentScriptUiOptions": "readonly",
"InjectScriptOptions": "readonly",
"IntegratedContentScriptUi": "readonly",
"IntegratedContentScriptUiOptions": "readonly",
"InvalidMatchPattern": "readonly",
"MatchPattern": "readonly",
"MigrationError": "readonly",
"ScriptPublicPath": "readonly",
"ShadowRootContentScriptUi": "readonly",
"ShadowRootContentScriptUiOptions": "readonly",
"StopAutoMount": "readonly",
"StorageArea": "readonly",
"StorageAreaChanges": "readonly",
"StorageItemKey": "readonly",
"WxtAppConfig": "readonly",
"WxtStorage": "readonly",
"WxtStorageItem": "readonly",
"WxtWindowEventMap": "readonly",
"browser": "readonly",
"createIframeUi": "readonly",
"createIntegratedUi": "readonly",
"createShadowRootUi": "readonly",
"defineAppConfig": "readonly",
"defineBackground": "readonly",
"defineContentScript": "readonly",
"defineUnlistedScript": "readonly",
"defineWxtPlugin": "readonly",
"fakeBrowser": "readonly",
"getAppConfig": "readonly",
"injectScript": "readonly",
"storage": "readonly",
"useAppConfig": "readonly"
}

export default {
name: "wxt/auto-imports",
languageOptions: {
globals,
/** @type {import('eslint').Linter.SourceType} */
sourceType: "module",
},
};
"
`;
21 changes: 11 additions & 10 deletions packages/wxt/e2e/tests/auto-imports.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, expect, it } from 'vitest';
import { TestProject } from '../utils';
import { EslintSupportedVersions } from '../../src';

describe('Auto Imports', () => {
describe('imports: { ... }', () => {
Expand Down Expand Up @@ -217,7 +218,7 @@ describe('Auto Imports', () => {
});

describe('eslintrc', () => {
it('"enabled: true" should output a JSON config file compatible with ESlint 8', async () => {
it('"enabled: true" should output a JSON config file compatible with ESlint <= 8', async () => {
const project = new TestProject();
project.addFile('entrypoints/popup.html', `<html></html>`);

Expand All @@ -234,14 +235,14 @@ describe('Auto Imports', () => {
).toMatchSnapshot();
});

it('"enabled: 8" should output a JSON config file compatible with ESlint 8', async () => {
it('"enabled: "old" should output a JSON config file compatible with ESlint <= 8', async () => {
const project = new TestProject();
project.addFile('entrypoints/popup.html', `<html></html>`);

await project.prepare({
imports: {
eslintrc: {
enabled: 8,
enabled: 'old',
},
},
});
Expand All @@ -251,14 +252,14 @@ describe('Auto Imports', () => {
).toMatchSnapshot();
});

it('"enabled: 9" should output a flat config file compatible with ESlint 9', async () => {
it('"enabled: "flat" should output a flat config file compatible with ESlint >= 9', async () => {
const project = new TestProject();
project.addFile('entrypoints/popup.html', `<html></html>`);

await project.prepare({
imports: {
eslintrc: {
enabled: 9,
enabled: 'flat',
},
},
});
Expand Down Expand Up @@ -308,7 +309,7 @@ describe('Auto Imports', () => {
describe('Actual linting results', () => {
async function runEslint(
project: TestProject,
version: boolean | 'auto' | 8 | 9,
version: EslintSupportedVersions,
) {
project.addFile(
'entrypoints/background.js',
Expand Down Expand Up @@ -340,7 +341,7 @@ describe('Auto Imports', () => {
`,
);

await expect(runEslint(project, 9)).rejects.toMatchObject({
await expect(runEslint(project, 'flat')).rejects.toMatchObject({
exitCode: 1,
stdout: expect.stringContaining(
"'defineBackground' is not defined",
Expand All @@ -367,7 +368,7 @@ describe('Auto Imports', () => {
];
`,
);
const res = await runEslint(project, 9);
const res = await runEslint(project, 'flat');

expect(res).toBeDefined();
});
Expand All @@ -389,7 +390,7 @@ describe('Auto Imports', () => {
}),
);

await expect(runEslint(project, 8)).rejects.toMatchObject({
await expect(runEslint(project, 'old')).rejects.toMatchObject({
exitCode: 1,
stdout: expect.stringContaining(
"'defineBackground' is not defined",
Expand All @@ -414,7 +415,7 @@ describe('Auto Imports', () => {
],
}),
);
const res = await runEslint(project, 8);
const res = await runEslint(project, 'old');

expect(res).toBeDefined();
});
Expand Down
13 changes: 7 additions & 6 deletions packages/wxt/src/builtin-modules/unimport.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { addViteConfig, defineWxtModule } from '../modules';
import type {
import {
EslintGlobalsPropValue,
EslintSupportedVersions,
Wxt,
WxtDirFileEntry,
WxtModule,
WxtResolvedUnimportOptions,
} from '../types';
import { type Unimport, createUnimport, toExports } from 'unimport';
import { createUnimport, toExports, type Unimport } from 'unimport';
import UnimportPlugin from 'unimport/unplugin';

export default defineWxtModule({
Expand Down Expand Up @@ -103,7 +104,7 @@ async function getImportsModuleEntry(

async function getEslintConfigEntry(
unimport: Unimport,
version: 8 | 9,
version: EslintSupportedVersions,
options: WxtResolvedUnimportOptions,
): Promise<WxtDirFileEntry> {
const globals = (await unimport.getImports())
Expand All @@ -115,8 +116,8 @@ async function getEslintConfigEntry(
return globals;
}, {});

if (version <= 8) return getEslint8ConfigEntry(options, globals);
else return getEslint9ConfigEntry(options, globals);
if (version == 'old') return getEslint8ConfigEntry(options, globals);
else return getEslint9PlusConfigEntry(options, globals);
}

export function getEslint8ConfigEntry(
Expand All @@ -129,7 +130,7 @@ export function getEslint8ConfigEntry(
};
}

export function getEslint9ConfigEntry(
export function getEslint9PlusConfigEntry(
options: WxtResolvedUnimportOptions,
globals: Record<string, EslintGlobalsPropValue>,
): WxtDirFileEntry {
Expand Down
Loading
Loading