From 60fa6ba082c50d57ec4abecb196573f4100636d2 Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Thu, 14 Jul 2022 16:41:54 +0800 Subject: [PATCH 1/5] docs: update library mode info (#9102) --- docs/guide/build.md | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/docs/guide/build.md b/docs/guide/build.md index a6bd0a5106587a..5fe3c2a78e25f8 100644 --- a/docs/guide/build.md +++ b/docs/guide/build.md @@ -163,8 +163,8 @@ Running `vite build` with this config uses a Rollup preset that is oriented towa ``` $ vite build building for production... -[write] my-lib.mjs 0.08kb, brotli: 0.07kb -[write] my-lib.umd.js 0.30kb, brotli: 0.16kb +dist/my-lib.js 0.08 KiB / gzip: 0.07 KiB +dist/my-lib.umd.cjs 0.30 KiB / gzip: 0.16 KiB ``` Recommended `package.json` for your lib: @@ -172,18 +172,23 @@ Recommended `package.json` for your lib: ```json { "name": "my-lib", + "type": "module", "files": ["dist"], - "main": "./dist/my-lib.umd.js", - "module": "./dist/my-lib.mjs", + "main": "./dist/my-lib.umd.cjs", + "module": "./dist/my-lib.js", "exports": { ".": { - "import": "./dist/my-lib.mjs", - "require": "./dist/my-lib.umd.js" + "import": "./dist/my-lib.js", + "require": "./dist/my-lib.umd.cjs" } } } ``` +::: tip Note +If the `package.json` does not contain `"type": "module"`, Vite will generate different file extensions for Node.js compatibility. `.js` will become `.mjs` and `.cjs` will become `.js`. +::: + ## Advanced Base Options ::: warning From b3934516431659b36ccfbfe1a87a3e73595982a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Thu, 14 Jul 2022 18:26:40 +0900 Subject: [PATCH 2/5] fix: resolve drive relative path (#9097) --- packages/vite/src/node/plugins/resolve.ts | 12 ++++++ playground/resolve/__tests__/resolve.spec.ts | 10 ++++- playground/resolve/absolute.js | 1 + playground/resolve/drive-relative.js | 1 + playground/resolve/index.html | 7 ++++ playground/resolve/vite.config.js | 43 ++++++++++++++++++++ 6 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 playground/resolve/absolute.js create mode 100644 playground/resolve/drive-relative.js diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 1ebe847359384c..af86ef76193c92 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -28,6 +28,7 @@ import { isObject, isPossibleTsOutput, isTsRequest, + isWindows, nestedResolveFrom, normalizePath, resolveFrom, @@ -243,6 +244,17 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin { } } + // drive relative fs paths (only windows) + if (isWindows && id.startsWith('/')) { + const basedir = importer ? path.dirname(importer) : process.cwd() + const fsPath = path.resolve(basedir, id) + if ((res = tryFsResolve(fsPath, options))) { + isDebug && + debug(`[drive-relative] ${colors.cyan(id)} -> ${colors.dim(res)}`) + return res + } + } + // absolute fs paths if ( isNonDriveRelativeAbsolutePath(id) && diff --git a/playground/resolve/__tests__/resolve.spec.ts b/playground/resolve/__tests__/resolve.spec.ts index 79ab9dce6409c3..e95e6d78c409c5 100644 --- a/playground/resolve/__tests__/resolve.spec.ts +++ b/playground/resolve/__tests__/resolve.spec.ts @@ -1,4 +1,4 @@ -import { isBuild, page } from '~utils' +import { isBuild, isWindows, page } from '~utils' test('bom import', async () => { expect(await page.textContent('.utf8-bom')).toMatch('[success]') @@ -75,6 +75,14 @@ test('filename with dot', async () => { expect(await page.textContent('.dot')).toMatch('[success]') }) +test.runIf(isWindows)('drive-relative path', async () => { + expect(await page.textContent('.drive-relative')).toMatch('[success]') +}) + +test('absolute path', async () => { + expect(await page.textContent('.absolute')).toMatch('[success]') +}) + test('browser field', async () => { expect(await page.textContent('.browser')).toMatch('[success]') }) diff --git a/playground/resolve/absolute.js b/playground/resolve/absolute.js new file mode 100644 index 00000000000000..c0581888ebb90b --- /dev/null +++ b/playground/resolve/absolute.js @@ -0,0 +1 @@ +export default '[success] absolute' diff --git a/playground/resolve/drive-relative.js b/playground/resolve/drive-relative.js new file mode 100644 index 00000000000000..188ac36e661b35 --- /dev/null +++ b/playground/resolve/drive-relative.js @@ -0,0 +1 @@ +export default '[success] drive relative' diff --git a/playground/resolve/index.html b/playground/resolve/index.html index 1920ebb675d24c..a1befcc2757348 100644 --- a/playground/resolve/index.html +++ b/playground/resolve/index.html @@ -55,6 +55,12 @@

Resolve file name containing dot

fail

+

Resolve drive-relative path (Windows only)

+

fail

+ +

Resolve absolute path

+

fail

+

Browser Field

fail

@@ -89,6 +95,7 @@

resolve package that contains # in path