Skip to content

Commit

Permalink
Project reorganization and bundling
Browse files Browse the repository at this point in the history
  • Loading branch information
koteelok committed Sep 15, 2023
1 parent bed64af commit 3ee16ec
Show file tree
Hide file tree
Showing 11 changed files with 2,438 additions and 176 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/dist
/tmp
/out-tsc
/bundles

# dependencies
node_modules
Expand Down
12 changes: 0 additions & 12 deletions electron-main/babel.config.js

This file was deleted.

25 changes: 2 additions & 23 deletions electron-main/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,19 @@
"name": "electron-main",
"version": "1.0.0",
"license": "MIT",
"scripts": {
"build": "webpack --config webpack.production.js",
"dev": "webpack --config webpack.dev.js"
},
"devDependencies": {
"@babel/core": "^7.21.4",
"@babel/plugin-transform-modules-commonjs": "^7.21.2",
"@babel/preset-typescript": "^7.21.4",
"@types/fs-extra": "^11.0.1",
"@types/lodash": "^4.14.194",
"@types/node": "^18.15.11",
"@types/uuid": "^9.0.1",
"@vue/babel-plugin-jsx": "^1.1.1",
"css-loader": "^6.7.3",
"fork-ts-checker-webpack-plugin": "^8.0.0",
"raw-loader": "^4.0.2",
"@types/lodash": "^4.14.194",
"sass": "^1.62.0",
"sass-loader": "^13.2.2",
"typescript": "^5.0.4",
"vue": "^3.2.47",
"webpack": "^5.79.0",
"webpack-cli": "^5.0.1",
"webpack-merge": "^5.8.0",
"webpack-node-externals": "^3.0.0",
"@fastify/deepmerge": "^1.3.0",
"babel-loader": "^9.1.2",
"chokidar": "^3.5.3",
"fs-extra": "^11.1.1",
"lodash": "^4.17.21",
"monaco-editor": "0.33.0",
"style-loader": "^3.3.2",
"ts-toolbelt": "^9.6.0",
"uuid": "^9.0.0",
"generate-license-file-webpack-plugin": "^1.0.1",
"node-loader": "^2.0.0"
"uuid": "^9.0.0"
}
}
27 changes: 23 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
"static"
],
"scripts": {
"build": "yarn workspace electron-main run build",
"dev": "yarn workspace electron-main run dev"
"webpack:electron-main:prod": "webpack --config scripts/webpack.prod.js",
"webpack:electron-main:dev": "webpack --config scripts/webpack.dev.js",
"bundle": "node scripts/bundle.js",
"build": "yarn run webpack:electron-main:prod && yarn run bundle",
"dev": "yarn run webpack:electron-main:dev"
},
"panels": {
"localization-editor": {
Expand Down Expand Up @@ -86,7 +89,23 @@
}
}
},
"dependencies": {
"yarn": "^1.22.19"
"devDependencies": {
"@babel/core": "^7.21.4",
"@babel/plugin-transform-modules-commonjs": "^7.21.2",
"@babel/preset-typescript": "^7.21.4",
"@vue/babel-plugin-jsx": "^1.1.1",
"babel-loader": "^9.1.2",
"css-loader": "^6.7.3",
"raw-loader": "^4.0.2",
"sass-loader": "^13.2.2",
"typescript": "^5.0.4",
"fork-ts-checker-webpack-plugin": "^8.0.0",
"style-loader": "^3.3.2",
"node-loader": "^2.0.0",
"webpack": "^5.79.0",
"webpack-cli": "^5.0.1",
"webpack-merge": "^5.8.0",
"webpack-node-externals": "^3.0.0",
"archiver": "^6.0.1"
}
}
55 changes: 55 additions & 0 deletions scripts/bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const fs = require("fs");
const path = require("path");
const archiver = require("archiver");
const package = require("../package.json");

const bundleName = `ysdk-cc-${package.version}.zip`;
const bundlePath = path.resolve(__dirname, `../bundles/${bundleName}`);
const output = fs.createWriteStream(bundlePath);

// create directory ../bundles if not exists
if (!fs.existsSync(path.resolve(__dirname, "../bundles"))) {
fs.mkdirSync(path.resolve(__dirname, "../bundles"));
}

const archive = archiver("zip", {
zlib: { level: 9 },
});

output.on("close", function () {
const kib = (archive.pointer() / 1048576).toPrecision(3);
console.log(`[${bundleName}] Bundle: Success (${kib} MiB)`);
});

archive.on("warning", console.warn);
archive.on("error", console.error);

archive.pipe(output);

const folders = ["@cc", "@types", "dist", "docs", "i18n", "static"];

folders.forEach((folder) => {
archive.directory("./" + folder, folder);
});

const modules = ["i18next"];

for (const module of modules) {
archive.directory(`./node_modules/${module}`, `node_modules/${module}`);
}

const files = [
"AUTHORS",
"CHANGELOG.md",
"CONTRIBUTING.md",
"LICENSE",
"package.json",
"README_ZH.md",
"README.md",
];

files.forEach((file) => {
archive.file("./" + file, { name: file });
});

archive.finalize();
16 changes: 8 additions & 8 deletions electron-main/webpack.dev.js → scripts/webpack.dev.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { merge } = require("webpack-merge");
const base = require("./webpack.base.js");

/** @type {import('webpack').Configuration} */
module.exports = merge(base, {
mode: "development",
watch: true,
});
const { merge } = require("webpack-merge");
const base = require("./webpack.electron-main.js");

