Skip to content

Inline local JS imports #41

Description

@dave-db

Is it possible to configure a transform to inline the content of other local files imported from an included js chunk?

e.g.

// js/main.js
import env from '../_data/env.js';

// some js code that references env values
<!-- layouts/base.njk -->
...
<script type="module">{% include "js/main.js" %}</script>
...

When the bundles are processed, every page includes the main.js, which (at runtime, in the browser) tries to import _data/env.js and errors.

I've had a go at adding a transform to the .addBundle('js') call to use a bundler to inline imports, but of course they're being run against the final page path (eg /en/index.njk), so resolving paths is complex, and maybe this is the wrong place to be doing it?

Either way, it would be great if there was a quick way to handle local imports so config and constants can be shared around without needing to copy and paste. Have I missed something obvious?

Metadata

Metadata

Assignees

No one assigned
    No fields configured for Education.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions