Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Require dynamic path synchronously

Tags:

webpack

I want to require a file. Its path is in some config:

//config.js
module.exports = "/home/css/style.css";

//entry.js
var path = require(./config.js);
require("style!css!" + path);

Uncaught Error: Cannot find module "."

How can I require files with path that can take root / directory?

like image 372
mqklin Avatar asked Jan 06 '16 14:01

mqklin


1 Answers

You'll have to use require.ensure
For ex.:

function handleRouteChange(path) {
    require(['/views/' + path], function(PageView) {
        app.setView(new PageView());
    });
} 

changes to:

function loadPage(PageView) {
    app.setView(new PageView());
}
function handleRouteChange(path) {
    switch (path) {
        case 'settings':
            require(['/views/settings'], loadPage);
            break;
        case 'transfer':
            require(['/views/transfer'], loadPage);
            break;
        case 'wallet':
            require(['/views/wallet'], loadPage);
            break;
    default:
        // you could either require everything here as a last resort or just leave it...
    }
}

Source: https://gist.github.com/xjamundx/b1c800e9282e16a6a18e#the-routing-code-old-and-new

like image 165
Abhinav Singi Avatar answered Nov 17 '22 22:11

Abhinav Singi