Skip to content

Commit

Permalink
fix: Small improvement for webpack inject (#1061)
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed May 23, 2023
1 parent 8877521 commit 84e7309
Showing 1 changed file with 70 additions and 64 deletions.
134 changes: 70 additions & 64 deletions src/webpack/index.ts
Expand Up @@ -78,76 +78,82 @@ export function injectLoader(): void {
const self = window as any;
const chunk = (self[chunkName] = self[chunkName] || []);

const id = Date.now();
chunk.push([
[id],
{},
async (__webpack_require__: any) => {
webpackRequire = __webpack_require__;

isInjected = true;
debug('injected');
await internalEv.emitAsync('webpack.injected').catch(() => null);

const availablesRuntimes = new Array(10000)
.fill(1)
.map((v, k) => v + k)
.filter((v) => {
const filename = webpackRequire.u(v);
if (filename.includes('undefined')) {
return false;
}
if (filename.includes('locales')) {
return navigator.languages.some((lang) =>
filename.includes(`locales/${lang}`)
);
}
return true;
});

const sortWeight: [RegExp, number][] = [
[/locale/, 99],
[/vendor.*main~/, 84],
[/vendor.*main/, 83],
[/vendor/, 82],
[/main~/, 81],
[/main/, 80],
[/vendor.*lazy.*high/, 75],
[/lazy.*high.*~/, 74],
[/lazy.*high/, 73],
[/lazy.*low.*~/, 71],
[/lazy.*low/, 70],
[/lazy/, 1],
];

const sortValue = (id: string) => {
const filename = webpackRequire.u(id);

for (const w of sortWeight) {
if (w[0].test(filename)) {
return w[1];
}
const injectFunction = async (__webpack_require__: any) => {
webpackRequire = __webpack_require__;

isInjected = true;
debug('injected');
await internalEv.emitAsync('webpack.injected').catch(() => null);

const availablesRuntimes = new Array(10000)
.fill(1)
.map((v, k) => v + k)
.filter((v) => {
const filename = webpackRequire.u(v);
if (filename.includes('undefined')) {
return false;
}
if (filename.includes('locales')) {
return navigator.languages.some((lang) =>
filename.includes(`locales/${lang}`)
);
}
return true;
});

const sortWeight: [RegExp, number][] = [
[/locale/, 99],
[/vendor.*main~/, 84],
[/vendor.*main/, 83],
[/vendor/, 82],
[/main~/, 81],
[/main/, 80],
[/vendor.*lazy.*high/, 75],
[/lazy.*high.*~/, 74],
[/lazy.*high/, 73],
[/lazy.*low.*~/, 71],
[/lazy.*low/, 70],
[/lazy/, 1],
];

const sortValue = (id: string) => {
const filename = webpackRequire.u(id);

for (const w of sortWeight) {
if (w[0].test(filename)) {
return w[1];
}
}

return 0;
};
return 0;
};

const sorted = availablesRuntimes.sort(
(a, b) => sortValue(b) - sortValue(a)
);
const sorted = availablesRuntimes.sort(
(a, b) => sortValue(b) - sortValue(a)
);

// Use sequential file load
for (const v of sorted) {
try {
await webpackRequire.e(v);
} catch (error) {
debug('load file error', webpackRequire.e(v));
}
// Use sequential file load
for (const v of sorted) {
try {
await webpackRequire.e(v);
} catch (error) {
debug('load file error', webpackRequire.e(v));
}
}

isReady = true;
debug('ready to use');
await internalEv.emitAsync('webpack.ready').catch(() => null);
};

const id = Date.now();
chunk.push([
[id],
{},
(__webpack_require__: any) => {
webpackRequire = __webpack_require__;

isReady = true;
debug('ready to use');
await internalEv.emitAsync('webpack.ready').catch(() => null);
queueMicrotask(() => injectFunction(__webpack_require__));
},
]);
}
Expand Down

0 comments on commit 84e7309

Please sign in to comment.