Skip to content

Commit

Permalink
npm the build scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Gk0Wk committed Dec 4, 2022
1 parent a37ec8c commit 3208be9
Show file tree
Hide file tree
Showing 13 changed files with 149 additions and 890 deletions.
16 changes: 4 additions & 12 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ on:
- 'README.md'
- 'wiki/**'
- 'public/**'
- 'scripts/**'
- '.scripts/**'
- '.vscode'
- '.idea'

Expand All @@ -39,31 +39,23 @@ jobs:
with:
node-version: '16'

- name: Get npm cache directory
uses: actions/cache@v3
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
${{ runner.os }}-node-
- name: Cache dependencies
uses: actions/cache@v3
with:
path: |
**/node_modules
~/.pnpm-store
~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
${{ runner.os }}-node-
- name: Install Dependencies
run: npm install
run: npm install -g pnpm && pnpm install

- name: Build Plugins
run: npm run build
run: pnpm run build

- name: Create Release
uses: softprops/action-gh-release@v1
Expand Down
143 changes: 47 additions & 96 deletions scripts/tw.ts → .scripts/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,43 @@
import fs from 'fs';
import path from 'path';
import { tmpdir } from 'os';
import UglifyJS from 'uglify-js';
import { program } from 'commander';
import { minify as htmlMinify } from 'html-minifier-terser';
import { tryCopy, mkdirsForFileSync, tiddlywiki } from './utils';
import { ITiddlyWiki } from 'tw5-typed';
import { TiddlyWiki } from 'tiddlywiki';

/**
* 初始化 TiddlyWiki
*
* @param {Record<string, unknown>[]} [preloadTiddlers=[]] 额外的 tiddler
* @param {string} [dir='.'] 工作路径
* @param {string[]} [commands=[]] 附加指令
* @return {ITiddlyWiki}
*/
export const tiddlywiki = (
preloadTiddlers: Record<string, unknown>[] = [],
dir = '.',
commands: string[] = [],
): ITiddlyWiki => {
const $tw = TiddlyWiki();
$tw.boot.argv = [dir, ...commands];
$tw.preloadTiddlerArray(preloadTiddlers);
$tw.boot.boot();
return $tw;
};

export const mkdirsForFileSync = (fileName: string) => {
const _path = path.dirname(fileName);
if (!fs.existsSync(_path)) {
mkdirsForFileSync(_path);
fs.mkdirSync(_path);
}
};

export const tryCopy = (from: string, to: string) => {
if (fs.existsSync(from)) {
fs.cpSync(from, to, { force: true, errorOnExist: false, recursive: true });
}
};

const waitForFile = (path: string) =>
new Promise<void>(resolve => {
Expand All @@ -17,42 +50,6 @@ const waitForFile = (path: string) =>
}, 100);
});

const htmlMinifierOptions = {
caseSensitive: true,
collapseBooleanAttributes: false,
collapseInlineTagWhitespace: false,
collapseWhitespace: true,
conservativeCollapse: true,
continueOnParseError: true,
customAttrCollapse: /.*/,
decodeEntities: true,
html5: true,
ignoreCustomFragments: [/<#[\s\S]*?#>/, /<%[\s\S]*?%>/, /<\?[\s\S]*?\?>/],
includeAutoGeneratedTags: false,
keepClosingSlash: false,
maxLineLength: 0,
minifyCSS: true,
minifyJS: true,
minifyURLs: true,
preserveLineBreaks: false,
preventAttributesEscaping: false,
processConditionalComments: true,
processScripts: ['text/html'],
removeAttributeQuotes: true,
removeComments: true,
removeEmptyAttributes: true,
removeEmptyElements: false,
removeOptionalTags: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
removeTagWhitespace: true,
sortAttributes: true,
sortClassName: true,
trimCustomFragments: true,
useShortDoctype: true,
};

/** 项目路径 */
const repoFolder = process.cwd();
const wikiFolder = path.resolve(repoFolder, 'wiki');
Expand All @@ -69,7 +66,6 @@ const tiddlersFolder = path.resolve(wikiFolder, 'tiddlers');
const buildOnlineHTML = async (
dist = 'dist',
htmlName = 'index.html',
minify = true,
excludeFilter = '-[is[draft]]',
) => {
const distDir = path.resolve(dist);
Expand Down Expand Up @@ -133,6 +129,10 @@ const buildOnlineHTML = async (

const rawCoreJsPath = path.join(distDir, 'tiddlywikicore.js');
await waitForFile(rawCoreJsPath);
fs.renameSync(
rawCoreJsPath,
path.join(distDir, `tiddlywikicore-${($tw as any).packageInfo.version}.js`),
);

// 将所有的二进制文件(媒体文件, 参考$tw.utils.registerFileType)导出
const binaryExtensionMap: Record<string, boolean> = {};
Expand All @@ -156,31 +156,6 @@ const buildOnlineHTML = async (
// 恢复被清空内容的媒体文件
tryCopy(backupFolder, tiddlersFolder);
fs.rmSync(backupFolder, { recursive: true, force: true });

// 最小化:核心JS和HTML
if (minify) {
// 核心
const { code } = UglifyJS.minify(
fs.readFileSync(rawCoreJsPath).toString('utf-8'),
{
warnings: false,
ie8: true,
webkit: true,
},
);
fs.writeFileSync(rawCoreJsPath, code);
// 网页
const rawHTMLPath = path.join(distDir, htmlName);
const html = await htmlMinify(
fs.readFileSync(rawHTMLPath).toString('utf-8'),
htmlMinifierOptions,
);
fs.writeFileSync(rawHTMLPath, html);
}
fs.renameSync(
rawCoreJsPath,
path.join(distDir, `tiddlywikicore-${($tw as any).packageInfo.version}.js`),
);
};

/**
Expand All @@ -193,7 +168,6 @@ const buildOnlineHTML = async (
const buildOfflineHTML = async (
dist = 'dist',
htmlName = 'index.html',
minify = true,
excludeFilter = '-[is[draft]]',
) => {
const distDir = path.resolve(dist);
Expand All @@ -215,17 +189,8 @@ const buildOfflineHTML = async (
excludeFilter,
] /* 将wiki导出为HTML */,
]);

// 最小化:HTML
const rawHTMLPath = path.join(distDir, htmlName);
await waitForFile(rawHTMLPath);
if (minify) {
const result = await htmlMinify(
fs.readFileSync(rawHTMLPath).toString('utf-8'),
htmlMinifierOptions,
);
fs.writeFileSync(rawHTMLPath, result);
}
// 由于导出是异步的,因此等待完成
await waitForFile(path.join(distDir, htmlName));
};

