Skip to content

Commit

Permalink
fix(builder): assetsRetry cause global variable pollution (#3989)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan committed Jun 16, 2023
1 parent e563ade commit 5a3eeff
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 10 deletions.
8 changes: 8 additions & 0 deletions .changeset/tame-cougars-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@modern-js/builder-shared': patch
---

fix(builder): assetsRetry cause global variable pollution

fix(builder): 修复 assetsRetry 污染全局变量的问题

2 changes: 1 addition & 1 deletion packages/builder/builder-shared/compiled/assetsRetry.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/builder/builder-shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
},
"devDependencies": {
"@babel/preset-env": "^7.21.5",
"@babel/preset-typescript": "^7.21.5",
"@scripts/vitest-config": "workspace:*",
"@types/babel__core": "^7.20.0",
"@types/caniuse-lite": "^1.0.1",
Expand Down
8 changes: 4 additions & 4 deletions packages/builder/builder-shared/scripts/postCompile.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ const { performance } = require('perf_hooks');

async function compileRetryRuntime() {
const { minify } = await import('terser');
const runtimeCode = await readFile(
path.join(__dirname, '../dist/runtime/assetsRetry.js'),
'utf8',
);
const source = path.join(__dirname, '../src/runtime/assetsRetry.ts');
const runtimeCode = await readFile(source, 'utf8');
const distPath = path.join(__dirname, '../compiled/assetsRetry.js');
const { code } = await transformAsync(runtimeCode, {
presets: [
'@babel/preset-typescript',
[
'@babel/preset-env',
{
Expand All @@ -20,6 +19,7 @@ async function compileRetryRuntime() {
},
],
],
filename: source,
});
const { code: minifiedRuntimeCode } = await minify(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,9 @@ export class AssetsRetryPlugin implements WebpackPluginInstance {
);
const runtimeFilePath = getSharedPkgCompiledPath('assetsRetry.js');
const runtimeCode = await fs.readFile(runtimeFilePath, 'utf-8');
// Runtime code will include `Object.defineProperty(exports,"__esModule",{value:!0})` after compiled by tsc
// So we inject `var exports={}` to avoid `exports is not defined` error
return `var exports={};${runtimeCode}init(${serialize(
return `(function(){${runtimeCode};init(${serialize(
this.#retryOptions,
)})`;
)});})()`;
}

async getScriptPath() {
Expand Down
2 changes: 1 addition & 1 deletion packages/builder/builder-shared/src/runtime/assetsRetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ function reloadElementResource(
}
}

export function retry(config: AssetsRetryOptions, e: Event) {
function retry(config: AssetsRetryOptions, e: Event) {
const targetInfo = validateTargetInfo(config, e);
if (targetInfo === false) {
return;
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5a3eeff

Please sign in to comment.