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

Can I push a file to plugin output file list? #837

Closed
ym-project opened this issue Feb 18, 2021 · 3 comments
Closed

Can I push a file to plugin output file list? #837

ym-project opened this issue Feb 18, 2021 · 3 comments

Comments

@ym-project
Copy link
Contributor

ym-project commented Feb 18, 2021

Hi!
Does exist an opportunity to push file to output file list via plugin API?
I have a plugin to transform stylus to css files. And I want to generate external sourcemaps.

onLoad({}, () => {
    // ...
    const result = await stylusToCss(stylusContent)
    
    if (result.sourcemap) {
        await fs.writeFile(fileName + '.map', JSON.stringify(result.sourcemap, null, 2))
    }
    
    return {
        contents: result.css,
        loader: 'css',
    }
})

I have 2 problems:

  1. I don't see external sourcemap in output file list (but let's skip this);
  2. When esbuild has an option "write: false" sourcemap will be generated anyway (critical case).

I see 2 solutions for this problem.

  1. Get access to esbuild user options to know value of option "write: false/true" (as workaround);
  2. Get an opportunity to return an array of files.

What do you think about this?

@evanw
Copy link
Owner

evanw commented Feb 19, 2021

Not yet. But that might not be what you want here. If you were to use the plugin proposed above where you write source maps to the file system outside of esbuild's view like this, the source maps would be broken because they would be referencing offsets in the input file instead of the output file.

There are several things here including:

  1. Plugins should be able to access options (tracked by Give plugins access to build options #373)
  2. Source map support for CSS (tracked by sourcemap support for CSS #519)

I think the real solution for this would be for esbuild to support source maps. Then you could pass the source map along with /*# sourceMappingURL=data:... */ and esbuild would transform it along with the CSS (and bundle multiple source maps into one when bundling). This is how generating source maps in JavaScript plugins works with esbuild. Unfortunately CSS support in esbuild is still early and CSS source maps aren't working yet. You can follow issue #519 for progress on that front.

@ym-project
Copy link
Contributor Author

Maybe in my case sourcemap support will resolve the problem. But there are situations where developer would like to create additional files for correct file transform.
So if you implement an opportunity to return array of files, it will be awesome.

@evanw
Copy link
Owner

evanw commented Jun 28, 2021

You can now do this using an onEnd callback: https://esbuild.github.io/plugins/#end-callbacks. Closing.

@evanw evanw closed this as completed Jun 28, 2021
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

2 participants