Skip to content

build.onEnd is not a function - in custom plugin setup() {...} #77

@rw3iss

Description

@rw3iss

I'm trying to make a quick plugin to basically do sipel template-parsing/replacement in specific types of files (ie. HTML files - in order to add a hash string to the end of static assets urls).

Anyway, I'm trying to utilize the onBuild hook to manually generator the output file, which the docs refer to here:
https://esbuild.github.io/plugins/#caching-your-plugin

However, when running, it's saying it doesn't exist on the build object:

transform setup { onResolve: [Function: onResolve], onLoad: [Function: onLoad] }
 > C:\Users\rw3is\Sites\adcloud\app\scripts\build.js:48:14: error: build.onEnd is not a function
    48 │         build.onEnd(result => {
       ╵               ^
    at setup (C:\Users\rw3is\Sites\adcloud\app\scripts\build.js:48:15)
    at handlePlugins (C:\Users\rw3is\Sites\adcloud\app\node_modules\esbuild\lib\main.js:659:7)

1 error
Error building html: Build failed with 1 error:

The code is essentially:

let transformHtmlTemplatePlugin = {
    name: 'transform',
    setup(build) {
        let fs = require('fs')

        console.log('transform setup', build)

        // Load ".txt" files and return an array of words
        build.onLoad({ filter: /\.html$/ }, async (args) => {
            let text = await fs.promises.readFile(args.path, 'utf8');
            console.log('html file', text);
            text = text.replace('{TEST}', 'THIS IS TEST');
            return {
                contents: text,
                loader: 'text',
            }
        })
        
       //... more ommitted
}

And the build process is:


function buildHtml(entryFile, outFile) {
    esbuild.build({
        entryPoints: [entryFile],
        outfile: outFile,
        target: TARGET,
        logLevel: 'verbose',
        // loader: { // built-in loaders: js, jsx, ts, tsx, css, json, text, base64, dataurl, file, binary
        //     '.html': 'file'
        // },
        define,
        plugins: [transformHtmlTemplatePlugin]
    })
        .then(r => { console.log(`Build html ${entryFile} to ${outFile} succeeded.`) })
        .catch((e) => {
            console.log("Error building html:", e.message);
            process.exit(1)
        })
}

Is there anything wrong with this approach, or is there a reason why onBuild doesn't exist on the plugin's build argument?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions