Skip to content

Provide an way to modify Emited chunk in generateBundle | writeBundle hooks #4210

@hardfist

Description

@hardfist

Feature Use Case

Given that rollup's bundle speedy is not fast, I want to leave third party module bundle to esbuild(and rollup bundle user code), considering supports code splitting, I need to modify the emited chunk in generateBundle hooks, which seem not supported yet

    async generateBundle(options, chunkMap) {
      const entryPoints = {};
      for (const [file, chunk] of Object.entries(chunkMap)) {
        if (file.endsWith('.js')) {
          entryPoints[path.resolve(config.root, file)] = chunk;
        }
      }
      const context = this;
      const result = await build({
        entryPoints: Object.keys(entryPoints),
        write: false,
        absWorkingDir: __dirname,
        bundle: true,
        outdir: '/assets',
        format: 'esm',
        allowOverwrite:true,
        splitting:true,
        plugins: [
          {
            name: 'memfs',
            setup(build) {
              build.onResolve({ filter: /.*/ },async (args) => {
                const realPath = path.resolve(args.resolveDir, args.path);
                if (entryPoints[realPath]) {
                  return {
                    path: realPath
                  };
                }else {
                  const result = await context.resolve(args.path, args.importer, {
                    skipSelf:true
                  })
                  return {
                    path: result.id
                  }
                }
              });
              build.onLoad({ filter: /.*/ }, (args) => {
                const res = entryPoints[args.path];
                if (res) {
                  return {
                    contents: entryPoints[args.path].code,
                    resolveDir: path.dirname(args.path)
                  };
                }
              });
            }
          }
        ]
      });
      for(const output of result.outputFiles){
        const fileName= path.relative('/', output.path);
        console.log('fileName:', fileName);
        // rollup not support emitChunk in generate bundle,which is hard to deal with
        this.emitFile({
          type: 'chunk', // support modify 'asset' but not support modify 'chunk'
          fileName: fileName,
          source: output.text
        })
      }
    }

Feature Proposal

provide an hook likes updateFile to modify emited chunk & asset

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions