diff --git a/common/shared/preset-build/__tests__/config.spec.ts b/common/shared/preset-build/__tests__/config.spec.ts deleted file mode 100644 index 983f19175c..0000000000 --- a/common/shared/preset-build/__tests__/config.spec.ts +++ /dev/null @@ -1,35 +0,0 @@ -/** - * Copyright 2023-present DreamNum Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { describe, expect, it } from 'vitest'; -import { resolvePresetBuildOptions } from '../index'; - -describe('resolvePresetBuildOptions', () => { - it('reads UMD prepend inputs from preset config and keeps CLI options as appenders', () => { - expect(resolvePresetBuildOptions({ - umdAdditionalFiles: ['./polyfill.js'], - umdDeps: ['@univerjs/core', '@univerjs/ui'], - }, { - cleanup: true, - umdAdditionalFiles: ['./polyfill.js', './extra.js'], - umdDeps: ['@univerjs/ui', '@univerjs/docs'], - })).toEqual({ - cleanup: true, - umdAdditionalFiles: ['./polyfill.js', './extra.js'], - umdDeps: ['@univerjs/core', '@univerjs/ui', '@univerjs/docs'], - }); - }); -}); diff --git a/common/shared/preset-build/index.ts b/common/shared/preset-build/index.ts index b84100d82d..f225a9bd90 100644 --- a/common/shared/preset-build/index.ts +++ b/common/shared/preset-build/index.ts @@ -121,6 +121,16 @@ export function resolvePresetBuildOptions(presetBuildConfig: IPresetBuildConfig return resolvedOptions; } +export function createPresetModuleEntryGroups(entries: ReturnType) { + const primaryEntries = entries.filter((entry) => entry.type === 'index' || entry.type === 'locale'); + const isolatedEntries = entries.filter((entry) => entry.type !== 'index' && entry.type !== 'locale'); + + return [ + ...(primaryEntries.length > 0 ? [primaryEntries] : []), + ...isolatedEntries.map((entry) => [entry]), + ]; +} + export function removePresetOutputs(packageDir = process.cwd()) { for (const dir of CLEANUP_DIRECTORIES) { const targetDir = path.resolve(packageDir, dir); @@ -163,11 +173,11 @@ export async function buildPresetPackage(options: IPresetBuildOptions = {}) { const plugins = createInputPlugins(packageDir); const userConfig = await loadUserConfig(resolvedOptions, packageDir); const moduleFormats: TModuleFormat[] = ['esm', 'cjs']; - const moduleConfigs = moduleFormats.flatMap((format) => { - return getPresetModuleEntries(packageDir).map((entry) => createModuleConfig({ + const moduleConfigs = createPresetModuleEntryGroups(getPresetModuleEntries(packageDir)).flatMap((entries) => { + return moduleFormats.map((format) => createModuleConfig({ baseConfig, enableObfuscation: false, - entry, + entries, externalPackages, facadeExternalPackages: externalPackages, format, diff --git a/common/shared/tsdown/configs/module.ts b/common/shared/tsdown/configs/module.ts index d8d09291c1..c10c3bde0c 100644 --- a/common/shared/tsdown/configs/module.ts +++ b/common/shared/tsdown/configs/module.ts @@ -17,6 +17,7 @@ import type { UserConfig } from 'tsdown'; import type { IEntryConfig } from '../types'; import { defineConfig } from 'tsdown'; +import { createCssNoopInputOptions } from '../plugins/css-noop'; import { createOutputAliasPlugin } from '../plugins/output-alias'; import { createOutputObfuscatorPlugin } from '../plugins/output-obfuscator'; @@ -25,7 +26,7 @@ export type TModuleFormat = 'cjs' | 'esm'; export interface ICreateModuleConfigOptions { baseConfig: Partial; enableObfuscation: boolean; - entry: IEntryConfig; + entries: IEntryConfig[]; externalPackages: string[]; facadeExternalPackages: string[]; format: TModuleFormat; @@ -39,10 +40,12 @@ export interface ICreateModuleConfigOptions { * Creates the common ESM/CJS bundle config for a single package entry. */ export function createModuleConfig(options: ICreateModuleConfigOptions): UserConfig { - const { baseConfig, enableObfuscation, entry, externalPackages, facadeExternalPackages, format, obfuscatorIgnorePatterns, outDir, packageDir, plugins } = options; - const neverBundle = entry.type === 'facade' ? facadeExternalPackages : externalPackages; + const { baseConfig, enableObfuscation, entries, externalPackages, facadeExternalPackages, format, obfuscatorIgnorePatterns, outDir, packageDir, plugins } = options; + const hasFacadeEntry = entries.some((entry) => entry.type === 'facade'); + const hasIndexEntry = entries.some((entry) => entry.type === 'index'); + const neverBundle = hasFacadeEntry ? facadeExternalPackages : externalPackages; const copyToRoot = format === 'esm'; - const keepRootIndexCss = entry.type === 'index' && format === 'esm'; + const keepRootIndexCss = hasIndexEntry && format === 'esm'; return defineConfig({ ...baseConfig, @@ -50,8 +53,9 @@ export function createModuleConfig(options: ICreateModuleConfigOptions): UserCon neverBundle, }, dts: false, - entry: { [entry.key]: entry.path }, + entry: Object.fromEntries(entries.map((entry) => [entry.key, entry.path])), format, + inputOptions: keepRootIndexCss ? baseConfig.inputOptions : createCssNoopInputOptions(baseConfig.inputOptions), outputOptions: { codeSplitting: true, minify: enableObfuscation, diff --git a/common/shared/tsdown/configs/umd.ts b/common/shared/tsdown/configs/umd.ts index f678762785..30b689ec1a 100644 --- a/common/shared/tsdown/configs/umd.ts +++ b/common/shared/tsdown/configs/umd.ts @@ -18,6 +18,7 @@ import type { UserConfig } from 'tsdown'; import type { IEntryConfig } from '../types'; import { defineConfig } from 'tsdown'; import { peerDepsMap } from '../data/peer-deps'; +import { createCssNoopInputOptions } from '../plugins/css-noop'; import { createOutputAliasPlugin } from '../plugins/output-alias'; import { createOutputObfuscatorPlugin } from '../plugins/output-obfuscator'; @@ -126,6 +127,7 @@ export function createUmdConfig(options: ICreateUmdConfigOptions): UserConfig { entry: { [entry.key]: entry.path }, format: 'umd', globalName: getGlobalName(packageName, entry.key), + inputOptions: createCssNoopInputOptions(baseConfig.inputOptions), outDir, outputOptions: { entryFileNames: '[name].js', diff --git a/common/shared/tsdown/index.ts b/common/shared/tsdown/index.ts index 5d52f77225..7f1ad9a692 100644 --- a/common/shared/tsdown/index.ts +++ b/common/shared/tsdown/index.ts @@ -51,16 +51,26 @@ function createBuildContext(packageDir: string, options: IBuildOptions): IBuildC /** * Expands the package context into all required tsdown configs. */ -function createConfigs(context: IBuildContext, options: IBuildOptions) { +function createModuleEntryGroups(entries: IBuildContext['entries']) { + const primaryEntries = entries.filter((entry) => entry.type === 'index' || entry.type === 'locale'); + const isolatedEntries = entries.filter((entry) => entry.type !== 'index' && entry.type !== 'locale'); + + return [ + ...(primaryEntries.length > 0 ? [primaryEntries] : []), + ...isolatedEntries.map((entry) => [entry]), + ]; +} + +export function createConfigs(context: IBuildContext, options: IBuildOptions) { const baseConfig = createBaseConfig(context); const moduleFormats: TModuleFormat[] = ['esm', 'cjs']; const enableObfuscation = context.packageJson.name.startsWith('@univerjs-pro/'); - const moduleConfigs = context.entries.flatMap((entry) => { + const moduleConfigs = createModuleEntryGroups(context.entries).flatMap((entries) => { return moduleFormats.map((format) => createModuleConfig({ baseConfig, enableObfuscation, - entry, + entries, externalPackages: context.externalPackages, facadeExternalPackages: context.facadeExternalPackages, format, diff --git a/common/shared/tsdown/plugins/class-name-whitespace-cleanup.spec.ts b/common/shared/tsdown/plugins/class-name-whitespace-cleanup.spec.ts index 86545c72c6..08cded1ec2 100644 --- a/common/shared/tsdown/plugins/class-name-whitespace-cleanup.spec.ts +++ b/common/shared/tsdown/plugins/class-name-whitespace-cleanup.spec.ts @@ -35,6 +35,25 @@ describe('cleanupClassNameTemplateWhitespace', () => { ); }); + it('should normalize whitespace for conditional clsx template arguments', () => { + const sourceCode = ` + const value = clsx( + "univer-relative univer-transition-all univer-duration-150", + isDraggingItem && "univer-opacity-0", + dragOverId === itemId && !isDraggingItem && \` + univer-bg-primary-50/60 + dark:!univer-bg-primary-900/20 + univer-rounded univer-border univer-border-primary-200 + dark:!univer-border-primary-700 + \` + ); + `; + + expect(cleanupClassNameTemplateWhitespace(sourceCode, '/tmp/example.tsx')).toContain( + 'dragOverId === itemId && !isDraggingItem && "univer-bg-primary-50/60 dark:!univer-bg-primary-900/20 univer-rounded univer-border univer-border-primary-200 dark:!univer-border-primary-700"' + ); + }); + it('should normalize whitespace for className template literals', () => { const sourceCode = ` const value = ( @@ -52,6 +71,76 @@ describe('cleanupClassNameTemplateWhitespace', () => { ); }); + it('should normalize whitespace for className string literals', () => { + const sourceCode = ` + const value = ( +