From 4bf63a73e0e08e65b541453978267bba967a9eca Mon Sep 17 00:00:00 2001 From: Ryan Cao <615-ry@users.noreply.git.joinfirefish.org> Date: Fri, 16 Feb 2024 09:31:32 +0800 Subject: [PATCH] refactor: make `client` and `sw` ES modules --- gulpfile.js | 8 +++++--- locales/{index.js => index.mjs} | 25 ++++++++++++++++--------- packages/client/package.json | 1 + packages/client/vite.config.ts | 6 +++--- packages/sw/package.json | 1 + packages/sw/tsconfig.json | 6 ++---- packages/sw/vite.config.ts | 4 ++-- 7 files changed, 30 insertions(+), 21 deletions(-) rename locales/{index.js => index.mjs} (78%) diff --git a/gulpfile.js b/gulpfile.js index 37a8e75b2..66f93dabb 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -8,7 +8,6 @@ const replace = require("gulp-replace"); const terser = require("gulp-terser"); const cssnano = require("gulp-cssnano"); -const locales = require("./locales"); const meta = require("./package.json"); gulp.task("copy:backend:views", () => @@ -29,8 +28,9 @@ gulp.task("copy:client:fonts", () => .pipe(gulp.dest("./built/_client_dist_/fonts/")), ); -gulp.task("copy:client:locales", (cb) => { +gulp.task("copy:client:locales", async (cb) => { fs.mkdirSync("./built/_client_dist_/locales", { recursive: true }); + const { default: locales } = await import("./locales/index.mjs"); const v = { _version_: meta.version }; @@ -45,7 +45,9 @@ gulp.task("copy:client:locales", (cb) => { cb(); }); -gulp.task("build:backend:script", () => { +gulp.task("build:backend:script", async () => { + const { default: locales } = await import("./locales/index.mjs"); + return gulp .src([ "./packages/backend/src/server/web/boot.js", diff --git a/locales/index.js b/locales/index.mjs similarity index 78% rename from locales/index.js rename to locales/index.mjs index 2c27c42c9..c65151e17 100644 --- a/locales/index.js +++ b/locales/index.mjs @@ -2,8 +2,14 @@ * Languages Loader */ -const fs = require("fs"); -const yaml = require("js-yaml"); +import fs from "node:fs"; +import yaml from "js-yaml"; + +import { dirname } from "node:path"; +import { fileURLToPath } from "node:url"; + +const __dirname = dirname(fileURLToPath(import.meta.url)); + const languages = []; const languages_custom = []; @@ -66,16 +72,17 @@ const locales_custom = languages_custom.reduce( ); Object.assign(locales, locales_custom); -module.exports = Object.entries(locales).reduce( +export default Object.entries(locales).reduce( (a, [k, v]) => ( (a[k] = (() => { const [lang] = k.split("-"); - return k === "en-US" ? v : - merge( - locales["en-US"], - locales[`${lang}-${primaries[lang]}`] || {}, - v, - ); + return k === "en-US" + ? v + : merge( + locales["en-US"], + locales[`${lang}-${primaries[lang]}`] || {}, + v, + ); })()), a ), diff --git a/packages/client/package.json b/packages/client/package.json index 7aa7a995e..c3eb49826 100644 --- a/packages/client/package.json +++ b/packages/client/package.json @@ -1,6 +1,7 @@ { "name": "client", "private": true, + "type": "module", "scripts": { "watch": "pnpm vite build --watch --mode development", "build": "pnpm vite build", diff --git a/packages/client/vite.config.ts b/packages/client/vite.config.ts index 76c29ff26..14bbc4466 100644 --- a/packages/client/vite.config.ts +++ b/packages/client/vite.config.ts @@ -2,10 +2,10 @@ import * as fs from "fs"; import pluginVue from "@vitejs/plugin-vue"; import { defineConfig } from "vite"; -import viteCompression from "vite-plugin-compression"; -import locales from "../../locales"; -import meta from "../../package.json"; +import locales from "../../locales/index.mjs"; +import meta from "../../package.json" assert { type: "json" }; import pluginJson5 from "./vite.json5"; +import viteCompression from "vite-plugin-compression"; const extensions = [ ".ts", diff --git a/packages/sw/package.json b/packages/sw/package.json index aa3e77603..fe09f05d9 100644 --- a/packages/sw/package.json +++ b/packages/sw/package.json @@ -1,6 +1,7 @@ { "name": "sw", "private": true, + "type": "module", "scripts": { "build": "pnpm vite build --emptyOutDir", "build:debug": "pnpm run build", diff --git a/packages/sw/tsconfig.json b/packages/sw/tsconfig.json index d2ae9e47b..43064e1d0 100644 --- a/packages/sw/tsconfig.json +++ b/packages/sw/tsconfig.json @@ -16,6 +16,7 @@ "noLib": false, "strict": true, "strictNullChecks": true, + "allowSyntheticDefaultImports": true, "experimentalDecorators": true, "resolveJsonModule": true, "isolatedModules": true, @@ -23,10 +24,7 @@ "paths": { "@/*": ["./src/*"] }, - "typeRoots": [ - "node_modules/@types", - "@types", - ], + "typeRoots": ["node_modules/@types", "@types"], "lib": ["esnext", "webworker"] }, "compileOnSave": false, diff --git a/packages/sw/vite.config.ts b/packages/sw/vite.config.ts index fb41c8914..69b13f7fd 100644 --- a/packages/sw/vite.config.ts +++ b/packages/sw/vite.config.ts @@ -1,6 +1,6 @@ import { defineConfig } from "vite"; -const locales = require("../../locales"); -const meta = require("../../package.json"); +import locales from "../../locales/index.mjs"; +import meta from "../../package.json" assert { type: "json" }; const isProduction = process.env.NODE_ENV === "production"; import viteCompression from "vite-plugin-compression";