/**
Expand All @@ -237,7 +202,6 @@ const buildOfflineHTML = async (
const buildLibrary = async (
pluginFilter = '[prefix[$:/plugins/]!prefix[$:/plugins/tiddlywiki/]!prefix[$:/languages/]!prefix[$:/themes/tiddlywiki/]!tag[$:/tags/PluginLibrary]]',
dist = 'dist/library',
minify = true,
) => {
const distDir = path.resolve(dist);

Expand Down Expand Up @@ -270,16 +234,8 @@ const buildLibrary = async (
] /* 删掉中间内容 */,
]);

// 最小化:HTML
const rawHTMLPath = path.resolve(distDir, 'index.html');
await waitForFile(rawHTMLPath);
if (minify) {
const result = await htmlMinify(
fs.readFileSync(rawHTMLPath).toString('utf-8'),
htmlMinifierOptions,
);
fs.writeFileSync(rawHTMLPath, result);
}
// 由于导出是异步的,因此等待完成
await waitForFile(path.join(distDir, 'index.html'));
};

program
Expand All @@ -291,7 +247,6 @@ program
'Filter to exclude publishing tiddlers',
'-[is[draft]]',
)
.option('--minify', 'Should minify generated files', true)
.option(
'--offline',
'Generate single wiki file, with an external core js file',
Expand All @@ -300,16 +255,12 @@ program
.action(
async (
dist: string,
{
offline,
minify,
excludeFilter,
}: { offline: boolean; excludeFilter: string; minify: boolean },
{ offline, excludeFilter }: { offline: boolean; excludeFilter: string },
) => {
if (offline) {
buildOfflineHTML(dist, 'index.html', minify, excludeFilter);
buildOfflineHTML(dist, 'index.html', excludeFilter);
} else {
buildOnlineHTML(dist, 'index.html', minify, excludeFilter);
buildOnlineHTML(dist, 'index.html', excludeFilter);
}
},
)
Expand Down
35 changes: 9 additions & 26 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
"reset": "rimraf ./**/node_modules",
"clean": "rimraf dist",
"prepare": "husky install",
"dev": "ts-node scripts/index dev",
"build": "rimraf dist && ts-node scripts/index build",
"build:library": "rimraf dist && ts-node scripts/index build --library",
"publish": "rimraf dist && ts-node scripts/tw && ts-node scripts/index build --library",
"publish:offline": "rimraf dist && ts-node scripts/tw --offline && ts-node scripts/index build --library"
"dev": "tiddlywiki-plugin-dev dev",
"build": "rimraf dist && tiddlywiki-plugin-dev build",
"build:library": "rimraf dist && tiddlywiki-plugin-dev build --library --output dist/library",
"inject": "tiddlywiki-plugin-dev build --output wiki/tiddlers",
"publish": "npm run build:library && npm run inject && ts-node .scripts",
"publish:offline": "npm run build:library && npm run inject && ts-node .scripts --offline"
},
"engines": {
"node": ">=16"
Expand All @@ -26,39 +27,21 @@
"devDependencies": {
"@modern-js/app-tools": "^1.21.5",
"@modern-js/tsconfig": "^1.21.5",
"@types/clean-css": "^4.2.6",
"@types/cli-progress": "^3.11.0",
"@types/html-minifier-terser": "^7.0.0",
"@types/lodash": "^4.14.190",
"@types/node": "^18.11.9",
"@types/uglify-js": "^3.17.1",
"@types/ws": "^8.5.3",
"browserslist": "^4.20.3",
"chokidar": "^3.5.3",
"clean-css": "^5.3.0",
"esbuild": "^0.14.38",
"esbuild-plugin-browserslist": "^0.4.9",
"fs-extra": "^10.1.0",
"html-minifier-terser": "^7.1.0",
"husky": "^8.0.2",
"lint-staged": "^13.0.4",
"rimraf": "^3.0.2",
"ts-node": "^10.9.1",
"tsconfig-paths": "^3.14.1",
"tslib": "^2.4.0",
"tw5-typed": "^0.2.7",
"typescript": "^4.6.4",
"uglify-js": "^3.15.4",
"ws": "^8.6.0"
"typescript": "^4.6.4"
},
"dependencies": {
"ansi-colors": "^4.1.3",
"chalk": "^5.1.2",
"cli-progress": "^3.11.2",
"commander": "^9.4.1",
"get-port": "^6.1.2",
"get-port-please": "^2.6.1",
"lodash": "^4.17.21",
"tiddlywiki": "^5.2.3"
"tiddlywiki": "^5.2.3",
"tiddlywiki-plugin-dev": "^0.0.4"
}
}
Loading

0 comments on commit 3208be9

Please sign in to comment.