MCPcopy
hub / github.com/vitejs/vite / renderChunk

Function renderChunk

packages/vite/src/node/plugins/terser.ts:85–133  ·  view source on GitHub ↗
(code, chunk, outputOptions)

Source from the content-addressed store, hash-verified

83 },
84
85 async renderChunk(code, chunk, outputOptions) {
86 // This plugin is included for any non-false value of config.build.minify,
87 // so that normal chunks can use the preferred minifier, and legacy chunks
88 // can use terser.
89 if (
90 config.build.minify !== 'terser' &&
91 !this.environment.config.isOutputOptionsForLegacyChunks?.(outputOptions)
92 ) {
93 return null
94 }
95
96 // Lazy load worker.
97 worker ||= makeWorker()
98
99 const terserPath = pathToFileURL(loadTerserPath(config.root)).href
100 try {
101 const res = await worker.run(terserPath, code, {
102 safari10: true,
103 ...terserOptions,
104 format: {
105 ...terserOptions.format,
106 // For ES lib mode, preserve comments to keep pure annotations for tree-shaking
107 preserve_annotations:
108 config.build.lib && outputOptions.format === 'es'
109 ? true
110 : terserOptions.format?.preserve_annotations,
111 },
112 sourceMap: !!outputOptions.sourcemap,
113 module: outputOptions.format.startsWith('es'),
114 toplevel: outputOptions.format === 'cjs',
115 })
116 return {
117 code: res.code!,
118 map: res.map as any,
119 }
120 } catch (e) {
121 if (e.line !== undefined && e.col !== undefined) {
122 e.loc = {
123 file: chunk.fileName,
124 line: e.line,
125 column: e.col,
126 }
127 }
128 if (e.pos !== undefined) {
129 e.frame = generateCodeFrame(code, e.pos)
130 }
131 throw e
132 }
133 },
134
135 closeBundle() {
136 worker?.stop()

Callers

nothing calls this directly

Calls 3

generateCodeFrameFunction · 0.90
makeWorkerFunction · 0.85
loadTerserPathFunction · 0.85

Tested by

no test coverage detected