rna icon indicating copy to clipboard operation
rna copied to clipboard

Weird behavior when not using `hash`

Open CodexHere opened this issue 2 years ago • 0 comments

When I don't set chunkNames to include [hash] it seems my JS file is written with a module importing the entryfile (my index.html) instead of the expected ESM output:

// src/index.html
var src_default = "./index.html";
export {
  src_default as default
};
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFtdLAogICJzb3VyY2VzQ29udGVudCI6IFtdLAogICJtYXBwaW5ncyI6ICIiLAogICJuYW1lcyI6IFtdCn0K

Here's the options that produce this output:

buildOptions: {
    bundle: true,
    metafile: true,
    minify: false,
    outdir: '/app/dist',
    platform: 'browser',
    sourcemap: 'inline',
    target: 'esnext',
    assetNames: '[name]',
    chunkNames: '[name]',
    format: 'esm',
    logLevel: 'debug',
    entryPoints: [ '/app/src/index.html' ],
    external: [],
    loader: { '.png': 'file', '.svg': 'file', '.ts': 'ts', '.tsx': 'tsx' }
}

However if I set chunkNames: '[ext]/[name]-[hash]' it outputs the js file as an ESM as expected.

In my setup, I have a conditional check so that it's just [name] if NODE_ENV == 'development' and [ext]/[name]-[hash] if it's set to production. I'm not sure what the importance of the hash is, and while the README does show it being used it doesn't explicitly state that it's necessary.

What's more, is I get weird build output from esbuild, and it looks like it's double processing the index.ts file:

(without hash)

  dist/app.css  1.2kb                                                                                                                                                                                                                         
                                                                                                                                                                                                                                              
⚡ Done in 2ms                                                                                                                                                                                                                                
                                                                                                                                                                                                                                              
  dist/index.js  10.6mb ⚠️                                                                                                                                                                                                                     
                                                                                                                                                                                                                                              
⚡ Done in 296ms                                                                                                                                                                                                                              
                                                                                                                                                                                                                                              
  dist/index.html  957b                                                                                                                                                                                                                       
  dist/index.js    264b                                                                                                                                                                                                                       
                                                                                                                                                                                                                                              
⚡ Done in 625ms    

(with hash)

  dist/css/app-V3MYB2PJ.css  1.2kb                                                                                                                                                                                                            
                                                                                                                                                                                                                                              
⚡ Done in 2ms                                                                                                                                                                                                                                
                                                                                                                                                                                                                                              
  dist/js/index-QJUXMFXL.js  10.6mb ⚠️                                                                                                                                                                                                         
                                                                                                                                                                                                                                              
⚡ Done in 294ms                                                                                                                                                                                                                              
                                                                                                                                                                                                                                              
  dist/index-HXAY6RKV.html  1.0kb                                                                                                                                                                                                             
  dist/index.js             273b     

The 10.6mb (it includes inline sourcemaps, so it's big) output file is the appropriate build, and while the MUCH smaller build appears after, it only works as expected in the hashed output, and I never see a hashed index file (which is preferred, but weird it shows it being built).

Anyway, I'm going to have to hard-code the hash for now, but it's all weird compared to other plugins so I wanted to point it out, and hopefully something can be clarified out of it.

CodexHere avatar Jun 25 '23 04:06 CodexHere