/** @type {import('webpack').Configuration} */
module.exports = merge(base, {
mode: "development",
watch: true,
});
220 changes: 116 additions & 104 deletions electron-main/webpack.base.js → scripts/webpack.electron-main.js
Original file line number Diff line number Diff line change
@@ -1,104 +1,116 @@
const { LicenseFilePlugin } = require("generate-license-file-webpack-plugin");
const ForkTsCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
const path = require("path");
const webpack = require("webpack");

const entryFiles = [
"./src/main.ts",
"./src/scene/index.ts",
"./src/server.ts",
"./src/panels/localization-editor/index.ts",
"./src/builder/index.ts",
"./src/builder/asset-handlers.ts",
"./src/builder/web-hooks.ts",
].reduce((obj, el) => {
el = el.replaceAll("\\", "/");
obj[el.replace(".ts", "")] = "./" + el;
return obj;
}, {});

const optionalPlugins = [];
if (process.platform !== "darwin") {
optionalPlugins.push(
new webpack.IgnorePlugin({ resourceRegExp: /^fsevents$/ })
);
}
/** @type {import('webpack').Configuration} */
module.exports = {
target: "electron-main",
devtool: "source-map",
entry: {
...entryFiles,
},
resolve: {
alias: {
"@common": path.join(__dirname, "../static/assets/common"),
},
extensions: [".ts", ".tsx", ".js", ".html", ".css", ".sass", ".scss"],
},
module: {
rules: [
{
test: /\.tsx?$/, // .ts, .tsx
loader: "babel-loader",
exclude: /node_modules/,
},
{
test: /\.(s[ac]|c)ss$/, // .css, .sass, .scss
use: ["raw-loader", "sass-loader"],
exclude: /node_modules/,
},
{
test: /\.css$/,
use: ["style-loader", "css-loader"],
include: /node_modules/,
},
{
test: /\.html$/,
loader: "raw-loader",
},
{
test: /.node$/,
loader: "node-loader",
},
],
},
optimization: {
minimize: false,
splitChunks: {
cacheGroups: {
defaultVendors: {
test: /[\\/]node_modules[\\/]/,
enforce: true,
reuseExistingChunk: true,
name(module) {
const name = module.resourceResolveData.descriptionFileData?.name;
if (name) return "modules/" + name + "/" + module.buildInfo.hash;
},
},
},
},
},
output: {
path: path.resolve(__dirname, "../dist/electron-main"),
libraryTarget: "umd",
},
externalsPresets: {
node: true,
},
plugins: [
...optionalPlugins,
new ForkTsCheckerWebpackPlugin(),
new webpack.DefinePlugin({
__VUE_OPTIONS_API__: false,
__VUE_PROD_DEVTOOLS__: false,
}),
new LicenseFilePlugin({
outputFileName: "LICENSE",
outputFolder: "./", // Relative to your build output directory.
pathToPackageJson: "../package.json",
isDev: false, // When true, uses placeholder content to reduce compilation time.
lineEnding: undefined, // Can be 'crlf' or 'lf'. If omitted, the system default will be used.
}),
],
};
const ForkTsCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
const path = require("path");
const webpack = require("webpack");

const entryFiles = [
"./src/main.ts",
"./src/scene/index.ts",
"./src/server.ts",
"./src/panels/localization-editor/index.ts",
"./src/builder/index.ts",
"./src/builder/asset-handlers.ts",
"./src/builder/web-hooks.ts",
].reduce((obj, el) => {
el = el.replaceAll("\\", "/");
obj[el.replace(".ts", "")] = "./" + el;
return obj;
}, {});

const optionalPlugins = [];
if (process.platform !== "darwin") {
optionalPlugins.push(
new webpack.IgnorePlugin({ resourceRegExp: /^fsevents$/ })
);
}

/** @type {import('webpack').Configuration} */
module.exports = {
target: "electron-main",
devtool: "source-map",
entry: {
...entryFiles,
},
context: path.resolve(__dirname, "../electron-main/"),
resolve: {
alias: {
"@common": path.join(__dirname, "../static/assets/common"),
},
extensions: [".ts", ".tsx", ".js", ".html", ".css", ".sass", ".scss"],
},
module: {
rules: [
{
test: /\.tsx?$/, // .ts, .tsx
use: {
loader: "babel-loader",
options: {
presets: ["@babel/preset-typescript"],
plugins: [
[
"@vue/babel-plugin-jsx",
{
isCustomElement: (tag) => tag.includes("ui-"),
},
],
"@babel/plugin-transform-modules-commonjs",
],
},
},
exclude: /node_modules/,
},
{
test: /\.(s[ac]|c)ss$/, // .css, .sass, .scss
use: ["raw-loader", "sass-loader"],
exclude: /node_modules/,
},
{
test: /\.css$/,
use: ["style-loader", "css-loader"],
include: /node_modules/,
},
{
test: /\.html$/,
loader: "raw-loader",
},
{
test: /.node$/,
loader: "node-loader",
},
],
},
optimization: {
minimize: false,
splitChunks: {
cacheGroups: {
defaultVendors: {
test: /[\\/]node_modules[\\/]/,
enforce: true,
reuseExistingChunk: true,
name(module) {
const name = module.resourceResolveData.descriptionFileData?.name;
if (name) return "modules/" + name + "/" + module.buildInfo.hash;
},
},
},
},
},
output: {
path: path.resolve(__dirname, "../dist/electron-main"),
libraryTarget: "umd",
},
externalsPresets: {
node: true,
},
plugins: [
...optionalPlugins,
new ForkTsCheckerWebpackPlugin({
typescript: {
configFile: path.resolve(__dirname, "../electron-main/tsconfig.json"),
},
}),
new webpack.DefinePlugin({
__VUE_OPTIONS_API__: false,
__VUE_PROD_DEVTOOLS__: false,
}),
],
};
Loading

0 comments on commit 3ee16ec

Please sign in to comment.