From e1840afda6ad2418bc1209539cbdbcc9d14883e4 Mon Sep 17 00:00:00 2001 From: jaywcjlove <398188662@qq.com> Date: Sat, 10 Aug 2024 20:16:57 +0800 Subject: [PATCH] feat: add `--copy-pattern` flag. --- core/README.md | 21 ++++++++++++++++++++- core/src/build.mts | 8 ++++++-- core/src/cli.mts | 5 +++++ core/src/help.mts | 1 + examples/template/img/demo.webp | 0 5 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 examples/template/img/demo.webp diff --git a/core/README.md b/core/README.md index 2325acf..c63449e 100644 --- a/core/README.md +++ b/core/README.md @@ -48,6 +48,7 @@ Options: -c, --close-delimiter Use CHARACTER instead of right angle bracket to close. -f, --data-file FILE Must be JSON-formatted. Use parsed input from FILE as data for rendering --rm-whitespace Remove all safe-to-remove whitespace, including leading and trailing + --copy-pattern Use shell patterns to match the files that need to be copied. Examples: @@ -57,7 +58,7 @@ Examples: $ ejsc "template/**/*" --watch $ ejs-cli "template/*.ejs" --watch --out build -Copyright 2023 +Copyright 2024 ``` ## Match files @@ -277,9 +278,27 @@ export default { age: 36, }, }, + /** + * Use shell patterns to match the files that need to be copied. + * @default "/**\/*.{css,js,png,jpg,gif,svg,webp,eot,ttf,woff,woff2}" + */ + copyPattern: "", + /** + * Pre-Save HTML Callback Method + * @param html + * @param output + * @param filename + * @returns + */ beforeSaveHTML: (html, output, filename) => { return minify(html, options); }, + /** + * Callback method after copying files. + * @param filepath + * @param output + * @returns + */ afterCopyFile: (filePath, outputPath) => { if (filePath.endsWith(".js")) { const result = UglifyJS.minify(fs.readFileSync(outputPath, "utf-8")); diff --git a/core/src/build.mts b/core/src/build.mts index bcc9e3e..1356471 100644 --- a/core/src/build.mts +++ b/core/src/build.mts @@ -44,6 +44,11 @@ export interface Options extends EjsOptions { * @returns */ afterCopyFile?: (filepath: string, output: string) => void; + /** + * Use shell patterns to match the files that need to be copied. + * @default "/**\/*.{css,js,png,jpg,gif,svg,webp,eot,ttf,woff,woff2}" + */ + copyPattern?: String; } export async function build( @@ -55,10 +60,9 @@ export async function build( entry.forEach((filePath) => { toHTML(filePath, output, ejsData, ejsOption); }); - // 拷贝静态资源 const dirs = [...new Set(getRootDirsAll(entry))].map((dir) => { - return dir + `/**/*.{css,js,png,jpg,gif,svg,eot,ttf,woff,woff2}`; + return dir + options.copyPattern; }); const data = await glob(dirs, { ignore: "node_modules/**" }); data.forEach((filePath) => { diff --git a/core/src/cli.mts b/core/src/cli.mts index 2cf87a3..f306137 100644 --- a/core/src/cli.mts +++ b/core/src/cli.mts @@ -51,6 +51,10 @@ import { helpStr } from "./help.mjs"; shortFlag: "f", type: "string", }, + copyPattern: { + type: "string", + default: "/**/*.{css,js,png,jpg,gif,svg,webp,eot,ttf,woff,woff2}", + }, }, }); @@ -86,6 +90,7 @@ import { helpStr } from "./help.mjs"; openDelimiter: cli.flags.openDelimiter, closeDelimiter: cli.flags.closeDelimiter, rmWhitespace: cli.flags.rmWhitespace, + copyPattern: cli.flags.copyPattern, globelData: {}, data: {}, }; diff --git a/core/src/help.mts b/core/src/help.mts index 42a2f1b..2facfc4 100644 --- a/core/src/help.mts +++ b/core/src/help.mts @@ -12,6 +12,7 @@ Options: \x1b[35;1m-c, --close-delimiter\x1b[0m Use CHARACTER instead of right angle bracket to close. \x1b[35;1m-f, --data-file FILE\x1b[0m Must be JSON-formatted. Use parsed input from FILE as data for rendering \x1b[35;1m--rm-whitespace\x1b[0m Remove all safe-to-remove whitespace, including leading and trailing + \x1b[35;1m--copy-pattern\x1b[0m Use shell patterns to match the files that need to be copied. Examples: diff --git a/examples/template/img/demo.webp b/examples/template/img/demo.webp new file mode 100644 index 0000000..e69de29