Skip to content

Commit

Permalink
fix: import.meta.glob problem on vite >= 4.4.8
Browse files Browse the repository at this point in the history
vitejs/vite#13973
- above fix makes import.meta.glob to ignore 'modulePreload: false' option. so, css are preloaded by wrapper function and occurs error because wrapper function does not use chrome.runtime.getURL for css files.

- so, prevented dynamic import from being wrapped by vite preloader.
  • Loading branch information
fractalo committed Jan 1, 2024
1 parent 430c78d commit dc30665
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions src/injected/main/index.ts
@@ -1,24 +1,47 @@
import { ScriptIds } from 'src/constants/scripts';
import { SCRIPT_IDS } from 'src/messaging';
import messaging from './messaging';
import './routes';
import './stores';
import { initI18next } from "src/i18n";
import { createLanguageDetector } from "src/i18n/languageDetectors/twitch";
import { waitForDOMReady } from 'src/util/waitForDOMReady';


(async() => {
await messaging.waitForConnected(SCRIPT_IDS.CONTENT);

messaging.postMessage({ type: "GET_LANGUAGE" });
const browserUILang = await messaging.waitForMessage<string>(ScriptIds.CONTENT, "LANGUAGE");
const browserUILang = await messaging.waitForMessage<string>(SCRIPT_IDS.CONTENT, "LANGUAGE");

/** initialize i18next */
const languageDetector = createLanguageDetector(browserUILang);
initI18next(languageDetector);
initI18next({ languageDetector });

/** load modules */
const modules = import.meta.glob('./modules/*/index.ts');

await waitForDOMReady();

Object.keys(modules).forEach(path => modules[path]());
if (import.meta.env.DEV) {
const modules = import.meta.glob('./modules/*/index.ts');
Object.values(modules).forEach(module => module());
} else {
// to prevent dynamic import from being wrapped by vite preloader
const modulePaths = import.meta.glob('./modules/*/index.ts', {
query: { script: true, module: true },
import: 'default',
eager: true
});

Object.values(modulePaths)
.forEach(path => {
try {
if (typeof path !== 'string') {
throw new Error('module path is not string');
}
import(/* @vite-ignore */ path);
} catch (err) {
console.error(err);
}
});
}

})();

0 comments on commit dc30665

Please sign in to comment.