Reputation: 9699
Typically when we need to import some raw string we use raw-loader
. The common use case is the following:
let data = (await import(/* webpackChunkName: "a.txt" */"./a.txt")).default;
console.log(a.default)
a.txt.js
that will contain:(window.webpackJsonp=window.webpackJsonp||[]).push([[1],{37:function(e,n){e.exports='CONTENT_OF_TXT_FILE'}}]);
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.
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:
file-loader
- copies a file into a dist directory and returns a file name. THis thing still requires some wrappers and doesn't look that elegant as es6-module.html-loader
- supports only html by parsing it and looking for src
attributes. Upvotes: 2
Views: 3680