From 4911ea32edd0a2c000498a3a386a78a0988d4523 Mon Sep 17 00:00:00 2001 From: GiveMe-A-Name Date: Fri, 14 Apr 2023 11:35:59 +0800 Subject: [PATCH 1/5] fix: the http://0.0.0.0:port cant visit in windows, we shouldnt set publicPath as //0.0.0.0:${port}/ --- .changeset/blue-kiwis-raise.md | 6 ++++++ packages/builder/builder-shared/src/apply/output.ts | 8 +++++++- tests/integration/asset-prefix/test/index.test.ts | 7 +++---- 3 files changed, 16 insertions(+), 5 deletions(-) create mode 100644 .changeset/blue-kiwis-raise.md diff --git a/.changeset/blue-kiwis-raise.md b/.changeset/blue-kiwis-raise.md new file mode 100644 index 00000000000..e933330145b --- /dev/null +++ b/.changeset/blue-kiwis-raise.md @@ -0,0 +1,6 @@ +--- +'@modern-js/builder-shared': patch +--- + +fix: The http://0.0.0.0:port can't visit in windows, we shouldn't set publicPath as `//0.0.0.0:${port}/`; +fix: 在 windows 里不能正常访问 http://0.0.0.0:port,我们不应该将 publicPath 设置成 `//0.0.0.0:${port}` diff --git a/packages/builder/builder-shared/src/apply/output.ts b/packages/builder/builder-shared/src/apply/output.ts index 5c854d76183..afb35186a45 100644 --- a/packages/builder/builder-shared/src/apply/output.ts +++ b/packages/builder/builder-shared/src/apply/output.ts @@ -82,7 +82,13 @@ function getPublicPath({ } else if (dev.assetPrefix === true) { const hostname = context.devServer?.hostname || DEFAULT_DEV_HOST; const port = context.devServer?.port || DEFAULT_PORT; - publicPath = `//${hostname}:${port}/`; + if (hostname !== DEFAULT_DEV_HOST) { + // If user not specify the hostname, it would use DEFAULT_DEV_HOST(0.0.0.0) + // The http://0.0.0.0:port can't visit in windows, so we shouldn't set publicPath as `//0.0.0.0:${port}/`; + // Relative to docs: + // - https://github.com/quarkusio/quarkus/issues/12246 + publicPath = `//${hostname}:${port}/`; + } } return addTrailingSlash(publicPath); diff --git a/tests/integration/asset-prefix/test/index.test.ts b/tests/integration/asset-prefix/test/index.test.ts index f87a375c5a5..e7341b9db07 100644 --- a/tests/integration/asset-prefix/test/index.test.ts +++ b/tests/integration/asset-prefix/test/index.test.ts @@ -1,7 +1,6 @@ import path from 'path'; import { readFileSync } from 'fs'; import { Page } from 'puppeteer'; -import { DEFAULT_DEV_HOST } from '@modern-js/utils'; import { launchApp, killApp } from '../../../utils/modernTestUtils'; declare const page: Page; @@ -18,7 +17,7 @@ describe('asset prefix', () => { path.join(appDir, 'dist/html/main/index.html'), 'utf-8', ); - expect(HTML.includes(`//${DEFAULT_DEV_HOST}:3333/static/js/`)).toBeTruthy(); + expect(HTML.includes(`/static/js/`)).toBeTruthy(); killApp(app); }); @@ -27,7 +26,7 @@ describe('asset prefix', () => { const appDir = path.resolve(fixtures, 'dev-asset-prefix'); const app = await launchApp(appDir); - const expected = `//${DEFAULT_DEV_HOST}:3333`; + const expected = ''; const mainJs = readFileSync( path.join(appDir, 'dist/static/js/main.js'), @@ -38,7 +37,7 @@ describe('asset prefix', () => { mainJs.includes(`window.__assetPrefix__ = '${expected}';`), ).toBeTruthy(); - await page.goto(`http:${expected}`); + await page.goto(`http://localhost:3333`); const assetPrefix = await page.evaluate(() => { // eslint-disable-next-line @typescript-eslint/ban-ts-comment From bc79429e738959f68da91d0fd370450a315ee9ff Mon Sep 17 00:00:00 2001 From: GiveMe-A-Name Date: Fri, 14 Apr 2023 15:11:10 +0800 Subject: [PATCH 2/5] fix: save hostname when assetPrefix set true --- packages/builder/builder-shared/src/apply/output.ts | 9 +++++++-- tests/integration/asset-prefix/test/index.test.ts | 7 ++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/builder/builder-shared/src/apply/output.ts b/packages/builder/builder-shared/src/apply/output.ts index afb35186a45..0c1a4882cbf 100644 --- a/packages/builder/builder-shared/src/apply/output.ts +++ b/packages/builder/builder-shared/src/apply/output.ts @@ -82,14 +82,19 @@ function getPublicPath({ } else if (dev.assetPrefix === true) { const hostname = context.devServer?.hostname || DEFAULT_DEV_HOST; const port = context.devServer?.port || DEFAULT_PORT; - if (hostname !== DEFAULT_DEV_HOST) { - // If user not specify the hostname, it would use DEFAULT_DEV_HOST(0.0.0.0) + if (hostname === DEFAULT_DEV_HOST) { + const localHostname = `127.0.0.1`; + // If user not specify the hostname, it would use 0.0.0.0 // The http://0.0.0.0:port can't visit in windows, so we shouldn't set publicPath as `//0.0.0.0:${port}/`; // Relative to docs: // - https://github.com/quarkusio/quarkus/issues/12246 + publicPath = `//${localHostname}:${port}/`; + } else { publicPath = `//${hostname}:${port}/`; } } + console.info('publicPath', publicPath); + return addTrailingSlash(publicPath); } diff --git a/tests/integration/asset-prefix/test/index.test.ts b/tests/integration/asset-prefix/test/index.test.ts index e7341b9db07..c7fa44978c3 100644 --- a/tests/integration/asset-prefix/test/index.test.ts +++ b/tests/integration/asset-prefix/test/index.test.ts @@ -3,6 +3,7 @@ import { readFileSync } from 'fs'; import { Page } from 'puppeteer'; import { launchApp, killApp } from '../../../utils/modernTestUtils'; +const DEFAULT_DEV_HOST = '127.0.0.1'; declare const page: Page; const fixtures = path.resolve(__dirname, '../fixtures'); @@ -17,7 +18,7 @@ describe('asset prefix', () => { path.join(appDir, 'dist/html/main/index.html'), 'utf-8', ); - expect(HTML.includes(`/static/js/`)).toBeTruthy(); + expect(HTML.includes(`//${DEFAULT_DEV_HOST}:3333/static/js/`)).toBeTruthy(); killApp(app); }); @@ -26,7 +27,7 @@ describe('asset prefix', () => { const appDir = path.resolve(fixtures, 'dev-asset-prefix'); const app = await launchApp(appDir); - const expected = ''; + const expected = `//${DEFAULT_DEV_HOST}:3333`; const mainJs = readFileSync( path.join(appDir, 'dist/static/js/main.js'), @@ -37,7 +38,7 @@ describe('asset prefix', () => { mainJs.includes(`window.__assetPrefix__ = '${expected}';`), ).toBeTruthy(); - await page.goto(`http://localhost:3333`); + await page.goto(`http:${expected}`); const assetPrefix = await page.evaluate(() => { // eslint-disable-next-line @typescript-eslint/ban-ts-comment From 1d27c56b5d2ba7c3da7e7375f38d24dc6e5c7dcb Mon Sep 17 00:00:00 2001 From: GiveMe-A-Name Date: Fri, 14 Apr 2023 15:15:15 +0800 Subject: [PATCH 3/5] fix: remove console info --- packages/builder/builder-shared/src/apply/output.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/builder/builder-shared/src/apply/output.ts b/packages/builder/builder-shared/src/apply/output.ts index 0c1a4882cbf..25ecc57e8bb 100644 --- a/packages/builder/builder-shared/src/apply/output.ts +++ b/packages/builder/builder-shared/src/apply/output.ts @@ -94,7 +94,5 @@ function getPublicPath({ } } - console.info('publicPath', publicPath); - return addTrailingSlash(publicPath); } From c8632d3904eeea96a59789e44483ba02ec2d118a Mon Sep 17 00:00:00 2001 From: GiveMe-A-Name Date: Mon, 17 Apr 2023 10:01:37 +0800 Subject: [PATCH 4/5] perf: set the localHostname as localhost --- packages/builder/builder-shared/src/apply/output.ts | 2 +- .../document/builder-doc/docs/en/config/dev/assetPrefix.md | 4 ++-- .../document/builder-doc/docs/zh/config/dev/assetPrefix.md | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/builder/builder-shared/src/apply/output.ts b/packages/builder/builder-shared/src/apply/output.ts index 25ecc57e8bb..5438c8cbba0 100644 --- a/packages/builder/builder-shared/src/apply/output.ts +++ b/packages/builder/builder-shared/src/apply/output.ts @@ -83,7 +83,7 @@ function getPublicPath({ const hostname = context.devServer?.hostname || DEFAULT_DEV_HOST; const port = context.devServer?.port || DEFAULT_PORT; if (hostname === DEFAULT_DEV_HOST) { - const localHostname = `127.0.0.1`; + const localHostname = `localhost`; // If user not specify the hostname, it would use 0.0.0.0 // The http://0.0.0.0:port can't visit in windows, so we shouldn't set publicPath as `//0.0.0.0:${port}/`; // Relative to docs: diff --git a/packages/document/builder-doc/docs/en/config/dev/assetPrefix.md b/packages/document/builder-doc/docs/en/config/dev/assetPrefix.md index 5c37bdb60f3..37dd6b7f9f2 100644 --- a/packages/document/builder-doc/docs/en/config/dev/assetPrefix.md +++ b/packages/document/builder-doc/docs/en/config/dev/assetPrefix.md @@ -9,7 +9,7 @@ This config is only used in the development environment. In the production envir #### Boolean Type -If `assetPrefix` is set to `true`, the URL prefix will be `//ip:port/`: +If `assetPrefix` is set to `true`, the URL prefix will be `//localhost:port/`: ```js export default { @@ -22,7 +22,7 @@ export default { The script URL will be: ```js - + ``` If `assetPrefix` is set to `false` or not set, `/` is used as the default value. diff --git a/packages/document/builder-doc/docs/zh/config/dev/assetPrefix.md b/packages/document/builder-doc/docs/zh/config/dev/assetPrefix.md index a7f5e2d2f69..50e09a311e0 100644 --- a/packages/document/builder-doc/docs/zh/config/dev/assetPrefix.md +++ b/packages/document/builder-doc/docs/zh/config/dev/assetPrefix.md @@ -9,7 +9,7 @@ #### Boolean 类型 -如果设置 `assetPrefix` 为 `true`,Builder 会自动计算出 `//ip:port/` 作为 URL 前缀: +如果设置 `assetPrefix` 为 `true`,Builder 会使用 `//localhost:port/` 作为 URL 前缀: ```js export default { @@ -22,7 +22,7 @@ export default { 对应 JS 文件在浏览器中加载的地址如下: ```js - + ``` 如果设置 `assetPrefix` 为 `false` 或不设置,则默认使用 `/` 作为访问前缀。 From 419f647073b4a8f37e237b823516a3c236ba208a Mon Sep 17 00:00:00 2001 From: GiveMe-A-Name Date: Mon, 17 Apr 2023 10:05:57 +0800 Subject: [PATCH 5/5] fix: update integration test --- tests/integration/asset-prefix/test/index.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/asset-prefix/test/index.test.ts b/tests/integration/asset-prefix/test/index.test.ts index fc51c59d9c0..e61a2aa3181 100644 --- a/tests/integration/asset-prefix/test/index.test.ts +++ b/tests/integration/asset-prefix/test/index.test.ts @@ -3,7 +3,7 @@ import { readFileSync } from 'fs'; import { Page } from 'puppeteer'; import { launchApp, killApp } from '../../../utils/modernTestUtils'; -const DEFAULT_DEV_HOST = '127.0.0.1'; +const DEFAULT_DEV_HOST = 'localhost'; declare const page: Page; const fixtures = path.resolve(__dirname, '../fixtures');