diff --git a/packages/playground/lib/package.json b/packages/playground/lib/package.json index c6b75f4fa7c6b2..7a857ec16590b8 100644 --- a/packages/playground/lib/package.json +++ b/packages/playground/lib/package.json @@ -1,5 +1,5 @@ { - "name": "my-lib", + "name": "@example/my-lib", "private": true, "version": "0.0.0", "scripts": { diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index 1e1df8b8d1c7ba..42c79c5fb1237a 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -340,9 +340,7 @@ async function doBuild( paralellBuilds.push(bundle) - const pkgName = - libOptions && - JSON.parse(lookupFile(config.root, ['package.json']) || `{}`).name + const pkgName = libOptions && getPkgName(config.root) const generate = (output: OutputOptions = {}) => { return bundle[options.write ? 'write' : 'generate']({ @@ -432,6 +430,14 @@ async function doBuild( } } +function getPkgName(root: string) { + const { name } = JSON.parse(lookupFile(root, ['package.json']) || `{}`) + + if (!name) throw new Error('no name found in package.json') + + return name.startsWith('@') ? name.split('/')[1] : name +} + function createMoveToVendorChunkFn(config: ResolvedConfig): GetManualChunk { const cache = new Map() return (id, { getModuleInfo }) => {