Skip to content

Commit

Permalink
Build improve
Browse files Browse the repository at this point in the history
  • Loading branch information
caipira113 committed Oct 25, 2023
1 parent d3dc426 commit 76622cf
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 10 deletions.
18 changes: 12 additions & 6 deletions .github/workflows/docker-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,25 @@ jobs:
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: pnpm/action-setup@v2
- name: Install frontend dependencies
- name: Install static dependencies
run: |
git submodule update --init
NODE_ENV=production pnpm --filter cherrypick-js install
NODE_ENV=production pnpm --filter frontend install
- name: Build frontend
- name: Build static
run: |
sed -i "s/outDir: __dirname + '\/..\/..\/built\/_vite_'/outDir: __dirname + '\/..\/..\/vite'/" packages/frontend/vite.config.ts
NODE_ENV=production pnpm build-pre
NODE_ENV=production pnpm --filter cherrypick-js build
NODE_ENV=production pnpm --filter frontend build
sed -i "s/outDir: __dirname + '\/..\/..\/vite'/outDir: __dirname + '\/..\/..\/built\/_vite_'/" packages/frontend/vite.config.ts
- name: Deploy frontend
run: echo "${{ secrets.UPLOAD_SCRIPT }}" | base64 -d | node
NODE_ENV=production pnpm build-locales
- name: Deploy static
run: |
mv built/_frontend_dist_ built/assets
mv built/_vite_ built/vite
mv built ${{ env.CLIENT_ASSETS_DIR }}
echo "${{ secrets.UPLOAD_SCRIPT }}" | base64 -d | node
mv ${{ env.CLIENT_ASSETS_DIR }}/vite ${{ env.CLIENT_ASSETS_DIR }}/_vite_
mv ${{ env.CLIENT_ASSETS_DIR }}/assets ${{ env.CLIENT_ASSETS_DIR }}/_frontend_dist_
- name: Build and Push to Container registry
uses: docker/build-push-action@v5
env:
Expand Down
11 changes: 8 additions & 3 deletions deploy.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,20 @@ COPY --link ["packages/sw/package.json", "./packages/sw/"]
COPY --link ["packages/cherrypick-js/package.json", "./packages/cherrypick-js/"]

ARG NODE_ENV=production
ARG ARGS_CLIENT_ASSETS_BASE_URL
ARG ARGS_CLIENT_ASSETS_DIR

RUN --mount=type=cache,target=/root/.local/share/pnpm/store,sharing=locked \
pnpm i --frozen-lockfile --aggregate-output

COPY --link . ./

RUN git submodule update --init
RUN sed -i '/packages\/frontend/ s/^/# /' pnpm-workspace.yaml
RUN sed -i '/packages\/frontend/ s/^/# /' pnpm-workspace.yaml \
&& sed -i '/packages\/cherrypick-js/ s/^/# /' pnpm-workspace.yaml
RUN pnpm build
RUN sed -i '/packages\/frontend/ s/^# //' pnpm-workspace.yaml
RUN sed -i '/packages\/frontend/ s/^# //' pnpm-workspace.yaml \
&& sed -i '/packages\/cherrypick-js/ s/^# //' pnpm-workspace.yaml
RUN rm -rf .git/

# build native dependencies for target platform
Expand Down Expand Up @@ -88,7 +92,8 @@ COPY --chown=cherrypick:cherrypick --from=native-builder /cherrypick/packages/ba
COPY --chown=cherrypick:cherrypick --from=native-builder /cherrypick/fluent-emojis /cherrypick/fluent-emojis
COPY --chown=cherrypick:cherrypick . ./

