From 39135a0b50f95481c5cf07370dd9d90f4bde9259 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E7=86=B1?= Date: Sat, 23 Nov 2024 17:16:55 +0800 Subject: [PATCH] build: fix build scripts --- common/shared/prepare/index.ts | 6 ++ common/shared/vite/index.ts | 15 ++-- common/shared/vite/prepend-umd-raw-plugin.ts | 76 ----------------- common/shared/vite/prepend-umd-raw.ts | 89 ++++++++++++++++++++ scripts/build-locales.mjs | 85 ------------------- 5 files changed, 103 insertions(+), 168 deletions(-) delete mode 100644 common/shared/vite/prepend-umd-raw-plugin.ts create mode 100644 common/shared/vite/prepend-umd-raw.ts delete mode 100644 scripts/build-locales.mjs diff --git a/common/shared/prepare/index.ts b/common/shared/prepare/index.ts index ea1285d..f974fbc 100644 --- a/common/shared/prepare/index.ts +++ b/common/shared/prepare/index.ts @@ -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) => { @@ -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`; diff --git a/common/shared/vite/index.ts b/common/shared/vite/index.ts index 0cc33b5..8532345 100644 --- a/common/shared/vite/index.ts +++ b/common/shared/vite/index.ts @@ -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'; @@ -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')) { @@ -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 { diff --git a/common/shared/vite/prepend-umd-raw-plugin.ts b/common/shared/vite/prepend-umd-raw-plugin.ts deleted file mode 100644 index 39f2d26..0000000 --- a/common/shared/vite/prepend-umd-raw-plugin.ts +++ /dev/null @@ -1,76 +0,0 @@ -import type { Plugin } from 'vite'; -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 function prependUMDRawPlugin(options: IOptions): Plugin { - const { umdDeps } = options; - - return { - name: 'prepend-umd-raw-plugin', - - closeBundle() { - const __nodeModules = path.resolve(process.cwd(), 'node_modules'); - const __umd = path.resolve(process.cwd(), 'lib/umd/index.js'); - - const umdContents: string[] = []; - - 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'); - - umdContents.push(`// ${__dep}`); - umdContents.push(fs.readFileSync(__depIndex, 'utf8')); - - if (fs.existsSync(__depFacade)) { - umdContents.push(`// ${__dep}/facade`); - umdContents.push(fs.readFileSync(__depFacade, 'utf8')); - } - }); - - if (fs.existsSync(__umd)) { - umdContents.push(fs.readFileSync(__umd, 'utf8')); - } - - fs.writeFileSync(__umd, umdContents.join('\n\n')); - - const __localeDir = path.resolve(process.cwd(), 'lib/umd/locales'); - - LOCLAES_MAP.forEach((localeKey) => { - const localeContents: string[] = []; - - umdDeps.forEach((dep) => { - const __dep = path.resolve(__nodeModules, dep); - const __depLocale = path.resolve(__dep, 'lib/umd/locale', `${localeKey}.js`); - - if (fs.existsSync(__depLocale)) { - umdContents.push(`// ${__dep}/locale/${localeKey}`); - localeContents.push(fs.readFileSync(__depLocale, 'utf8')); - } - }); - - const __locale = path.resolve(__localeDir, `${localeKey}.js`); - - if (fs.existsSync(__locale)) { - localeContents.push(fs.readFileSync(__locale, 'utf8')); - - fs.writeFileSync(__locale, localeContents.join('\n\n')); - } - }); - }, - }; -} diff --git a/common/shared/vite/prepend-umd-raw.ts b/common/shared/vite/prepend-umd-raw.ts new file mode 100644 index 0000000..70833bc --- /dev/null +++ b/common/shared/vite/prepend-umd-raw.ts @@ -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 = 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 = 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); + } + }); +} diff --git a/scripts/build-locales.mjs b/scripts/build-locales.mjs deleted file mode 100644 index 6571c4d..0000000 --- a/scripts/build-locales.mjs +++ /dev/null @@ -1,85 +0,0 @@ -import path from 'node:path'; -import { fileURLToPath } from 'node:url'; -import fs from 'fs-extra'; - -const __dirname = path.dirname(fileURLToPath(import.meta.url)); - -const __packagesDir = path.resolve(__dirname, '../packages'); - -const pkgs = fs.readdirSync(__packagesDir).filter(pkg => pkg.startsWith('preset')); - -const __mainPkg = path.resolve(__dirname, '../packages/presets'); -const __mainPkgJSON = path.resolve(__mainPkg, './package.json'); - -const mainPkgJSON = fs.readJSONSync(__mainPkgJSON, 'utf8'); - -const LOCLAES_MAP = [ - 'en-US', - 'fa-IR', - 'ru-RU', - 'vi-VN', - 'zh-CN', - 'zh-TW', -]; - -pkgs.forEach((pkg) => { - if (!pkg.startsWith('preset-')) - return; - - // modify package.json - mainPkgJSON.exports[`./${pkg}`] = `./src/${pkg}/index.ts`; - mainPkgJSON.exports[`./${pkg}/*`] = `./src/${pkg}/*.ts`; - mainPkgJSON.dependencies[`@univerjs/${pkg}`] = 'workspace:*'; - - fs.ensureDirSync(path.resolve(__mainPkg, `./src/${pkg}`)); - - const __pkg = path.resolve(__packagesDir, pkg); - const pkgJson = fs.readJSONSync(path.resolve(__pkg, 'package.json')); - - // create presets/preset-[pkg]/index.ts - fs.writeFileSync(path.resolve(__mainPkg, `./src/${pkg}/index.ts`), `export * from '@univerjs/${pkg}';\n`); - - // create presets/preset-[pkg]/web-worker.ts - if (pkgJson.exports['./web-worker']) { - fs.writeFileSync(path.resolve(__mainPkg, `./src/${pkg}/web-worker.ts`), `export * from '@univerjs/${pkg}/web-worker';\n`); - } - // create presets/preset-[pkg]/locales/[lang].ts - if (pkgJson.exports['./locales/*']) { - fs.ensureDirSync(path.resolve(__mainPkg, `./src/${pkg}/locales`)); - LOCLAES_MAP.forEach((locale) => { - fs.writeFileSync(path.resolve(__mainPkg, `./src/${pkg}/locales/${locale}.ts`), `export { default } from '@univerjs/${pkg}/locales/${locale}';\n`); - }); - } - - const pkgWithLocales = []; - Object.keys(pkgJson.dependencies) - .filter(dep => dep.startsWith('@univerjs/') || dep.startsWith('@univerjs-pro/')) - .forEach((dep) => { - const hasLocales = fs.existsSync(path.resolve(__pkg, './node_modules', dep, './lib/es/locale')); - - if (hasLocales) { - pkgWithLocales.push(dep); - } - }); - - LOCLAES_MAP.forEach((locale) => { - const __locales = path.resolve(__dirname, '../packages', pkg, './src/locales'); - fs.ensureDirSync(__locales); - let content = `import { Tools } from '@univerjs/core';\n\n`; - pkgWithLocales.forEach((dep) => { - const name = dep.replace(/([\-@/])/g, ''); - content += `import ${name} from '${dep}/locale/${locale}';\n`; - }); - content += `\nexport default Tools.deepMerge(\n`; - content += ` {},\n`; - pkgWithLocales.forEach((dep) => { - const name = dep.replace(/([\-@/])/g, ''); - content += ` ${name},\n`; - }); - content += `);\n`; - - fs.writeFileSync(path.resolve(__locales, `${locale}.ts`), content); - }); -}); - -fs.writeFileSync(__mainPkgJSON, `${JSON.stringify(mainPkgJSON, null, 4)}\n`);