3

I want to create a webpack loader which can load optional dependencies without a try/catch.

When I'm using it like this

import optional from 'file-exists-loader!./optional-file';` 

webpack causes an module not found error. But I want to prevent this in my loader and return an empty object for example.

This would be my loader code:

var fs = require('fs');
var emptyExport = 'module.exports = {}';

module.exports = function(content) {
    if (!fs.existsSync(this.resourcePath)) {
        return emptyExport;
    }

    return content;
};

I know that I could use this also with a query

import optional from 'file-exists-loader?file=./optional-file!';

but then I have no context where this file will be loaded?

Does anyone know what I can do else?

Kaufmo
  • 63
  • 8

0 Answers0