Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add worker presets #8

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ jobs:
uses: ./.github/actions/setup-node

- name: 🩺 Run build
run: pnpm build
run: |
pnpm build
pnpm build:demo
28 changes: 28 additions & 0 deletions common/shared/prepare/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { execSync } from 'node:child_process';
import path from 'node:path';
import process from 'node:process';
import fs from 'fs-extra';
Expand Down Expand Up @@ -59,15 +60,42 @@ export function createLocalesFiles() {
});
}

interface IPkg {
name: string;
path: string;
private: boolean;
hasWorker: boolean;
}

function getWorkspacePkgs() {
const pkgs = JSON.parse(execSync('pnpm m ls --json').toString()) as IPkg[];
const result: Record<string, IPkg> = {};

pkgs.filter(it => it.name.startsWith('@univerjs/preset-')).forEach((it) => {
it.hasWorker = fs.existsSync(path.join(it.path, 'src', 'worker.ts'));
result[it.name] = it;
});
return result;
}

export function createPresetsFiles() {
cleanupLibDir();
const presetPkgs = getWorkspacePkgs();

Object.keys(pkg.dependencies).forEach((key) => {
if (key.startsWith('@univerjs/preset')) {
const indexTs = `export * from '${key}';\n`;
const __indexTs = path.resolve(__dirname, 'src', `${key.replace('@univerjs/', '')}/index.ts`);
fs.ensureFileSync(__indexTs);
fs.writeFileSync(__indexTs, indexTs);

if (presetPkgs[key].hasWorker) {
const workerTs = `export * from '${key}/worker';\n`;
const __workerTs = path.resolve(__dirname, 'src', `${key.replace('@univerjs/', '')}/worker.ts`);
fs.ensureFileSync(__workerTs);
fs.writeFileSync(__workerTs, workerTs);
}

LOCLAES_MAP.forEach((localeKey) => {
const localeTs = `export { default } from '${key}/locales/${localeKey}';\n`;
const __localeTs = path.resolve(__dirname, 'src', `${key.replace('@univerjs/', '')}/locales/${localeKey}.ts`);
Expand Down
8 changes: 6 additions & 2 deletions common/shared/vite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ export async function build(options?: IBuildOptions) {
}
}

const hasWorker = fs.existsSync(path.resolve(__dirname, 'src/web-worker.ts'));
const hasWorker = fs.existsSync(path.resolve(__dirname, 'src/worker.ts'));
if (hasWorker) {
entry.webWorker = path.resolve(__dirname, 'src/web-worker.ts');
entry.webWorker = path.resolve(__dirname, 'src/worker.ts');
}

const sharedConfig: InlineConfig = {
Expand Down Expand Up @@ -184,6 +184,10 @@ export async function build(options?: IBuildOptions) {
const __presetDir = path.resolve(__dirname, 'src', preset);
entry[`${preset}/index`] = path.resolve(__presetDir, 'index.ts');

if (fs.existsSync(path.resolve(__presetDir, 'worker.ts'))) {
entry[`${preset}/worker`] = path.resolve(__presetDir, 'worker.ts');
}

const locales = fs.readdirSync(path.resolve(__presetDir, 'locales'));
for (const file of locales) {
const localeValue = file.replace('.ts', '');
Expand Down
2 changes: 1 addition & 1 deletion examples-node/esbuild.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const define = {

if (!args.watch) {
const gitCommitHash = execSync('git rev-parse --short HEAD').toString().trim();
const gitRefName = execSync('git symbolic-ref -q --short HEAD || git describe --tags --exact-match').toString().trim();
const gitRefName = execSync('git symbolic-ref -q --short HEAD || git describe --tags --exact-match || echo "unknown"').toString().trim();

define['process.env.GIT_COMMIT_HASH'] = `"${gitCommitHash}"`;
define['process.env.GIT_REF_NAME'] = `"${gitRefName}"`;
Expand Down
2 changes: 1 addition & 1 deletion examples-node/src/sheets-node-basic/worker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createUniver, LocaleType } from '@univerjs/presets';
import { UniverSheetsNodeCoreWorkerPreset } from '@univerjs/presets/preset-sheets-node-core/web-worker';
import { UniverSheetsNodeCoreWorkerPreset } from '@univerjs/presets/preset-sheets-node-core/worker';

createUniver({
locale: LocaleType.ZH_CN,
Expand Down
2 changes: 1 addition & 1 deletion examples/esbuild.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const define = {

if (!args.watch) {
const gitCommitHash = execSync('git rev-parse --short HEAD').toString().trim();
const gitRefName = execSync('git symbolic-ref -q --short HEAD || git describe --tags --exact-match').toString().trim();
const gitRefName = execSync('git symbolic-ref -q --short HEAD || git describe --tags --exact-match || echo "unknown"').toString().trim();

define['process.env.GIT_COMMIT_HASH'] = `"${gitCommitHash}"`;
define['process.env.GIT_REF_NAME'] = `"${gitRefName}"`;
Expand Down
4 changes: 2 additions & 2 deletions examples/src/sheets-collaboration-with-worker/worker.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createUniver, LocaleType, Tools } from '@univerjs/presets';
import sheetsAdvancedZhCN from '@univerjs/presets/preset-sheets-advanced/locales/zh-CN';
import { UniverSheetsAdvancedWorkerPreset } from '@univerjs/presets/preset-sheets-advanced/web-worker';
import { UniverSheetsAdvancedWorkerPreset } from '@univerjs/presets/preset-sheets-advanced/worker';
import sheetsCoreZhCN from '@univerjs/presets/preset-sheets-core/locales/zh-CN';
import { UniverSheetsCoreWorkerPreset } from '@univerjs/presets/preset-sheets-core/web-worker';
import { UniverSheetsCoreWorkerPreset } from '@univerjs/presets/preset-sheets-core/worker';

createUniver({
locale: LocaleType.ZH_CN,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"prepare": "simple-git-hooks",
"dev": "pnpm --filter examples dev:demo",
"build": "turbo build --concurrency=2",
"build:demo": "pnpm --filter examples build:demo",
"build:demo": "pnpm --filter examples --filter examples-node build:demo",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"release": "release-it"
Expand Down
8 changes: 4 additions & 4 deletions packages/preset-sheets-advanced/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"exports": {
".": "./src/index.ts",
"./*": "./src/*",
"./web-worker": "./src/web-worker.ts",
"./worker": "./src/worker.ts",
"./locales/*": "./src/locales/*.ts"
},
"main": "./src/index.ts",
Expand All @@ -42,10 +42,10 @@
"require": "./lib/cjs/*",
"types": "./lib/types/index.d.ts"
},
"./web-worker": {
"import": "./lib/es/web-worker.js",
"./worker": {
"import": "./lib/es/worker.js",
"require": "./lib/cjs/locales/*.js",
"types": "./lib/types/web-worker.d.ts"
"types": "./lib/types/worker.d.ts"
},
"./locales/*": {
"import": "./lib/es/locales/*.js",
Expand Down
8 changes: 4 additions & 4 deletions packages/preset-sheets-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"exports": {
".": "./src/index.ts",
"./*": "./src/*",
"./web-worker": "./src/web-worker.ts",
"./worker": "./src/worker.ts",
"./locales/*": "./src/locales/*.ts"
},
"publishConfig": {
Expand All @@ -41,10 +41,10 @@
"require": "./lib/cjs/*",
"types": "./lib/types/index.d.ts"
},
"./web-worker": {
"import": "./lib/es/web-worker.js",
"./worker": {
"import": "./lib/es/worker.js",
"require": "./lib/cjs/locales/*.js",
"types": "./lib/types/web-worker.d.ts"
"types": "./lib/types/worker.d.ts"
},
"./locales/*": {
"import": "./lib/es/locales/*.js",
Expand Down