Optional dependencies in webpack

for example, if I want to add require("index.less") to all files and ignore this line if the file does not exist. how to do it (including, for example, using bootloaders).

+6
source share
2 answers

What I ended up with was improving the bootloader import to add an option to import a less file for each jsx file using the same name if it exists.

My improved import loader : https://github.com/welldone-software/imports-loader

Stretch Request: https://github.com/webpack/imports-loader/pull/12

For example, removing mainview.less in the same directory as mainview.jsx will add require("mainview.less") imports to the top of the jsx file:

 loaders: [ { test: /\.jsx?$/, loaders: ['imports?null=[./{name}.less]', 'react-hot', 'babel'] }, { test: /\.less$/, loader: 'style!css!less' } ] 
+1
source

One option is to configure require.context , and then check if the file exists against this.

Tough idea:

 var req = require.context('./', false, /^index.less$/); if(req.keys().includes('./index.less')) { req('./index.less'); } 
+5
source

All Articles