Search code examples
javascriptbundleservice-workerparceljs

Parceljs import javascript file as string


I have a very particular use case. I want to import a javascript file as a string and inject it into html responses at will in a service worker. I can't see how to do this using parceljs beyond hosting the javascript file somewhere and doing fetch at runtime to load the js file into memory. However, I want to do this at build time. How best to do this?

Note: Ideally the dependencies of javascript file I am importing should be bundled into the string.


Solution

  • Seems to be possible in parcel 2 with

    import js from "bundle-text:./b.ts";
    
    console.log(js);
    

    https://v2.parceljs.org/configuration/plugin-configuration/#predefined-(offical)-named-pipelines