Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack dynamic import based on entry name

I'm trying to have Webpack to bundle specific files depending on the entry file.

I have multiple entry files in my project. They all use common helpers functions (separate modules), but some entry files use a slightly modified version.

Here's what I've done so far:

entry1.js

import helper from './helper';

helper();

entry2.js

import helper from './helper';

helper();

helper.js

import example from './example';

export default function helper() {
  console.log('common console log for Entry 1 and 2 pages');

  example();
}

example.js

const context = window.__CONTEXT__;

export default require(`./example_${context}`).default;

example_entry1.js

export default function() {
  console.log('specific console log for Entry 1 page only');
};

example_entry2.js

export default function() {
  console.log('specific console log for Entry 2 page only');
};

entry1.html

<script>
  window.__CONTEXT__ = 'entry1';
</script>
<script src="entry1.js" />

Browser's console output:

common console log for Entry 1 and 2 pages

specific console log for Entry 1 page only


entry2.html

<script>
  window.__CONTEXT__ = 'entry2';
</script>
<script src="entry2.js" />

Browser's console output:

common console log for Entry 1 and 2 pages

specific console log for Entry 2 page only


Is there a way to have something like this in example.js?

export default require(`./example_${__CURRENT_ENTRY_FILENAME__}`).default;

So, Webpack can bundle entry1.js and entry2.js with their specific helper modules!

And, that way, I'm hoping to get rid of the terrible window.__CONTEXT__ in my HTML and JS files.

Thanks!

like image 418
Antoine Avatar asked Jun 21 '17 16:06

Antoine


People also ask

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.

How do dynamic imports work?

Dynamic module imports are very similar to static module imports, but the code is loaded only when it is needed instead of being loaded right away. This means your page load speed will be much quicker and the user will only ever load JavaScript code that they are actually going to use.

What does dynamic import return?

Dynamic import of modules It returns a promise and starts an asynchronous task to load the module. If the module was loaded successfully, then the promise resolves to the module content, otherwise, the promise rejects. path can be any expression that evaluates to a string denoting a path.

Does webpack use CommonJS?

As you may know, webpack supports a couple of module types out of the box, including both CommonJS and ES modules. Webpack also works on both client- and server-side JavaScript, so with webpack, we can also easily handle assets and resources like images, fonts, stylesheets, and so on.


1 Answers

You can use Modules Dynamic imports but you'll have to use promises:

example.js

exports default (moduleName) => import(`./example_${moduleName}`);

helper.js

import example from './example';

export default function helper() {
  example(__CURRENT_ENTRY_FILENAME__).then((module) => {
    console.log('common console log for Entry 1 and 2 pages');

    module.default();
  });
}
like image 118
Christos Lytras Avatar answered Oct 11 '22 12:10

Christos Lytras