Search code examples
node.jsangularrequirewebpack-file-loader

Require behaves differently in Angular project


If to write something like const theoretically = require('jasmine-theories');, require returns the content of the file. But if to set declare var require: any;, then next **require** executions steps inside webpack bootstrap function __webpack_require__(moduleId) and return real file path with hash, not content (for example 'file.65465436547.js').

I've found out that file-loader has such behavior https://www.npmjs.com/package/file-loader .

I may assume that depending on declare var require: any; require is taken from NodeJS or from File-loader.

Is that correct? And is there more obvious way how and when to use each of them?

And how can I config File-loader to behave another way in case of Angular application? Angular CLI doesn't provide webpack.config, therefore loaders just get installed without any configuration possible.

Overall the question can be shortened to:

Why require returns content in one case and file name in another case?


Solution

  • It looks that before declare var require: any; default method is used from Node JS. But after explicit declaring FileLoader's method becomes visible. And as it has default exporting - require usage is switched to FileLoader's one.