Skip to content

Commit

Permalink
build: fix build scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
jikkai committed Nov 23, 2024
1 parent 09181f6 commit 39135a0
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 168 deletions.
6 changes: 6 additions & 0 deletions common/shared/prepare/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ function convertImportNameFromPackageName(name: string) {
.replace(/-/g, '');
}

function cleanupLibDir() {
fs.removeSync(path.resolve(__dirname, 'lib'));
}

export function createLocalesFiles() {
cleanupLibDir();
fs.ensureDirSync(path.resolve(__dirname, 'src/locales'));

LOCLAES_MAP.forEach((localeKey) => {
Expand Down Expand Up @@ -55,6 +60,7 @@ export function createLocalesFiles() {
}

export function createPresetsFiles() {
cleanupLibDir();
Object.keys(pkg.dependencies).forEach((key) => {
if (key.startsWith('@univerjs/preset')) {
const indexTs = `export * from '${key}';\n`;
Expand Down
15 changes: 8 additions & 7 deletions common/shared/vite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import dts from 'vite-plugin-dts';
import vitePluginExternal from 'vite-plugin-external';

import { autoDetectedExternalPlugin } from './auto-detected-external-plugin';
import { prependUMDRawPlugin } from './prepend-umd-raw-plugin';
import prependUMDRaw from './prepend-umd-raw';

import { convertLibNameFromPackageName } from './utils';

Expand Down Expand Up @@ -80,7 +80,7 @@ async function buildCJS(sharedConfig: InlineConfig, options: IBuildExecuterOptio
async function buildUMD(sharedConfig: InlineConfig, options: IBuildExecuterOptions) {
const { pkg, entry, umdDeps } = options;

return await Promise.all(Object.keys(entry).map((key) => {
await Promise.all(Object.keys(entry).map((key) => {
let name = convertLibNameFromPackageName(pkg.name);

if (key.includes('locales')) {
Expand All @@ -101,15 +101,16 @@ async function buildUMD(sharedConfig: InlineConfig, options: IBuildExecuterOptio
formats: ['umd'],
},
},
plugins: [
prependUMDRawPlugin({
umdDeps,
}),
],
});

return viteBuild(config);
}));

prependUMDRaw({
umdDeps,
});

return Promise.resolve();
}

interface IBuildOptions {
Expand Down
76 changes: 0 additions & 76 deletions common/shared/vite/prepend-umd-raw-plugin.ts

This file was deleted.

89 changes: 89 additions & 0 deletions common/shared/vite/prepend-umd-raw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import path from 'node:path';
import process from 'node:process';
import fs from 'fs-extra';

const LOCLAES_MAP = [
'en-US',
'fa-IR',
'ru-RU',
'vi-VN',
'zh-CN',
'zh-TW',
];

interface IOptions {
umdDeps: string[];
}

export default function prependUMDRaw(options: IOptions) {
const { umdDeps } = options;

const __nodeModules = path.resolve(process.cwd(), 'node_modules');
const __umd = path.resolve(process.cwd(), 'lib/umd/index.js');

const umdContentsMap: Map<string, string> = new Map();

umdDeps.forEach((dep) => {
const __dep = path.resolve(__nodeModules, dep);
const __depIndex = path.resolve(__dep, 'lib/umd/index.js');
const __depFacade = path.resolve(__dep, 'lib/umd/facade.js');

const key = `${dep}/index`;
const content = `// ${key}\n${fs.readFileSync(__depIndex, 'utf8')}`;
if (!umdContentsMap.has(key)) {
umdContentsMap.set(key, content);
}

if (fs.existsSync(__depFacade)) {
const key = `${dep}/facade`;
const content = `// ${key}\n${fs.readFileSync(__depFacade, 'utf8')}`;
if (!umdContentsMap.has(key)) {
umdContentsMap.set(key, content);
}
}
});

if (fs.existsSync(__umd)) {
const key = 'index';
const content = `// ${key}\n${fs.readFileSync(__umd, 'utf8')}`;
if (!umdContentsMap.has(key)) {
umdContentsMap.set(key, content);
}
}

const umdContents = Array.from(umdContentsMap.values()).join('\n\n');
fs.writeFileSync(__umd, umdContents);

const __localeDir = path.resolve(process.cwd(), 'lib/umd/locales');

LOCLAES_MAP.forEach((localeKey) => {
const localeContentsMap: Map<string, string> = new Map();

umdDeps.forEach((dep) => {
const __dep = path.resolve(__nodeModules, dep);
const __depLocale = path.resolve(__dep, 'lib/umd/locale', `${localeKey}.js`);

if (fs.existsSync(__depLocale)) {
const key = `${dep}/locale/${localeKey}`;
const content = `// ${key}\n${fs.readFileSync(__depLocale, 'utf8')}`;
if (!localeContentsMap.has(key)) {
localeContentsMap.set(key, content);
}
}
});

const __locale = path.resolve(__localeDir, `${localeKey}.js`);

if (fs.existsSync(__locale)) {
const key = `locale/${localeKey}`;
const content = `// ${key}\n${fs.readFileSync(__locale, 'utf8')}`;

if (!localeContentsMap.has(key)) {
localeContentsMap.set(key, content);
}

const localeContents = Array.from(localeContentsMap.values()).join('\n\n');
fs.writeFileSync(__locale, localeContents);
}
});
}
85 changes: 0 additions & 85 deletions scripts/build-locales.mjs

This file was deleted.

0 comments on commit 39135a0

Please sign in to comment.