Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
dynamic-entry-webpack-plugin
Advanced tools
webpack plugin to make entry point as dynamic import
webpack plugin to make entry point as dynamic import
When creating front-end applications, webpack might generate multiple chunks (based on configurations). In order to load application, certain chunks needs to be present before loading application's main chunk. Example, vendor chunk must be present before main chunk is loaded.
Loading of dependent chunks before main chunk is taken by html-webpack-plugin. For example, we might encounter script tags injected in HTML
<script src="/static/js/bundle.js"></script>
<script src="/static/js/3.chunk.js"></script>
<script src="/static/js/main.chunk.js"></script>
html-webpack-plugin loads dependent chunks in sequence and finally main chunk is loaded at last. This works fine for SPA, where bundles are intended to be loaded in browser.
Consider the scenario where you need to load application bundle conditionally.
For example, loading several micro-frontends bundles at run-time or loading a library.
Such bundles are hosted on different servers and they are included into parent application at runtime.
For scenarios described above, it will be best if we could load single chunk, which in turn loads all the dependent chunks and main chunk for us.
This is where dynamic-entry-webpack-plugin comes handy. It will turn all webpack entry points into dynamic import statements and place them in a dynamic in-memory module. The actual entry point configuration is then updated with this new dyanmic module.
Webpack internally has all the information about dependent chunks for given chunk. When dynamically loading a module, webpack ensures that all the dependend modules for dynamic import are loaded first then the asked module is loaded.
This behavior of webpack is leveraged to provide only single entry chunk, which will load dependent and main chunks.
Create a object of plugin and pass it to the webpack plugins config.
const DynamicEntry = require('dynamic-entry-webpack-plugin');
module.exports = {
...,
plugins: [
new DynamicEntry({
exportable: true,
}),
]
};
Options
exportable - boolean, optional - To make entry module exportable. Default: true
webpack supports entry point config in various formats. So accordingly, dynamic-entry-webpack-plugin behaves in following ways:
string
When entry point is string, plugin creates new entry module with following code:
export default () => import('<actual webpack entry point>');
// OR
import('<actual webpack entry point>'); // if exportable is false
array
According to webpack when multiple configs are provided in array, all the entries are included in bundle with last entry exported. Providing similar behavior, plugin also imports all the entry points and last entry point is returned.
export default () => (async () => {
await import('<entry one>');
await import('<entry two>');
.
.
return import('last entry');
})();
// when exportable is false, entry module generated will as follows:
(async () => {
await import('<entry one>');
await import('<entry two>');
.
.
return import('last entry');
})();
object
According to webpack, for object entry point, webpack creates multiple bundles with its own runtime. Similar behavior is provided by this plugin as well.
function
When given function to entry point it is awaited and result is then processed to form entry module. Return value can be string, array or object and it is processed according to above rules.
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue
FAQs
webpack plugin to make entry point as dynamic import
The npm package dynamic-entry-webpack-plugin receives a total of 0 weekly downloads. As such, dynamic-entry-webpack-plugin popularity was classified as not popular.
We found that dynamic-entry-webpack-plugin demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.