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: build library new option - emitAssets #5028

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/playground/lib/__tests__/lib.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isBuild, findAssetFile, testDir } from 'testUtils'
import { isBuild, findAssetFile, testDir, getBg } from 'testUtils'
import path from 'path'
import fs from 'fs'

Expand All @@ -11,6 +11,16 @@ if (isBuild) {
expect(await page.textContent('.umd')).toBe('It works')
})

test('lib: emitAssets:undefined|false = is inlined', async () => {
const match = `data:image/png;base64`
expect(await getBg('.emitAssets-default')).toMatch(match)
})

test('lib: emitAssets:true = is emitted', async () => {
const match = /\/assets\/asset\.\w{8}\.png/
expect(await getBg('.emitAssets-true')).toMatch(match)
})

test('Library mode does not include `preload`', async () => {
expect(await page.textContent('.dynamic-import-message')).toBe('hello vite')
const code = fs.readFileSync(
Expand Down
6 changes: 6 additions & 0 deletions packages/playground/lib/__tests__/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ exports.serve = async function serve(root, isBuildTest) {
configFile: path.resolve(__dirname, '../vite.dyimport.config.js')
})

await build({
root,
logLevel: 'silent',
configFile: path.resolve(__dirname, '../vite.emitAssets.config.js')
})

// start static file server
const serve = sirv(path.resolve(root, 'dist'))
const httpServer = http.createServer((req, res) => {
Expand Down
13 changes: 13 additions & 0 deletions packages/playground/lib/index.dist.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@
<div class="umd"></div>
<div class="dynamic-import-message"></div>

<div
class="emitAssets-default"
style="
background-size: contain;
background-repeat: no-repeat;
padding-left: 20px;
"
>
<-background-image should be inline unless emitAssets:true is set
</div>
<div class="emitAssets-true"></div>
<script src="./lib2/emit-assets.js"></script>

<script type="module">
import myLib from './my-lib-custom-filename.es.js'

Expand Down
4 changes: 4 additions & 0 deletions packages/playground/lib/src/emitAssets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import asset from '../../assets/nested/asset.png'
document.querySelector(
'.emitAssets-true'
).style.backgroundImage = `url(${asset})`
6 changes: 6 additions & 0 deletions packages/playground/lib/src/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export default function myLib(sel) {
document.querySelector(sel).textContent = 'It works'
document.querySelector(
'.emitAssets-default'
).style.backgroundImage = `url(${new URL(
`../../assets/nested/asset.png`,
import.meta.url
)})`
}
19 changes: 19 additions & 0 deletions packages/playground/lib/vite.emitAssets.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const fs = require('fs')
const path = require('path')

/**
* @type {import('vite').UserConfig}
*/
module.exports = {
build: {
minify: false,
lib: {
entry: path.resolve(__dirname, 'src/emitAssets.js'),
formats: ['es'],
name: 'emitAssets',
fileName: () => `emit-assets.js`,
emitAssets: true
},
outDir: 'dist/lib2'
}
}
1 change: 1 addition & 0 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ export interface LibraryOptions {
name?: string
formats?: LibraryFormats[]
fileName?: string | ((format: ModuleFormat) => string)
emitAssets?: boolean
}

export type LibraryFormats = 'es' | 'cjs' | 'umd' | 'iife'
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ async function fileToBuiltUrl(

let url: string
if (
config.build.lib ||
(config.build.lib && !config.build.lib.emitAssets) ||
(!file.endsWith('.svg') &&
content.length < Number(config.build.assetsInlineLimit))
) {
Expand Down