From d463bd9872b112c16c6bb8d4d23a551474347386 Mon Sep 17 00:00:00 2001 From: Torstein Ringnes Date: Wed, 22 Sep 2021 15:34:28 +0200 Subject: [PATCH 1/2] feat: build library new option - emitAssets #4454 #3295 --- packages/vite/src/node/build.ts | 1 + packages/vite/src/node/plugins/asset.ts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index 465efee7e2a21e..bef4cd6652e184 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -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' diff --git a/packages/vite/src/node/plugins/asset.ts b/packages/vite/src/node/plugins/asset.ts index 6ea01788df2a88..69d0f78fdfec79 100644 --- a/packages/vite/src/node/plugins/asset.ts +++ b/packages/vite/src/node/plugins/asset.ts @@ -287,7 +287,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)) ) { From f5d7286682a8bcf46f9093831d664247343563c5 Mon Sep 17 00:00:00 2001 From: Torstein Ringnes Date: Mon, 27 Sep 2021 14:08:24 +0200 Subject: [PATCH 2/2] feat: lib build - emit assets - added tests --- packages/playground/lib/__tests__/lib.spec.ts | 12 +++++++++++- packages/playground/lib/__tests__/serve.js | 6 ++++++ packages/playground/lib/index.dist.html | 13 +++++++++++++ packages/playground/lib/src/emitAssets.js | 4 ++++ packages/playground/lib/src/main.js | 6 ++++++ .../playground/lib/vite.emitAssets.config.js | 19 +++++++++++++++++++ 6 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 packages/playground/lib/src/emitAssets.js create mode 100644 packages/playground/lib/vite.emitAssets.config.js diff --git a/packages/playground/lib/__tests__/lib.spec.ts b/packages/playground/lib/__tests__/lib.spec.ts index 9978d62ac777ad..61f00625fb22db 100644 --- a/packages/playground/lib/__tests__/lib.spec.ts +++ b/packages/playground/lib/__tests__/lib.spec.ts @@ -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' @@ -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( diff --git a/packages/playground/lib/__tests__/serve.js b/packages/playground/lib/__tests__/serve.js index 15c64de40276d1..1d9a1ba3edd0ae 100644 --- a/packages/playground/lib/__tests__/serve.js +++ b/packages/playground/lib/__tests__/serve.js @@ -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) => { diff --git a/packages/playground/lib/index.dist.html b/packages/playground/lib/index.dist.html index b10b55e0fd913d..2079e204956805 100644 --- a/packages/playground/lib/index.dist.html +++ b/packages/playground/lib/index.dist.html @@ -3,6 +3,19 @@
+
+ <-background-image should be inline unless emitAssets:true is set +
+
+ +