Skip to content

A method of adding an import based on dependency graph #3790

@benmccann

Description

@benmccann

Feature Use Case

Sapper has a CSS-handling plugin, which has recently been significantly cleaned up and is about ready to graduate into a separate Rollup plugin for wider community use:

https://github.com/sveltejs/sapper/blob/ead10eebb7416971a390a0190f1df7c1f3327791/src/core/create_compilers/RollupCompiler.ts#L156

It finds CSS that a dynamically imported chunk tree includes and then rewrites the dynamic imports with an extra bit of code. That code makes it so that in parallel to the original dynamic import we also write link tags pointing to the CSS.

So as not to duplicate code across chunks, this plugin emits a new inject_styles chunk. It then adds an import to that chunk where it's used. The problem here is that this import does not show up in the chunk.imports for the plugins that follow it.

Feature Proposal

Things I've tried:

  • I've tried doing chunk.imports.push(newImport) when adding the import in generateBundle. This does not persist for the next plugin
  • I've tried rewriting the code in transform. This does not work because I don't have access to the dependency graph. When I call this.getModuleInfo it returns empty arrays for the importedIds and dynamicallyImportedIds
  • I've tried rewriting the code in renderChunk. However, I don't know the file name to add. If I simply do import __inject_styles from 'inject_styles.js' then that doesn't get properly rewritten to include the hash in the filename.

It would be nice if at least one of these worked or there was another solution to this problem

Related Requests

#3519

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