Skip to content

Commit

Permalink
feat: add esbuild config for zhi-core
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Apr 10, 2023
1 parent c6d51bc commit 722a070
Show file tree
Hide file tree
Showing 11 changed files with 482 additions and 96 deletions.
2 changes: 2 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## registry=https://registry.npmjs.com
registry=https://registry.npmmirror.com
1 change: 1 addition & 0 deletions apps/zhi-core/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
17 changes: 16 additions & 1 deletion apps/zhi-core/esbuild.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,19 @@
* questions.
*/

console.log("hello, esbuild")
import { BuildOptions } from "esbuild"
import path from "path"

const outDir = "dist"
const outFile = "theme.js"

/**
* 构建配置
*/
export const esbuildConfig: BuildOptions = {
entryPoints: ["src/index.ts"],
outfile: path.join(outDir, outFile),
bundle: true,
format: "cjs",
platform: "node",
}
12 changes: 8 additions & 4 deletions apps/zhi-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@
"theme"
],
"scripts": {
"dev": "node --experimental-specifier-resolution=node --loader ts-node/esm esbuild.config.ts"
"dev": "node --experimental-specifier-resolution=node --loader ts-node/esm scripts/build.ts --watch",
"build": "node --experimental-specifier-resolution=node --loader ts-node/esm scripts/build.ts --production"
},
"devDependencies": {
"esbuild": "^0.17.16",
"esbuild-dev-server": "^0.3.0",
"ts-node": "^10.9.1"
"@types/minimist": "^1.2.2",
"@types/node": "^18.15.11",
"esbuild": "^0.16.9",
"minimist": "^1.2.8",
"ts-node": "^10.9.1",
"tsconfig": "workspace:*"
}
}
2 changes: 2 additions & 0 deletions apps/zhi-core/public/plugin.js

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions apps/zhi-core/public/theme.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "zhi",
"author": "terwer",
"url": "https://github.com/terwer/zhi",
"version": "1.0.0",
"modes": ["dark", "light", "green"]
}
4 changes: 4 additions & 0 deletions apps/zhi-core/public/update-plugin-system.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## How to update plugin system

- Download the latest `插件系统` widget in the siyuan-note bazaar
- Copy `plugin.js` to `zhi/plugin.js`
130 changes: 130 additions & 0 deletions apps/zhi-core/scripts/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Copyright (c) 2023, Terwer . All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Terwer designates this
* particular file as subject to the "Classpath" exception as provided
* by Terwer in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com
* or visit www.terwer.space if you need additional information or have any
* questions.
*/

import esbuild from "esbuild"
import { esbuildConfig } from "../esbuild.config"
import minimist from "minimist"

/**
* zhi 主题构建
*
* @author terwer
* @since 1.0.0
*/
class ZhiBuild {
/**
* 构建过程
*
* @param isWatch - 是否监视
* @param isProduction - 是否生产模式
*/
public static async processBuild(isWatch: boolean, isProduction: boolean) {
// dev
if (isWatch) {
const firstBuildFinished = new Set()
let buildStartTime: number

// Following the log format of https://github.com/connor4312/esbuild-problem-matchers
const status = (msg: any) => console.log(`${isWatch ? "[watch] " : ""}${msg}`)
const watchPlugin = (type: any) => ({
name: "watcher",
setup(build: any) {
build.onStart(() => {
buildStartTime = Date.now()
status(`${type} build started.`)
})
build.onEnd((result: any) => {
result.errors.forEach((error: any) =>
console.error(
`> ${error.location.file}:${error.location.line}:${error.location.column}: error: ${error.text}`
)
)
firstBuildFinished.add(type)
status(`${type} build finished in ${Date.now() - buildStartTime} ms.`)
if (firstBuildFinished.size === 2) {
// esbuild problem matcher extension is listening for this log, once this is logged, it will open the Extension Host
// So we have to assure only printing this when both extension and webview have been built
status(`build finished in ${Date.now() - buildStartTime} ms.`)
}
})
},
})

if (!esbuildConfig.plugins) {
esbuildConfig.plugins = []
}
esbuildConfig.plugins.push(watchPlugin("extension"))
esbuildConfig.watch = true
}

// 是否压缩
esbuildConfig.minify = isProduction
esbuildConfig.sourcemap = isProduction ? false : "inline"

// define
if (!esbuildConfig.define) {
esbuildConfig.define = {}
}
esbuildConfig.define = {
...esbuildConfig.define,
"process.env.NODE_ENV": isProduction ? '"production"' : '"development"',
}

// hande result
const resultHandler = async (result: any) => {
result.metafile &&
console.log(
await esbuild.analyzeMetafile(result.metafile, {
verbose: true,
})
)
}

// do build
esbuild
.build(esbuildConfig)
.then(resultHandler)
.catch(() => {
process.exit(1)
})
}
}

;(async function () {
console.log("Zhi plugins is building...")

const args = minimist(process.argv.slice(2))
const isWatch = args.watch || args.w
const isProduction = args.production
console.log("isWatch=>", isWatch)
console.log("isProduction=>", isProduction)

try {
const buildResult = await ZhiBuild.processBuild(isWatch, isProduction)
console.log("Zhi build success.")
} catch (e) {
console.error("Zhi build error=>", e)
}
})()
25 changes: 25 additions & 0 deletions apps/zhi-core/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2023, Terwer . All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Terwer designates this
* particular file as subject to the "Classpath" exception as provided
* by Terwer in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com
* or visit www.terwer.space if you need additional information or have any
* questions.
*/
console.log("hello, zhi theme")
11 changes: 11 additions & 0 deletions apps/zhi-core/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "tsconfig/base.json",
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"isolatedModules": false,
"esModuleInterop": true
},
"include": ["env.d.ts", "**/*.ts"],
"exclude": ["node_modules"]
}
Loading

0 comments on commit 722a070

Please sign in to comment.