Search code examples
requireecmascript-6webpackecmascript-2016

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 exists. how do I do it (including using of loaders for example).


Solution

  • What I ended doing was improving the imports loader to add an option to import a less file for every jsx file with the same name if it exists.

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

    The pull request: https://github.com/webpack/imports-loader/pull/12

    For example dropping mainview.less in the same directory as mainview.jsx, would add a require("mainview.less") import to the top of the jsx file:

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