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
59 lines
1.8 KiB
TypeScript
59 lines
1.8 KiB
TypeScript
import { cpSync, existsSync, mkdirSync, unlinkSync } from 'node:fs';
|
|
import path from 'node:path';
|
|
|
|
interface IBundleOutput {
|
|
fileName?: string;
|
|
}
|
|
|
|
export interface ICreateOutputAliasPluginOptions {
|
|
copyToRoot: boolean;
|
|
keepRootIndexCss: boolean;
|
|
packageDir: string;
|
|
}
|
|
|
|
export function createOutputAliasPlugin(options: ICreateOutputAliasPluginOptions) {
|
|
const { copyToRoot, keepRootIndexCss, packageDir } = options;
|
|
|
|
return {
|
|
name: 'create-output-alias',
|
|
writeBundle(outputOptions: { dir?: string }, bundle: Record<string, IBundleOutput>) {
|
|
if (!outputOptions.dir) {
|
|
return;
|
|
}
|
|
|
|
const outDir = path.resolve(packageDir, outputOptions.dir);
|
|
const rootDir = path.resolve(outDir, '..');
|
|
|
|
for (const output of Object.values(bundle)) {
|
|
if (!output.fileName || output.fileName.endsWith('.map')) {
|
|
continue;
|
|
}
|
|
|
|
const sourcePath = path.join(outDir, output.fileName);
|
|
|
|
if (output.fileName.endsWith('.css')) {
|
|
if (keepRootIndexCss) {
|
|
const rootCssPath = path.join(rootDir, 'index.css');
|
|
mkdirSync(path.dirname(rootCssPath), { recursive: true });
|
|
cpSync(sourcePath, rootCssPath);
|
|
}
|
|
|
|
if (existsSync(sourcePath)) {
|
|
unlinkSync(sourcePath);
|
|
}
|
|
|
|
continue;
|
|
}
|
|
|
|
if (!copyToRoot) {
|
|
continue;
|
|
}
|
|
|
|
const targetPath = path.join(rootDir, output.fileName);
|
|
mkdirSync(path.dirname(targetPath), { recursive: true });
|
|
cpSync(sourcePath, targetPath);
|
|
}
|
|
},
|
|
};
|
|
}
|