Skip to content

Commit

Permalink
fix: fix issue where minissg does not work on Windows
Browse files Browse the repository at this point in the history
Dynamic import expression must be given a file URL, not a file path.
On Windows, Node.js confuses the drive letter in a file path with
URL's protocol part.
  • Loading branch information
uenoB committed Sep 9, 2024
1 parent 3f42add commit f94a3d6
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/vite-plugin-minissg/src/build.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { resolve } from 'node:path'
import { pathToFileURL } from 'node:url'
import { format } from 'node:util'
import type { Plugin, Rollup, UserConfig, InlineConfig } from 'vite'
import { build } from 'vite'
Expand All @@ -11,6 +12,8 @@ import type { LibModule } from './loader'
import { Lib, Exact, Head, loaderPlugin, clientNodeInfo } from './loader'
import * as util from './util'

const fileURL = (...a: string[]): string => pathToFileURL(resolve(...a)).href

const setupRoot = (
outDir: string,
lib: PromiseLike<LibModule>,
Expand All @@ -26,7 +29,7 @@ const setupRoot = (
if (r == null || r.external !== false) return [k, { default: null }]
const chunk = chunkMap.get(r.id)
if (chunk == null) return [k, { default: null }]
const fileName = resolve(outDir, chunk.fileName)
const fileName = fileURL(outDir, chunk.fileName)
const module = util.lazy((): PromiseLike<Module> => import(fileName))
const main = (): PromiseLike<Module> =>
lib.then(m => m.add(r.id)).then(() => module)
Expand Down Expand Up @@ -199,7 +202,7 @@ export const buildPlugin = (
const dir = outputOptions.dir ?? site.config.build.outDir
const outDir = resolve(site.config.root, dir)
if (libEmitId == null) throw Error('Lib module not found')
const libFileName = resolve(outDir, this.getFileName(libEmitId))
const libFileName = fileURL(outDir, this.getFileName(libEmitId))
const lib = util.lazy((): PromiseLike<LibModule> => import(libFileName))
const root = setupRoot(outDir, lib, bundle, entryModules)
const input = await generateInput(this, site, entryModules)
Expand Down

0 comments on commit f94a3d6

Please sign in to comment.