Delete assets in build template

Environment
OS : MacOS 10.15.7
CocosCreator : 3.3.1

We want to use a build template to exclude certain assets from the build result.
The code to exclude specific assets is implemented in hooks.ts which is generated when the build template is deployed.
If we delete the corresponding file in the onBeforeBuild() method of hooks.ts, the build will stop midway.
From the error message, I found out that it was trying to read the information of the deleted file from under project/lib/.

This method worked in v3.1.0, but it no longer works in v3.3.1.

Here are the steps to reproduce it.

Translated with www.DeepL.com/Translator (free version)


・Introduce the build template from the menu.
[Project] > [Generate Builder Extention]

・Enable cocos-build-temprate.
[Extention] > [Extention Manager]

・Modify generated project/extensions/cocos-build-template/source/hooks.ts to delete specific files in onBeforeBuild().

export async function onBeforeBuild(options: ITaskOptions) {
    const srcPath = `${Editor.Project.path}/assets/CheckScene.scene`;
    const srcMetaPath = `${Editor.Project.path}/assets/CheckScene.meta`;
    fs.unlinkSync(srcPath);
    fs.unlinkSync(srcMetaPath);
}

・Compile hooks.ts with npx tsc.

$ pwd
$ ...project/extensions/cocos-build-template/
# npx tsc 

・Build the project from the console.
/Applications/CocosCreator/Creator/3.3.1/CocosCreator.app/Contents/MacOS/CocosCreator --project …/build-template-test/project --build “configPath=…/build-template-test/buildConfig_web-desktop.json;”

・The following error is output.

2021-10-15 12:53-error: {"errno":-2,"code":"ENOENT","syscall":"open","path":"...build-template-test/project/library/a9/a90df829-593d-42d3-af14-114e7652f3f8.json"}

2021-10-15 12:53-debug: ENOENT: no such file or directory, open '...build-template-test/project/library/a9/a90df829-593d-42d3-af14-114e7652f3f8.json'10%

The error a90df829-593d-42d3-af14-114e7652f3f8.json was the meta-information of CheckScene.scene.

Awesome. Let me have engineering review this.

Hi, if you want to delete a scene at build time, then you should uncheck that build scene. This way the editor will not look for this scene resource when building.
图片

Thanks slackmoehrle !

hi zzf520
Thanks for the response.

I unchecked the scene and built it and it worked fine.

In the future, I’d like to pass arguments from the command line at build time to remove assets under various conditions.
In short, I want to delete various assets at build time without manually manipulating the CocosCreator screen.

I want CocosCreator to do the following when I delete a file.

  • How to make CocosCreator rebuild the asset-db after deleting the corresponding file with fs.unlinkSync().
  • How to operate CocosCreator from build-template and delete the assets successfully.

Is it possible to do this kind of operation?