Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: using rollupOptions instead of esbuildOptions #13

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -776,10 +776,8 @@ export async function resolveConfig(
optimizeDeps: {
disabled: 'build',
...optimizeDeps,
esbuildOptions: {
preserveSymlinks: resolveOptions.preserveSymlinks,
...optimizeDeps.esbuildOptions,
},
rollupOptions: optimizeDeps.rollupOptions,
// TODO preserveSymlinks: resolveOptions.preserveSymlinks,
},
worker: resolvedWorkerOptions,
appType: config.appType ?? 'spa',
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/optimizer/esbuildDepPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const externalTypes = [
...KNOWN_ASSET_TYPES,
]

export function rollupDepPlugin(
export function rolldownDepPlugin(
qualified: Record<string, string>,
external: string[],
config: ResolvedConfig,
Expand Down Expand Up @@ -278,7 +278,7 @@ const matchesEntireLine = (text: string) => `^${escapeRegex(text)}$`

// esbuild doesn't transpile `require('foo')` into `import` statements if 'foo' is externalized
// https://github.com/evanw/esbuild/issues/566#issuecomment-735551834
export function rollupCjsExternalPlugin(
export function rolldownCjsExternalPlugin(
externals: string[],
platform: 'node' | 'browser',
): Plugin {
Expand Down
37 changes: 22 additions & 15 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { performance } from 'node:perf_hooks'
import type { RollupOptions, RollupOutput } from '@rolldown/node'
import * as rolldown from '@rolldown/node'
import colors from 'picocolors'
import type { BuildOptions as EsbuildBuildOptions } from 'esbuild'
import type { BuildOptions as EsbuildBuildOptions, Loader } from 'esbuild'
import { build } from 'esbuild'
import { init, parse } from 'es-module-lexer'
import glob from 'fast-glob'
Expand All @@ -29,7 +29,10 @@ import {
} from '../utils'
import { prettifyMessage, transformWithEsbuild } from '../plugins/esbuild'
import { ESBUILD_MODULES_TARGET } from '../constants'
import { rollupCjsExternalPlugin, rollupDepPlugin } from './esbuildDepPlugin'
import {
rolldownCjsExternalPlugin,
rolldownDepPlugin,
} from './esbuildDepPlugin'
import { scanImports } from './scan'
import { createOptimizeDepsIncludeResolver, expandGlobIds } from './resolve'
export {
Expand Down Expand Up @@ -617,7 +620,10 @@ export function runOptimizeDeps(
if (chunk.isEntry) {
const { exportsData, file, id, ...info } = Object.values(
depsInfo,
).find((d) => d.src?.endsWith(chunk.facadeModuleId!))!
).find(
(d) =>
d.src === path.join(process.cwd(), chunk.facadeModuleId!),
)!
addOptimizedDepInfo(metadata, 'optimized', {
id,
file,
Expand Down Expand Up @@ -771,9 +777,9 @@ async function prepareRollupOptimizerRun(
Array.isArray(pluginsFromConfig) ? pluginsFromConfig : [pluginsFromConfig],
)
if (external.length) {
plugins.push(rollupCjsExternalPlugin(external, platform))
plugins.push(rolldownCjsExternalPlugin(external, platform))
}
plugins.push(rollupDepPlugin(flatIdDeps, external, config, ssr))
plugins.push(rolldownDepPlugin(flatIdDeps, external, config, ssr))
plugins.push({
name: 'optimizer-transform',
async transform(code, id) {
Expand Down Expand Up @@ -1103,18 +1109,19 @@ export async function extractExportsData(

const optimizeDeps = getDepOptimizationConfig(config, ssr)

const esbuildOptions = optimizeDeps?.esbuildOptions ?? {}
const rollupOptions = optimizeDeps?.rollupOptions ?? {}
if (optimizeDeps.extensions?.some((ext) => filePath.endsWith(ext))) {
// For custom supported extensions, build the entry file to transform it into JS,
// and then parse with es-module-lexer. Note that the `bundle` option is not `true`,
// so only the entry file is being transformed.
const result = await build({
...esbuildOptions,
entryPoints: [filePath],
write: false,
const build = await rolldown.rolldown({
...rollupOptions,
input: [filePath],
})
const result = await build.generate({
format: 'esm',
})
const [imports, exports] = parse(result.outputFiles[0].text)
const [imports, exports] = parse(result.output[0].code)
return {
hasImports: imports.length > 0,
exports: exports.map((e) => e.n),
Expand All @@ -1128,7 +1135,7 @@ export async function extractExportsData(
try {
parseResult = parse(entryContent)
} catch {
const loader = esbuildOptions.loader?.[path.extname(filePath)] || 'jsx'
const loader = (path.extname(filePath).slice(1) as Loader) || 'jsx'
debug?.(
`Unable to parse: ${filePath}.\n Trying again with a ${loader} transform.`,
)
Expand Down Expand Up @@ -1226,9 +1233,9 @@ export function getDepHash(config: ResolvedConfig, ssr: boolean): string {
optimizeDeps: {
include: optimizeDeps?.include,
exclude: optimizeDeps?.exclude,
esbuildOptions: {
...optimizeDeps?.esbuildOptions,
plugins: optimizeDeps?.esbuildOptions?.plugins?.map((p) => p.name),
rollupOptions: {
...optimizeDeps?.rollupOptions,
plugins: optimizeDeps?.rollupOptions?.plugins?.map((p) => p.name),
},
},
},
Expand Down
6 changes: 2 additions & 4 deletions packages/vite/src/node/optimizer/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ async function prepareRolldownScanner(
plugins.push(rolldownScanPlugin(config, container, deps, missing, entries))

async function build() {
await rolldown.rolldown({
await rolldown.experimental_scan({
input: entries,
// logLevel: 'silent',
plugins,
Expand Down Expand Up @@ -561,9 +561,7 @@ function rolldownScanPlugin(
contents = config.esbuild.jsxInject + `\n` + contents
}

const loader =
config.optimizeDeps?.esbuildOptions?.loader?.[`.${ext}`] ||
(ext as Loader)
const loader = ext as Loader

if (loader !== 'js') {
contents = (await transform(contents, { loader })).code
Expand Down
6 changes: 2 additions & 4 deletions packages/vite/src/node/ssr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,8 @@ export function resolveSSROptions(
optimizeDeps: {
disabled: true,
...optimizeDeps,
esbuildOptions: {
preserveSymlinks,
...optimizeDeps.esbuildOptions,
},
// TODO preserveSymlinks
rollupOptions: optimizeDeps.rollupOptions,
},
}
}
27 changes: 12 additions & 15 deletions playground/optimize-deps/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,15 @@ export default defineConfig({
'@vitejs/test-dep-optimize-with-glob/**/*.js',
],
exclude: ['@vitejs/test-nested-exclude', '@vitejs/test-dep-non-optimized'],
esbuildOptions: {
rollupOptions: {
plugins: [
{
name: 'replace-a-file',
setup(build) {
build.onLoad(
{ filter: /dep-esbuild-plugin-transform(\\|\/)index\.js$/ },
() => ({
contents: `export const hello = () => 'Hello from an esbuild plugin'`,
loader: 'js',
}),
)
load: (id) => {
// eslint-disable-next-line regexp/no-unused-capturing-group
if (/dep-esbuild-plugin-transform(\\|\/)index\.js$/.test(id)) {
return `export const hello = () => 'Hello from an esbuild plugin'`
}
},
},
],
Expand Down Expand Up @@ -142,18 +139,18 @@ function notjs() {
return {
optimizeDeps: {
extensions: ['.notjs'],
esbuildOptions: {
rollupOptions: {
plugins: [
{
name: 'esbuild-notjs',
setup(build) {
build.onLoad({ filter: /\.notjs$/ }, ({ path }) => {
let contents = fs.readFileSync(path, 'utf-8')
load: (id) => {
if (id.endsWith('.notjs')) {
let contents = fs.readFileSync(id, 'utf-8')
contents = contents
.replace('<notjs>', '')
.replace('</notjs>', '')
return { contents, loader: 'js' }
})
return contents
}
},
},
],
Expand Down
Loading