Skip to content

Commit

Permalink
chore: Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Aug 27, 2023
1 parent 67089d7 commit 57cdb4f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 46 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import './gtag';
import * as webpack from './webpack';

export { webpack };
export { isInjected, isReady } from './webpack';
export { isInjected, isReady, isFullReady } from './webpack';

export { config, Config } from './config';

Expand Down
2 changes: 1 addition & 1 deletion src/tools/updateModuleID.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ dirs.push(
async function start() {
const { browser, page } = await getPage();

await page.waitForFunction(() => window.WPP?.isReady, null, {
await page.waitForFunction(() => window.WPP?.isFullReady, null, {
timeout: 0,
});

Expand Down
1 change: 1 addition & 0 deletions src/webpack/eventTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
export interface WebpackEvents {
'webpack.injected': void;
'webpack.ready': void;
'webpack.full_ready': void;
}
78 changes: 34 additions & 44 deletions src/webpack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,22 @@ const debug = Debug('WA-JS:webpack');
export let isInjected = false;

/**
* Is setted true when the all webpack modules are fully loaded
* Is setted true when the main webpack modules are fully loaded
*/
export let isReady = false;

/**
* Is setted true when the all webpack modules are fully loaded
*/
export let isFullReady = false;

export function onInjected(listener: () => void, delay = 0): void {
internalEv.on('webpack.injected', () => {
setTimeout(listener, delay);
});
}

export function onReady(listener: () => void, delay = 1000): void {
export function onReady(listener: () => void, delay = 0): void {
internalEv.on('webpack.ready', () => {
setTimeout(listener, delay);
});
Expand Down Expand Up @@ -84,66 +89,51 @@ export function injectLoader(): void {
debug('injected');
await internalEv.emitAsync('webpack.injected').catch(() => null);

const availablesRuntimes = new Array(10000)
const allRuntimes = 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 filename.includes('main');
return !filename.includes('undefined');
});

const sortWeight: [RegExp, number][] = [
[/vendor.*main~/, 85],
[/vendor.*main/, 84],
[/vendor.*lazy.*high/, 75],
[/vendor.*lazy.*low/, 74],
[/vendor/, 83],
[/main~/, 82],
[/locale/, 81],
[/main/, 80],
[/lazy.*high.*~/, 73],
[/lazy.*high/, 72],
[/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 mainRuntimes = allRuntimes.filter((v) => {
const filename = webpackRequire.u(v);
if (filename.includes('locales')) {
return navigator.languages.some((lang) =>
filename.includes(`locales/${lang}`)
);
}

return 0;
};

const sorted = availablesRuntimes.sort(
(a, b) => sortValue(b) - sortValue(a)
);
return filename.includes('main');
});

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

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

await new Promise((resolve) => setTimeout(resolve, 5000));

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

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

const id = Date.now();
Expand Down

0 comments on commit 57cdb4f

Please sign in to comment.