Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to require JavaScript using webpack from a dynamic directory

Tags:

I have some old-school plain old ES5 JavaScript that I need to pull into my application that is using webpack as a bundler. The problem is that the location of these files is in a folder whose path is dynamic. It seems that webpack does not work well with expressions in require and require.context doesn't allow them at all.

For known path directories to require all the files in a directory and its subdirectories, this approach works fine:

let domainRequires = require.context('./app/domain', true, /\.js$/); domainRequires.keys().forEach(domainRequires);

Since I can't use an expression in require.context, I tried just using plain require and it is NOT working:

require('../../path/to/code/my-library-' + versionNumber + '/domain/clazz.js');

using the full relative path.

I've also tried to use require.context but enhancing the regex portion to get it working and haven't had luck:

require.context('../../path/to/code', true, /^my-library-.*\/domain\/.*\.js$/);

Any thoughts, suggestions, or solutions would be welcome. If I need to use something 3rd party with webpack, that's fine too.

like image 230
Alex White Avatar asked Nov 04 '16 15:11

Alex White


People also ask

How do I include a JavaScript file in webpack?

You can use webpack-inject-plugin to inject any JS code as string into the resulting . js bundle created by webpack. This way you can read the File you want to inject as a string (e.g. fs. readFile in nodejs) and inject it with the plugin.

How import works in webpack?

The imports loader allows you to use modules that depend on specific global variables. This is useful for third-party modules that rely on global variables like $ or this being the window object. The imports loader can add the necessary require('whatever') calls, so those modules work with webpack.

How does webpack dynamic import work?

Whenever you import a file in your code, Webpack will look for it in the project directory and copy it to the build folder with a new name, then Webpack replaces the import code in your bundled JavaScript file with the path to the newly copied file.


1 Answers

I ended up figuring this out. By using the webpack resolve.alias, I could add:

resolve: {   alias: {     common: path.resolve(__dirname, '../../path/to/code/my-library-' + versionNumber + '/domain')   } } 

in webpack.config.

Then in my code, I can require all of the files under /domainvia:

var commonRequires = require.context('common', true, /\.js$/); commonRequires.keys().forEach(commonRequires); 

Likewise, I could get a single file via:

require('common/clazz.js'); // ../../path/to/code/my-library-1.0.0/domain/clazz.js 

FWIW, to get versionNumber I used node's fs module to read from a json file, used JSON.parse(file) and then retrieved the version at the top of webpack.config

like image 194
Alex White Avatar answered Sep 27 '22 20:09

Alex White