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

Function prepareRolldownOptimizerRun

packages/vite/src/node/optimizer/index.ts:770–885  ·  view source on GitHub ↗
(
  environment: Environment,
  depsInfo: Record<string, OptimizedDepInfo>,
  processingCacheDir: string,
  optimizerContext: { cancelled: boolean },
)

Source from the content-addressed store, hash-verified

768}
769
770async function prepareRolldownOptimizerRun(
771 environment: Environment,
772 depsInfo: Record<string, OptimizedDepInfo>,
773 processingCacheDir: string,
774 optimizerContext: { cancelled: boolean },
775): Promise<{
776 context?: { build: () => Promise<RolldownOutput>; cancel: () => void }
777 idToExports: Record<string, ExportsData>
778}> {
779 // esbuild generates nested directory output with lowest common ancestor base
780 // this is unpredictable and makes it difficult to analyze entry / output
781 // mapping. So what we do here is:
782 // 1. flatten all ids to eliminate slash
783 // 2. in the plugin, read the entry ourselves as virtual files to retain the
784 // path.
785 const flatIdDeps: Record<string, string> = {}
786 const idToExports: Record<string, ExportsData> = {}
787
788 const { optimizeDeps } = environment.config
789
790 const { plugins: pluginsFromConfig = [], ...rolldownOptions } =
791 optimizeDeps.rolldownOptions ?? {}
792
793 let jsxLoader = false
794 await Promise.all(
795 Object.keys(depsInfo).map(async (id) => {
796 const src = depsInfo[id].src!
797 const exportsData = await (depsInfo[id].exportsData ??
798 extractExportsData(environment, src))
799 if (exportsData.jsxLoader) {
800 // Ensure that optimization won't fail by defaulting '.js' to the JSX parser.
801 // This is useful for packages such as Gatsby.
802 jsxLoader = true
803 }
804 const flatId = flattenId(id)
805 flatIdDeps[flatId] = isWindows ? src.replaceAll('/', '\\') : src
806 idToExports[id] = exportsData
807 }),
808 )
809
810 if (optimizerContext.cancelled) return { context: undefined, idToExports }
811
812 const define = {
813 'process.env.NODE_ENV': environment.config.keepProcessEnv
814 ? // define process.env.NODE_ENV even for keepProcessEnv === true
815 // as esbuild will replace it automatically when `platform` is `'browser'`
816 'process.env.NODE_ENV'
817 : JSON.stringify(process.env.NODE_ENV || environment.config.mode),
818 ...rolldownOptions.transform?.define,
819 }
820
821 const platform =
822 optimizeDeps.rolldownOptions?.platform ??
823 // We generally don't want to use platform 'neutral', as esbuild has custom handling
824 // when the platform is 'node' or 'browser' that can't be emulated by using mainFields
825 // and conditions
826 (environment.config.consumer === 'client' ||
827 environment.config.ssr.target === 'webworker'

Callers 1

runOptimizeDepsFunction · 0.85

Calls 6

flattenIdFunction · 0.90
asyncFlattenFunction · 0.90
arraifyFunction · 0.90
rolldownDepPluginFunction · 0.90
extractExportsDataFunction · 0.85

Tested by

no test coverage detected