D
D
d-virt2016-06-30 21:38:27
JavaScript
d-virt, 2016-06-30 21:38:27

How to make dynamic require (webpack)?

Hello.
Unfortunately, the nerves are already at the limit ..... Please, tell me, please ....
There are two entry points:

entry: {
        Api : './modules/Api/resources/assets/frontend/app/index',
        Test : './modules/Test/resources/assets/frontend/app/index'
    }, 
output: {
        path: path.resolve(__dirname, 'public/js'),
        filename: '[name].js',
        chunkFilename: '[name].js',
        publicPath: '/js/'
    },
resolve : {
        alias : {
            Api : path.resolve(__dirname, 'modules/Api/resources/assets/frontend/app/index'),
            Test : path.resolve(__dirname, 'modules/Test/resources/assets/frontend/app/index')
        }
    }

PS the main thing is to pay attention to the paths in the entry!
From the Api.js module, I want to include Test.js,
BUT with
var name = 'Test';

require.ensure([], require => {
    require("./" + name);
});

Could you please clarify how this can be done?
PS I know there is a context, but alas it doesn't work like this:
var req = require.context('bundle!./../../../../../', false, /^\.\/.*\.js$/);
req("./" + name + "/resources/assets/frontend/app/require");

I tried url:
require("url?./" + name);
it doesn't work...
Alas, yes, it works like this (code from Api.js):
require.ensure([], require => {
        require('./../../../../../Test/resources/assets/frontend/app');
    });

But, and if I delete the folder ("./modules/Test"), then there will be an error during compilation, and I would like to avoid this. No matter how there is no module, well, to hell with it, let what is work.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
_
_ _, 2016-06-30
@d-virt

If you have registered alias, then just write

require.ensure(['Test'], require => {
    require('Test');
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question