Skip to content

Commit de10cf4

Browse files
committed
wip: support assets
1 parent d935d3a commit de10cf4

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
@@ -237,20 +237,24 @@ export function assetPlugin(config: ResolvedConfig): Plugin {
237237
},
238238
},
239239

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

255259
generateBundle(_, bundle) {
256260
// Remove empty entry point file
@@ -308,7 +312,7 @@ export async function fileToUrl(
308312
id: string,
309313
): Promise<string> {
310314
const { environment } = pluginContext
311-
if (environment.config.command === 'serve') {
315+
if (!environment.config.isBundled) {
312316
return fileToDevUrl(environment, id)
313317
} else {
314318
return fileToBuiltUrl(pluginContext, id)
@@ -457,7 +461,23 @@ async function fileToBuiltUrl(
457461
postfix = postfix.replace(noInlineRE, '').replace(/^&/, '?')
458462
}
459463

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

463483
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)