To exclude js files from a build

Hi, im korean develper.

I made a javascript file based on nodejs for convenience of development. It is also in the asset folder and cannot be rerouted due to personal reasons.

My problem is, this js file doesn’t need to be included in the build, but it tries to be included in the build and causes an error. Is there a way to exclude the javascript file in the asset folder from the build?

Hi, you can try to wrap up your js code in defines as you needed


if (!CC_BUILD) {
// your js code here
}

I don’t want to modify the code, since there’s no part that calls the js file, I want to ignore it in the build.

Then you could modify a build process via an editor extension that you need to create.

I agree with you, I was already working on the process

// hooks.ts
import * as fs from 'fs';

function moveFile(from: string, to: string) {
    console.log('from : ' + from + ', to : ' + to);
    let dirTemp = to.split('\\');
    dirTemp.pop();
    let dir = dirTemp.join('\\');
    !fs.existsSync(dir) && fs.mkdirSync(dir);
    fs.copyFileSync(from, to);
    fs.unlinkSync(from);
}

export async function onBeforeBuild(options) {
    let pack = options['packages'] || {};
    let module = pack['build-ignore-files'] || {};
    let list = (module['ignoreList'] || '').split(';');
    for (let i = list.length - 1; i >= 0; i--) {
        list.push(list[i] + '.meta');
    }
    for (let path of list) {
        console.log(path + ' is exist : ' + fs.existsSync(path).toString());
        if (path === '' || !fs.existsSync(path)) {
            continue;
        }
        moveFile(path, Editor.Project.path + '\\' + path.split('\\').pop());
    }
}

export async function onAfterBuildAssets(options) {
    console.log('after_build');
    let pack = options['packages'] || {};
    let module = pack['build-ignore-files'] || {};
    let list = (module['ignoreList'] || '').split(';');
    for (let i = list.length - 1; i >= 0; i--) {
        list.push(list[i] + '.meta');
    }
    for (let path of list) {
        let tempPath = Editor.Project.path + '\\' + path.split('\\').pop();
        if (tempPath === '' || !fs.existsSync(tempPath)) {
            continue;
        }
        moveFile(tempPath, path);
    }
}

export function load() {
    console.log('build-ignore-files load');
}

export function unload() {
    console.log('build-ignore-files unload');
}

This is my code.

Also, if you proceed with the build, the file will be deleted

However, there seems to be a problem with asset db. The file has been erased, but it is still about to be included in the build

What should i do?

seems you need to refresh/update assets db, probably is better delete file via assets-db API directly instead of FileSystem API.

I also considered that, but I’m using Cocos version 3.6 and I don’t have Editor.assetdb in 3.6 How can I use the assetdb?

there is new Message system could help you i guess.

https://docs.cocos.com/creator/manual/en/editor/extension/package.html