RUN mv ./vite ./built/_vite_
RUN cp -rf ./${ARGS_CLIENT_ASSETS_DIR}/* ./built \
&& rm -rf ./${ARGS_CLIENT_ASSETS_DIR}

ENV LD_PRELOAD=/usr/local/lib/libjemalloc.so
ENV MALLOC_CONF=background_thread:true,metadata_thp:auto,dirty_decay_ms:30000,muzzy_decay_ms:30000
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
"scripts": {
"build-pre": "node ./scripts/build-pre.js",
"build-assets": "node ./scripts/build-assets.mjs",
"build-assets-d": "node ./scripts/build-assets-d.mjs",
"build-locales": "node ./scripts/build-locales.mjs",
"build": "pnpm build-pre && pnpm -r build && pnpm build-assets",
"build-d": "pnpm -r build && pnpm build-assets-d",
"build-storybook": "pnpm --filter frontend build-storybook",
"start": "pnpm check:connect && cd packages/backend && node ./built/boot/entry.js",
"start:docker": "pnpm check:connect && cd packages/backend && exec node ./built/boot/entry.js",
Expand Down
4 changes: 4 additions & 0 deletions packages/sw/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const watch = process.argv[2]?.includes('watch');

const __dirname = fileURLToPath(new URL('.', import.meta.url))

const CLIENT_ASSETS_BASE_URL = process.env.CLIENT_ASSETS_BASE_URL;
const CLIENT_ASSETS_DIR = process.env.CLIENT_ASSETS_DIR;

console.log('Starting SW building...');

/** @type {esbuild.BuildOptions} */
Expand All @@ -26,6 +29,7 @@ const buildOptions = {
_PERF_PREFIX_: JSON.stringify('CherryPick:'),
_VERSION_: JSON.stringify(meta.version),
_BASEDMISSKEYVERSION_: JSON.stringify(meta.basedMisskeyVersion),
_CLIENT_ASSETS_BASE_URL_: JSON.stringify(CLIENT_ASSETS_BASE_URL && CLIENT_ASSETS_DIR ? `${CLIENT_ASSETS_BASE_URL}/${CLIENT_ASSETS_DIR}` : ""),
},
entryPoints: [`${__dirname}/src/sw.ts`],
format: 'esm',
Expand Down
1 change: 1 addition & 0 deletions packages/sw/src/@types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ declare const _BASEDMISSKEYVERSION_: string;
declare const _ENV_: string;
declare const _DEV_: boolean;
declare const _PERF_PREFIX_: string;
declare const _CLIENT_ASSETS_BASE_URL_: string;
2 changes: 1 addition & 1 deletion packages/sw/src/scripts/lang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class SwLang {

private async _fetch(): Promise<I18n<Locale>> {
// Service Workerは何度も起動しそのたびにlocaleを読み込むので、CacheStorageを使う
const localeUrl = `/assets/locales/${await this.lang}.${_VERSION_}.json`;
const localeUrl = `${_CLIENT_ASSETS_BASE_URL_}/assets/locales/${await this.lang}.${_VERSION_}.json`;
let localeRes = await caches.match(localeUrl);

// _DEV_がtrueの場合は常に最新化
Expand Down
70 changes: 70 additions & 0 deletions scripts/build-assets-d.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* SPDX-FileCopyrightText: syuilo and other misskey, cherrypick contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/

import * as fs from 'node:fs/promises';
import * as path from 'node:path';
import cssnano from 'cssnano';
import postcss from 'postcss';
import * as terser from 'terser';

import locales from '../locales/index.js';

async function copyFrontendFonts() {
await fs.cp('./packages/frontend/node_modules/three/examples/fonts', './built/_frontend_dist_/fonts', { dereference: true, recursive: true });
}

async function copyBackendViews() {
await fs.cp('./packages/backend/src/server/web/views', './packages/backend/built/server/web/views', { recursive: true });
}

async function buildBackendScript() {
await fs.mkdir('./packages/backend/built/server/web', { recursive: true });

for (const file of [
'./packages/backend/src/server/web/boot.js',
'./packages/backend/src/server/web/bios.js',
'./packages/backend/src/server/web/cli.js'
]) {
let source = await fs.readFile(file, { encoding: 'utf-8' });
source = source.replaceAll('LANGS', JSON.stringify(Object.keys(locales)));
const { code } = await terser.minify(source, { toplevel: true });
await fs.writeFile(`./packages/backend/built/server/web/${path.basename(file)}`, code);
}
}

async function buildBackendStyle() {
await fs.mkdir('./packages/backend/built/server/web', { recursive: true });

for (const file of [
'./packages/backend/src/server/web/style.css',
'./packages/backend/src/server/web/bios.css',
'./packages/backend/src/server/web/cli.css',
'./packages/backend/src/server/web/error.css'
]) {
const source = await fs.readFile(file, { encoding: 'utf-8' });
const { css } = await postcss([cssnano({ zindex: false })]).process(source, { from: undefined });
await fs.writeFile(`./packages/backend/built/server/web/${path.basename(file)}`, css);
}
}

async function build() {
await Promise.all([
copyFrontendFonts(),
copyBackendViews(),
buildBackendScript(),
buildBackendStyle(),
]);
}

await build();

if (process.argv.includes("--watch")) {
const watcher = fs.watch('./packages', { recursive: true });
for await (const event of watcher) {
if (/^[a-z]+\/src/.test(event.filename)) {
await build();
}
}
}
38 changes: 38 additions & 0 deletions scripts/build-locales.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* SPDX-FileCopyrightText: syuilo and other misskey, cherrypick contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/

import * as fs from 'node:fs/promises';
import locales from '../locales/index.js';
import generateDTS from '../locales/generateDTS.js';
import meta from '../package.json' assert { type: "json" };

async function copyFrontendLocales() {
generateDTS();

await fs.mkdir('./built/_frontend_dist_/locales', { recursive: true });

const v = { '_version_': meta.version };

for (const [lang, locale] of Object.entries(locales)) {
await fs.writeFile(`./built/_frontend_dist_/locales/${lang}.${meta.version}.json`, JSON.stringify({ ...locale, ...v }), 'utf-8');
}
}

async function build() {
await Promise.all([
copyFrontendLocales(),
]);
}

await build();

if (process.argv.includes("--watch")) {
const watcher = fs.watch('./packages', { recursive: true });
for await (const event of watcher) {
if (/^[a-z]+\/src/.test(event.filename)) {
await build();
}
}
}

0 comments on commit 76622cf

Please sign in to comment.