From 81eadf340f7e6dd797f70f6076a6e88b3ebb6a28 Mon Sep 17 00:00:00 2001 From: underfin <2218301630@qq.com> Date: Wed, 15 Nov 2023 16:33:32 +0800 Subject: [PATCH] feat: using rolldown --- packages/vite/LICENSE.md | 34 ++++++++++++++ packages/vite/package.json | 4 +- .../src/node/optimizer/esbuildDepPlugin.ts | 6 +-- packages/vite/src/node/optimizer/index.ts | 44 +++++++++-------- packages/vite/src/node/plugins/esbuild.ts | 2 +- pnpm-lock.yaml | 47 ++----------------- 6 files changed, 64 insertions(+), 73 deletions(-) diff --git a/packages/vite/LICENSE.md b/packages/vite/LICENSE.md index 7b45e464cf268f..14f150aac7eac7 100644 --- a/packages/vite/LICENSE.md +++ b/packages/vite/LICENSE.md @@ -1596,6 +1596,23 @@ Repository: gulpjs/glob-parent --------------------------------------- +## has-flag +License: MIT +By: Sindre Sorhus +Repository: sindresorhus/has-flag + +> MIT License +> +> Copyright (c) Sindre Sorhus (sindresorhus.com) +> +> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +> +> The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +> +> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +--------------------------------------- + ## http-proxy License: MIT By: Charlie Robbins, jcrugzz @@ -3106,6 +3123,23 @@ Repository: git+https://github.com/antfu/strip-literal.git --------------------------------------- +## supports-color +License: MIT +By: Sindre Sorhus +Repository: chalk/supports-color + +> MIT License +> +> Copyright (c) Sindre Sorhus (sindresorhus.com) +> +> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +> +> The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +> +> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +--------------------------------------- + ## to-regex-range License: MIT By: Jon Schlinkert, Rouven Weßling diff --git a/packages/vite/package.json b/packages/vite/package.json index 74852b4aee4a83..1bd21d87026885 100644 --- a/packages/vite/package.json +++ b/packages/vite/package.json @@ -76,7 +76,8 @@ "dependencies": { "esbuild": "^0.19.3", "postcss": "^8.4.31", - "rollup": "^4.2.0" + "rollup": "^4.2.0", + "@rolldown/node": "/Users/likui/Github/rolldown/packages/node" }, "optionalDependencies": { "fsevents": "~2.3.3" @@ -90,7 +91,6 @@ "@rollup/plugin-json": "^6.0.1", "@rollup/plugin-node-resolve": "15.2.3", "@rollup/plugin-typescript": "^11.1.5", - "@rollup/plugin-replace": "^2.4.2", "@rollup/pluginutils": "^5.0.5", "@types/escape-html": "^1.0.3", "@types/pnpapi": "^0.0.4", diff --git a/packages/vite/src/node/optimizer/esbuildDepPlugin.ts b/packages/vite/src/node/optimizer/esbuildDepPlugin.ts index f303d3f8f8c4d5..65d323eaa61a85 100644 --- a/packages/vite/src/node/optimizer/esbuildDepPlugin.ts +++ b/packages/vite/src/node/optimizer/esbuildDepPlugin.ts @@ -1,6 +1,6 @@ import path from 'node:path' import type { ImportKind } from 'esbuild' -import type { Plugin } from 'rollup' +import type { Plugin } from '@rolldown/node' import { KNOWN_ASSET_TYPES } from '../constants' import type { PackageCache } from '../packages' import { getDepOptimizationConfig } from '../config' @@ -149,7 +149,7 @@ export function rollupDepPlugin( }, resolveId: async function (id, importer, options) { // TODO rolldown: kind is not available in rollup - const kind: ImportKind = options.custom?.kind + const kind: ImportKind = options.kind // externalize assets and commonly known non-js file types // See #8459 for more details about this require-import conversion if (allExternalTypesReg.test(id)) { @@ -296,7 +296,7 @@ export function rollupCjsExternalPlugin( if (filter.test(id)) { // TODO rolldown: kind is not available in rollup - const kind: ImportKind = options.custom?.kind + const kind: ImportKind = options.kind if (kind === 'require-call' && platform !== 'node') { return { id: cjsExternalFacadeNamespace + id, diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index 0f4b82809d30d6..96e778ae0f04c4 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -3,9 +3,9 @@ import fsp from 'node:fs/promises' import path from 'node:path' import { promisify } from 'node:util' import { performance } from 'node:perf_hooks' -import type { RollupOutput } from 'rollup' +import type { RollupOptions, RollupOutput } from '@rolldown/node' import rollupPluginReplace from '@rollup/plugin-replace' -import rollup from 'rollup' +import * as rolldown from '@rolldown/node' import colors from 'picocolors' import type { BuildOptions as EsbuildBuildOptions } from 'esbuild' import { build } from 'esbuild' @@ -28,10 +28,8 @@ import { removeLeadingSlash, tryStatSync, } from '../utils' -import { transformWithEsbuild } from '../plugins/esbuild' +import { prettifyMessage, transformWithEsbuild } from '../plugins/esbuild' import { ESBUILD_MODULES_TARGET } from '../constants' -import { Rollup } from '..' -import { definePlugin } from '../plugins/define' import { rollupCjsExternalPlugin, rollupDepPlugin } from './esbuildDepPlugin' import { scanImports } from './scan' import { createOptimizeDepsIncludeResolver, expandGlobIds } from './resolve' @@ -115,7 +113,7 @@ export interface DepOptimizationConfig { | 'metafile' > - rollupOptions: rollup.RollupOptions + rollupOptions?: RollupOptions /** * List of file extensions that can be optimized. A corresponding esbuild * plugin must exist to handle the specific extension. @@ -626,17 +624,17 @@ export function runOptimizeDeps( for (const chunk of result.output) { if (chunk.type === 'chunk' && chunk.isEntry) { if (chunk.isEntry) { - const id = chunk.facadeModuleId! - - const { exportsData, ...info } = depsInfo[id] + const { exportsData, file, id, ...info } = Object.values( + depsInfo, + ).find((d) => d.src?.endsWith(chunk.facadeModuleId!))! addOptimizedDepInfo(metadata, 'optimized', { + id, + file, ...info, // We only need to hash the output.imports in to check for stability, but adding the hash // and file path gives us a unique hash that may be useful for other things in the future fileHash: getHash( - metadata.hash + - depsInfo[id].file + - JSON.stringify(chunk.imports), + metadata.hash + file + JSON.stringify(chunk.modules), ), browserHash: metadata.browserHash, // After bundling we have more information and can warn the user about legacy packages @@ -790,7 +788,6 @@ async function prepareRollupOptimizerRun( plugins.push(rollupCjsExternalPlugin(external, platform)) } plugins.push(rollupDepPlugin(flatIdDeps, external, config, ssr)) - plugins.push(rollupPluginReplace(define)) plugins.push({ name: 'optimizer-transform', async transform(code, id) { @@ -799,6 +796,7 @@ async function prepareRollupOptimizerRun( sourcemap: true, sourcefile: id, loader: jsxLoader && /\.js$/.test(id) ? 'jsx' : undefined, + define, target: isBuild ? config.build.target || undefined : ESBUILD_MODULES_TARGET, @@ -816,25 +814,25 @@ async function prepareRollupOptimizerRun( async function build() { // TODO platform - const bundle = await rollup.rollup({ + const bundle = await rolldown.rolldown({ input: Object.keys(flatIdDeps), external, - logLevel: 'warn', + // logLevel: 'warn', plugins, ...rollupOptions, }) return await bundle.write({ format: 'esm', - sourcemap: true, + // sourcemap: true, dir: processingCacheDir, // See https://github.com/evanw/esbuild/issues/1921#issuecomment-1152991694 - banner: - platform === 'node' - ? (chunk) => - chunk.fileName.endsWith('.js') - ? `import { createRequire } from 'module';const require = createRequire(import.meta.url);` - : '' - : undefined, + // banner: + // platform === 'node' + // ? (chunk) => + // chunk.fileName.endsWith('.js') + // ? `import { createRequire } from 'module';const require = createRequire(import.meta.url);` + // : '' + // : undefined, ...rollupOptions.output, }) } diff --git a/packages/vite/src/node/plugins/esbuild.ts b/packages/vite/src/node/plugins/esbuild.ts index ba7f338c8eec59..0e1554f89ac553 100644 --- a/packages/vite/src/node/plugins/esbuild.ts +++ b/packages/vite/src/node/plugins/esbuild.ts @@ -429,7 +429,7 @@ export function resolveEsbuildTranspileOptions( } } -function prettifyMessage(m: Message, code: string): string { +export function prettifyMessage(m: Message, code: string): string { let res = colors.yellow(m.text) if (m.location) { const lines = code.split(/\r?\n/g) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 38755e870ff38a..96951aa4508896 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -223,6 +223,9 @@ importers: packages/vite: dependencies: + '@rolldown/node': + specifier: /Users/likui/Github/rolldown/packages/node + version: link:../../../rolldown/packages/node esbuild: specifier: ^0.19.3 version: 0.19.3 @@ -258,9 +261,6 @@ importers: '@rollup/plugin-node-resolve': specifier: 15.2.3 version: 15.2.3(rollup@4.2.0) - '@rollup/plugin-replace': - specifier: ^2.4.2 - version: 2.4.2(rollup@4.2.0) '@rollup/plugin-typescript': specifier: ^11.1.5 version: 11.1.5(rollup@4.2.0)(tslib@2.6.2)(typescript@5.2.2) @@ -3552,16 +3552,6 @@ packages: rollup: 4.2.0 dev: true - /@rollup/plugin-replace@2.4.2(rollup@4.2.0): - resolution: {integrity: sha512-IGcu+cydlUMZ5En85jxHH4qj2hta/11BHq95iHEyb2sbgiN0eCdzvUcHw5gt9pBL5lTi4JDYJ1acCoMGpTvEZg==} - peerDependencies: - rollup: ^1.20.0 || ^2.0.0 - dependencies: - '@rollup/pluginutils': 3.1.0(rollup@4.2.0) - magic-string: 0.25.9 - rollup: 4.2.0 - dev: true - /@rollup/plugin-replace@5.0.2(rollup@3.29.2): resolution: {integrity: sha512-M9YXNekv/C/iHHK+cvORzfRYfPbq0RDD8r0G+bMiTXjNGKulPnCT9O3Ss46WfhI6ZOCgApOP7xAdmCQJ+U2LAA==} engines: {node: '>=14.0.0'} @@ -3596,18 +3586,6 @@ packages: typescript: 5.2.2 dev: true - /@rollup/pluginutils@3.1.0(rollup@4.2.0): - resolution: {integrity: sha512-GksZ6pr6TpIjHm8h9lSQ8pi8BE9VeubNT0OMJ3B5uZJ8pz73NPiqOtCog/x2/QzM1ENChPKxMDhiQuRHsqc+lg==} - engines: {node: '>= 8.0.0'} - peerDependencies: - rollup: ^1.20.0||^2.0.0 - dependencies: - '@types/estree': 0.0.39 - estree-walker: 1.0.1 - picomatch: 2.3.1 - rollup: 4.2.0 - dev: true - /@rollup/pluginutils@5.0.4(rollup@3.29.2): resolution: {integrity: sha512-0KJnIoRI8A+a1dqOYLxH8vBf8bphDmty5QvIm2hqm7oFCFYKCAZWWd2hXgMibaPsNDhI0AtpYfQZJG47pt/k4g==} engines: {node: '>=14.0.0'} @@ -3820,10 +3798,6 @@ packages: resolution: {integrity: sha512-QbNxKa2IX2y/9eGiy4w8rrwk//ERHXA6zwYVRA3+ayA/D3pkz+/bLL4b5uSLA0L0kPuNX1Jbv9HyPzv9T4zbJQ==} dev: true - /@types/estree@0.0.39: - resolution: {integrity: sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw==} - dev: true - /@types/estree@1.0.0: resolution: {integrity: sha512-WulqXMDUTYAXCjZnk6JtIHPigp55cVtDgDrO2gHRwhyJto21+1zbVCtOYB2L1F9w4qCQ0rOGWBnBe0FNTiEJIQ==} dev: true @@ -5893,10 +5867,6 @@ packages: engines: {node: '>=4.0'} dev: true - /estree-walker@1.0.1: - resolution: {integrity: sha512-1fMXF3YP4pZZVozF8j/ZLfvnR8NSIljt56UhbZ5PeeDmmGHpgpdwQt7ITlGvYaQukCvuBRMLEiKiYC+oeIg4cg==} - dev: true - /estree-walker@2.0.2: resolution: {integrity: sha512-Rfkk/Mp/DL7JVje3u18FxFujQlTNR2q6QfMSMB7AvCBx91NGj/ba3kCfza0f6dVDbw7YlRf/nDrn7pQrCCyQ/w==} @@ -7332,12 +7302,6 @@ packages: engines: {node: 14 || >=16.14} dev: true - /magic-string@0.25.9: - resolution: {integrity: sha512-RmF0AsMzgt25qzqqLc1+MbHmhdx0ojF2Fvs4XnOqz2ZOBXzzkEwc/dJQZCYHAn7v1jbVOjAZfK8msRn4BxO4VQ==} - dependencies: - sourcemap-codec: 1.4.8 - dev: true - /magic-string@0.27.0: resolution: {integrity: sha512-8UnnX2PeRAPZuN12svgR9j7M1uWMovg/CEnIwIG0LFkXSJJe4PdfUGiTGl8V9bsBHFUtfVINcSyYxd7q+kx9fA==} engines: {node: '>=12'} @@ -9065,11 +9029,6 @@ packages: engines: {node: '>= 8'} dev: true - /sourcemap-codec@1.4.8: - resolution: {integrity: sha512-9NykojV5Uih4lgo5So5dtw+f0JgJX30KCNI8gwhz2J9A15wD0Ml6tjHKwf6fTSa6fAdVBdZeNOs9eJ71qCk8vA==} - deprecated: Please use @jridgewell/sourcemap-codec instead - dev: true - /spdx-compare@1.0.0: resolution: {integrity: sha512-C1mDZOX0hnu0ep9dfmuoi03+eOdDoz2yvK79RxbcrVEG1NO1Ph35yW102DHWKN4pk80nwCgeMmSY5L25VE4D9A==} dependencies: