Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: lib mode respect rollup output config #8597

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 41 additions & 18 deletions packages/vite/src/node/config.ts
Expand Up @@ -7,7 +7,7 @@ import colors from 'picocolors'
import type { Alias, AliasOptions } from 'types/alias'
import aliasPlugin from '@rollup/plugin-alias'
import { build } from 'esbuild'
import type { RollupOptions } from 'rollup'
import type { OutputOptions, RollupOptions } from 'rollup'
import type { Plugin } from './plugin'
import type { BuildOptions, ResolvedBuildOptions } from './build'
import { resolveBuildOptions } from './build'
Expand Down Expand Up @@ -570,31 +570,54 @@ export async function resolveConfig(
}

// Check if all assetFileNames have the same reference.
// If not, display a warn for user.
// If not, display a warn for user. (exclude lib mode, it will make all asset to base64)
const outputOption = config.build?.rollupOptions?.output ?? []
// Use isArray to narrow its type to array
if (Array.isArray(outputOption)) {
const assetFileNamesList = outputOption.map(
(output) => output.assetFileNames
)
if (assetFileNamesList.length > 1) {
const firstAssetFileNames = assetFileNamesList[0]
const hasDifferentReference = assetFileNamesList.some(
(assetFileNames) => assetFileNames !== firstAssetFileNames
if (
!config.build?.lib &&
!verifyRollupOutputOption(outputOption, 'assetFileNames')
) {
resolved.logger.warn(
colors.yellow(
`assetFileNames isn't equal for every build.rollupOptions.output. ` +
`A single pattern across all outputs is supported by Vite.`
)
if (hasDifferentReference) {
resolved.logger.warn(
colors.yellow(`
assetFileNames isn't equal for every build.rollupOptions.output. A single pattern across all outputs is supported by Vite.
`)
)
}
}
)
}

const workerOutputOption = config.worker?.rollupOptions?.output ?? []
const differentOpts = [
!verifyRollupOutputOption(workerOutputOption, 'assetFileNames') &&
'assetFileNames',
!verifyRollupOutputOption(workerOutputOption, 'chunkFileNames') &&
'chunkFileNames',
!verifyRollupOutputOption(workerOutputOption, 'entryFileNames') &&
'entryFileNames'
poyoho marked this conversation as resolved.
Show resolved Hide resolved
].filter(Boolean)
if (differentOpts.length > 0) {
resolved.logger.warn(
colors.yellow(
`${differentOpts.join(
','
patak-dev marked this conversation as resolved.
Show resolved Hide resolved
)} isn't equal for every worker.rollupOptions.output. ` +
`A single pattern across all outputs is supported by Vite.`
)
)
}
return resolved
}

function verifyRollupOutputOption(
options: OutputOptions | OutputOptions[],
field: 'assetFileNames' | 'chunkFileNames' | 'entryFileNames'
): boolean {
if (Array.isArray(options)) {
const names = options.map((option) => option[field])
return names.length > 1 && names.some((name) => name !== names[0])
}
return false
}

/**
* Resolve base. Note that some users use Vite to build for non-web targets like
* electron or expects to deploy
Expand Down
7 changes: 6 additions & 1 deletion packages/vite/src/node/plugins/css.ts
Expand Up @@ -597,8 +597,13 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {

let extractedCss = outputToExtractedCSSMap.get(opts)
if (extractedCss && !hasEmitted) {
hasEmitted = true
extractedCss = await finalizeCss(extractedCss, true, config)

// lib mode should emit css each rollup.generate
if (!config.build.lib) {
hasEmitted = true
}

this.emitFile({
name: cssBundleName,
type: 'asset',
Expand Down
Binary file added playground/lib/src/asset.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions playground/lib/src/main.js
@@ -1,9 +1,19 @@
import './dynamic.css'
import png from './asset.png?url'
export default function myLib(sel) {
const worker = new Worker(new URL('./worker.js', import.meta.url))
worker.addEventListener('message', (e) => {
console.log(e.data)
})
// Force esbuild spread helpers (https://github.com/evanw/esbuild/issues/951)
console.log({ ...'foo' })

document.querySelector(sel).textContent = 'It works'

const img = document.createElement('img')
img.src = png
document.body.appendChild(img)

// Env vars should not be replaced
console.log(process.env.NODE_ENV)
}
1 change: 1 addition & 0 deletions playground/lib/src/worker.js
@@ -0,0 +1 @@
self.postMessage('hello')
24 changes: 20 additions & 4 deletions playground/lib/vite.config.js
Expand Up @@ -7,14 +7,30 @@ const path = require('path')
module.exports = {
build: {
rollupOptions: {
output: {
banner: `/*!\nMayLib\n*/`
}
output: [
{
format: 'es',
banner: `/*!\nMayLib\n*/`,
assetFileNames: `subdir/assets.[name].[ext]`,
chunkFileNames: `subdir/chunk.[name].js`
},
{
format: 'umd',
banner: `/*!\nMayLib\n*/`,
assetFileNames: `subdir2/assets.[name].[ext]`,
chunkFileNames: `subdir2/chunk.[name].js`
},
{
format: 'iife',
banner: `/*!\nMayLib\n*/`,
assetFileNames: `subdir3/assets.[name].[ext]`,
chunkFileNames: `subdir3/chunk.[name].js`
}
]
},
lib: {
entry: path.resolve(__dirname, 'src/main.js'),
name: 'MyLib',
formats: ['es', 'umd', 'iife'],
fileName: 'my-lib-custom-filename'
}
},
Expand Down
4 changes: 2 additions & 2 deletions playground/vue-lib/src-consumer/index.ts
@@ -1,7 +1,7 @@
import { createApp } from 'vue'
// @ts-ignore
import { CompA } from '../dist/lib/my-vue-lib'
import '../dist/lib/style.css'
import { CompA } from '../dist/lib/my-vue-lib.mjs'
import '../dist/lib/assets1.style.css'

const app = createApp(CompA)
app.mount('#app')
17 changes: 14 additions & 3 deletions playground/vue-lib/vite.config.lib.ts
Expand Up @@ -14,9 +14,20 @@ export default defineConfig({
},
rollupOptions: {
external: ['vue'],
output: {
globals: { vue: 'Vue' }
}
output: [
{
globals: { vue: 'Vue' },
format: 'es',
assetFileNames: `assets1.[name].[ext]`,
chunkFileNames: `chunk1.[name].js`
},
{
globals: { vue: 'Vue' },
format: 'es',
assetFileNames: `assets2.[name].[ext]`,
chunkFileNames: `chunk2.[name].js`
}
]
}
},
plugins: [vue()]
Expand Down