Search code examples
importecmascript-6webpackes6-module-loaderraw-loader

Import a cert file as a string using webpack


I was really hoping I'd be able to use webpack to load a certificate file containing text using the raw-loader. Unfortunately it fails at the -'s in the first line: -----BEGIN CERTIFICATE-----.
As a test I removed the first -----, and then it fails at a " " (space) character.

Seemed like a much simpler solution than using fs and a callback.

To clarify, i'd like to be able to do this:

import caCert from './cacert';

Solution

  • If you want to load some file via loader, add loader name as prefix to your import:

    import caCert from 'raw!./cacert';
    

    Also, you can setup your loader in webpack config to match appropriate files by their names

    module: {
      loaders: [
        {
          test: /cacert$/,
          loaders: [ 'raw-loader' ]
        }
      ]
    }