Skip to content

Commit

Permalink
feat: add --copy-pattern flag.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Aug 10, 2024
1 parent 9060433 commit e1840af
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
21 changes: 20 additions & 1 deletion core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -57,7 +58,7 @@ Examples:
$ ejsc "template/**/*" --watch
$ ejs-cli "template/*.ejs" --watch --out build

Copyright 2023
Copyright 2024
```

## Match files
Expand Down Expand Up @@ -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"));
Expand Down
8 changes: 6 additions & 2 deletions core/src/build.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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) => {
Expand Down
5 changes: 5 additions & 0 deletions core/src/cli.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
},
},
});

Expand Down Expand Up @@ -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: {},
};
Expand Down
1 change: 1 addition & 0 deletions core/src/help.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Empty file added examples/template/img/demo.webp
Empty file.

0 comments on commit e1840af

Please sign in to comment.