Skip to content

Commit 601f47e

Browse files
committed
wip: support assets
1 parent 495e683 commit 601f47e

File tree

2 files changed

+49
-15
lines changed

2 files changed

+49
-15
lines changed

packages/vite/src/node/plugins/asset.ts

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -238,20 +238,24 @@ export function assetPlugin(config: ResolvedConfig): Plugin {
238238
},
239239
},
240240

241-
renderChunk(code, chunk, opts) {
242-
const s = renderAssetUrlInJS(this, chunk, opts, code)
243-
244-
if (s) {
245-
return {
246-
code: s.toString(),
247-
map: this.environment.config.build.sourcemap
248-
? s.generateMap({ hires: 'boundary' })
249-
: null,
241+
...(config.command === 'build'
242+
? {
243+
renderChunk(code, chunk, opts) {
244+
const s = renderAssetUrlInJS(this, chunk, opts, code)
245+
246+
if (s) {
247+
return {
248+
code: s.toString(),
249+
map: this.environment.config.build.sourcemap
250+
? s.generateMap({ hires: 'boundary' })
251+
: null,
252+
}
253+
} else {
254+
return null
255+
}
256+
},
250257
}
251-
} else {
252-
return null
253-
}
254-
},
258+
: {}),
255259

256260
generateBundle(_, bundle) {
257261
// Remove empty entry point file
@@ -309,7 +313,7 @@ export async function fileToUrl(
309313
id: string,
310314
): Promise<string> {
311315
const { environment } = pluginContext
312-
if (environment.config.command === 'serve') {
316+
if (!environment.config.isBundled) {
313317
return fileToDevUrl(environment, id)
314318
} else {
315319
return fileToBuiltUrl(pluginContext, id)
@@ -458,7 +462,23 @@ async function fileToBuiltUrl(
458462
postfix = postfix.replace(noInlineRE, '').replace(/^&/, '?')
459463
}
460464

461-
url = `__VITE_ASSET__${referenceId}__${postfix ? `$_${postfix}__` : ``}`
465+
if (environment.config.experimental.fullBundleMode) {
466+
const outputFilename = pluginContext.getFileName(referenceId)
467+
const outputUrl = toOutputFilePathInJS(
468+
environment,
469+
outputFilename,
470+
'asset',
471+
'assets/dummy.js',
472+
'js',
473+
() => {
474+
throw new Error('unreachable')
475+
},
476+
)
477+
if (typeof outputUrl === 'object') throw new Error('not supported')
478+
url = outputUrl
479+
} else {
480+
url = `__VITE_ASSET__${referenceId}__${postfix ? `$_${postfix}__` : ``}`
481+
}
462482
}
463483

464484
cache.set(id, url)

packages/vite/src/node/server/environments/fullBundleEnvironment.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,20 @@ export class FullBundleDevEnvironment extends DevEnvironment {
236236

237237
rolldownOptions.treeshake = false
238238

239+
// set filenames to make output paths predictable so that `renderChunk` hook does not need to be used
240+
if (Array.isArray(rolldownOptions.output)) {
241+
for (const output of rolldownOptions.output) {
242+
output.entryFileNames = 'assets/[name].js'
243+
output.chunkFileNames = 'assets/[name]-[hash].js'
244+
output.assetFileNames = 'assets/[name]-[hash][extname]'
245+
}
246+
} else {
247+
rolldownOptions.output ??= {}
248+
rolldownOptions.output.entryFileNames = 'assets/[name].js'
249+
rolldownOptions.output.chunkFileNames = 'assets/[name]-[hash].js'
250+
rolldownOptions.output.assetFileNames = 'assets/[name]-[hash][extname]'
251+
}
252+
239253
return rolldownOptions
240254
}
241255

0 commit comments

Comments
 (0)