Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
rw3iss opened this issue Sep 16, 2021 · 0 comments
Open

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

rw3iss opened this issue Sep 16, 2021 · 0 comments

Comments

@rw3iss
Copy link

rw3iss commented Sep 16, 2021

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant