Some checks failed
📦 Build / build (push) Has been cancelled
🧪 CI Test / test shard (1/4) (push) Has been cancelled
🧪 CI Test / test shard (2/4) (push) Has been cancelled
🧪 CI Test / test shard (3/4) (push) Has been cancelled
🧪 CI Test / test shard (4/4) (push) Has been cancelled
🧪 CI Test / test (push) Has been cancelled
🧪 CI Test / eslint (push) Has been cancelled
🧪 CI Test / typecheck (push) Has been cancelled
🔫 Sync to Pro / dispatch-sync (push) Has been cancelled
🔮 Sync Mirror / sync (push) Has been cancelled
96 lines
2.9 KiB
TypeScript
96 lines
2.9 KiB
TypeScript
import type { UserConfig } from 'tsdown';
|
|
|
|
const CSS_NOOP_PLUGIN_NAME = 'univer-css-noop';
|
|
const CSS_IMPORT_RE = /\.(?:css|less|sass|scss|styl|stylus)(?:$|\?)/;
|
|
const CSS_NOOP_ID_PREFIX = '\0univer-css-noop:';
|
|
const CSS_NOOP_ID_RE = /^\0univer-css-noop:/;
|
|
|
|
type TInputOptions = NonNullable<UserConfig['inputOptions']>;
|
|
type TInputOptionsFunction = Extract<TInputOptions, (...args: any[]) => any>;
|
|
type TInputOptionsObject = Exclude<TInputOptions, (...args: any[]) => any>;
|
|
|
|
function toPluginArray(plugins: unknown): any[] {
|
|
return Array.isArray(plugins)
|
|
? plugins
|
|
: plugins
|
|
? [plugins]
|
|
: [];
|
|
}
|
|
|
|
function withoutCssNoopPlugin(plugins: any[]) {
|
|
return plugins.filter((plugin) => !plugin || plugin.name !== CSS_NOOP_PLUGIN_NAME);
|
|
}
|
|
|
|
export function createCssNoopPlugin() {
|
|
let nextId = 0;
|
|
|
|
return {
|
|
name: CSS_NOOP_PLUGIN_NAME,
|
|
resolveId: {
|
|
filter: { id: CSS_IMPORT_RE },
|
|
handler() {
|
|
return `${CSS_NOOP_ID_PREFIX}${nextId++}`;
|
|
},
|
|
},
|
|
load: {
|
|
filter: { id: CSS_NOOP_ID_RE },
|
|
handler() {
|
|
return {
|
|
code: 'export default {};',
|
|
moduleSideEffects: false,
|
|
moduleType: 'js',
|
|
};
|
|
},
|
|
},
|
|
};
|
|
}
|
|
|
|
function prependCssNoopPlugin(plugins: any[]) {
|
|
return [
|
|
createCssNoopPlugin(),
|
|
...withoutCssNoopPlugin(plugins),
|
|
];
|
|
}
|
|
|
|
export function createCssNoopInputOptions(inputOptions?: TInputOptions): TInputOptionsFunction {
|
|
return (async (defaultOptions: { plugins?: unknown }, ...args: any[]) => {
|
|
const defaultPlugins = toPluginArray(defaultOptions.plugins);
|
|
|
|
if (!inputOptions) {
|
|
return {
|
|
...defaultOptions,
|
|
plugins: prependCssNoopPlugin(defaultPlugins),
|
|
};
|
|
}
|
|
|
|
if (typeof inputOptions === 'function') {
|
|
const defaultOptionsWithCssNoop = {
|
|
...defaultOptions,
|
|
plugins: prependCssNoopPlugin(defaultPlugins),
|
|
};
|
|
const resolvedOptions = await (inputOptions as any)(defaultOptionsWithCssNoop, ...args);
|
|
|
|
if (!resolvedOptions) {
|
|
return defaultOptionsWithCssNoop;
|
|
}
|
|
|
|
return {
|
|
...defaultOptionsWithCssNoop,
|
|
...resolvedOptions,
|
|
plugins: prependCssNoopPlugin(toPluginArray(resolvedOptions.plugins ?? defaultOptionsWithCssNoop.plugins)),
|
|
};
|
|
}
|
|
|
|
const inputOptionsObject = inputOptions as TInputOptionsObject & { plugins?: unknown };
|
|
|
|
return {
|
|
...defaultOptions,
|
|
...inputOptionsObject,
|
|
plugins: prependCssNoopPlugin([
|
|
...toPluginArray(inputOptionsObject.plugins),
|
|
...defaultPlugins,
|
|
]),
|
|
};
|
|
}) as TInputOptionsFunction;
|
|
}
|