deathangel908
deathangel908

Reputation: 9699

Dynamic import text files in webpack

Typically when we need to import some raw string we use raw-loader. The common use case is the following:

  1. In the source code we add:
let data = (await import(/* webpackChunkName: "a.txt" */"./a.txt")).default;
console.log(a.default) 
  1. Will generate file a.txt.js that will contain:
(window.webpackJsonp=window.webpackJsonp||[]).push([[1],{37:function(e,n){e.exports='CONTENT_OF_TXT_FILE'}}]);
  1. The source code where we do import will produce things that would add <script charset="utf-8" src="/a.txt.js"></script> in the HTML HEAD section.

  2. After the browser evaluates both scripts, the webpack magic would make it look like the es6 import.

Fine, this is how es6 imports typically look like. And raw-loader looks like a hack with dynamic imports. BUT is there to just copy a.txt to the destination directory instead of making it an es6 module? I want an elegant solution w/o writing some nasty wrappers with XmlHttlRequest. Is there such a plugin in the webpack?

There're some other similar tools:

Upvotes: 2

Views: 3680

Answers (0)

Related Questions