All Projects → agoldis → Webpack Require From

agoldis / Webpack Require From

Licence: mit
Webpack plugin that allows to configure path or URL for fetching dynamic imports

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Webpack Require From

Bundle Buddy Webpack Plugin
🐐🐐🐐🐐 bundle-buddy-webpack-plugin 🐐🐐🐐🐐
Stars: ✭ 199 (+40.14%)
Mutual labels:  webpack, webpack-plugin, code-splitting
bs-dynamic-import
📦🚀 BuckleScript dynamic import interop on JavaScript environment
Stars: ✭ 31 (-78.17%)
Mutual labels:  dynamic, code-splitting, import
Reason Loadable
🔥 Suspense/Lazy for ReasonReact.
Stars: ✭ 88 (-38.03%)
Mutual labels:  import, code-splitting, dynamic
Webpack Tools
☕️Just a simple webpack sample project.
Stars: ✭ 106 (-25.35%)
Mutual labels:  webpack, webpack-plugin
Asf Ui
The official web interface for ASF
Stars: ✭ 100 (-29.58%)
Mutual labels:  webpack, hacktoberfest
Ts React Boilerplate
Universal React App with Redux 4, Typescript 3, and Webpack 4
Stars: ✭ 104 (-26.76%)
Mutual labels:  webpack, hacktoberfest
Webpack Core Usage
webpack2完整系列课程,欢迎阅读。同时欢迎移步我的react全家桶文章全集: https://github.com/liangklfangl/react-article-bucket
Stars: ✭ 94 (-33.8%)
Mutual labels:  webpack, webpack-plugin
Awesome Bundle Size
📝 An awesome list of tools and techniques to make your web bundle size smaller and your web apps load faster.
Stars: ✭ 118 (-16.9%)
Mutual labels:  webpack, code-splitting
Lazy Compile Webpack Plugin
Boost webpack startup time by lazily compiling dynamic imports
Stars: ✭ 106 (-25.35%)
Mutual labels:  webpack, dynamic
Across Tabs
Easy communication between cross-origin browser tabs. Simplified "CORS"ing!
Stars: ✭ 1,575 (+1009.15%)
Mutual labels:  webpack, hacktoberfest
Rollbar Sourcemap Webpack Plugin
A Webpack plugin to upload sourcemaps to Rollbar
Stars: ✭ 127 (-10.56%)
Mutual labels:  webpack, webpack-plugin
Gas Webpack Plugin
Webpack plugin for Google Apps Script
Stars: ✭ 97 (-31.69%)
Mutual labels:  webpack, webpack-plugin
Uglifyjs Webpack Plugin
[deprecated] UglifyJS Plugin
Stars: ✭ 1,343 (+845.77%)
Mutual labels:  webpack, webpack-plugin
Lichter.io
My own website and CV
Stars: ✭ 105 (-26.06%)
Mutual labels:  webpack, hacktoberfest
Fork Ts Checker Webpack Plugin
Webpack plugin that runs typescript type checker on a separate process.
Stars: ✭ 1,343 (+845.77%)
Mutual labels:  webpack, webpack-plugin
React Async Component
Resolve components asynchronously, with support for code splitting and advanced server side rendering use cases.
Stars: ✭ 1,441 (+914.79%)
Mutual labels:  webpack, code-splitting
Terser Webpack Plugin
Terser Plugin
Stars: ✭ 1,687 (+1088.03%)
Mutual labels:  webpack, webpack-plugin
Webpack Visualizer
Visualize your Webpack bundle
Stars: ✭ 1,664 (+1071.83%)
Mutual labels:  webpack, webpack-plugin
Svg Sprite Loader
Webpack loader for creating SVG sprites.
Stars: ✭ 1,822 (+1183.1%)
Mutual labels:  webpack, webpack-plugin
Jest Webpack
Use jest with webpack.
Stars: ✭ 89 (-37.32%)
Mutual labels:  webpack, webpack-plugin

npm version npm CircleCI

webpack-require-from

Control the dynamic imports path / URL at runtime

Table of contents

Why changing dynamic imports path at runtime?

Webpack allows to split and load code atomatically using require.ensure or dynamic import import(). Modules are fetched "on-demand" when your main bundle is running in browser.

Webpack loads the modules (chunks) from a static URL, which is determined by config.output.publicPath of webpack configuration.

Sometimes you need to control this modules (chunks) URL at runtime, for example:

  • Chunks are hosted at a CDN
  • Different environments use different URLs for loading assets (production, staging, qa)
  • Your index file is served from a different location / port
  • You need to dynamically load pre-compiled files from a different location
  • You need to load 3rd part web worker from a CDN

How to use

// webpack.config.js
const WebpackRequireFrom = require("webpack-require-from");
const webpackRequireFromConfig = (module.exports = {
  output: {
    publicPath: "/webpack/"
  },
  plugins: [
    new WebpackRequireFrom({
      // see configuration options below
    })
  ]
});

Configuration

If no options provided, the plugin will use the default config.output.publicPath. Check out the "example" directory.

path

Set path for dynamically loading modules. The value you provide will replace config.output.publicPath when dynamically importing chunks.

For example, if default URL is https://localhost, chunk name is 0.js and options object is {path: "customPath/" }, the chunk will be fetched from https://localhost/customPath/0.js

NOTE path, methodName and variableName are mutualy exclusive and cannot be used together

variableName

variableName is the globaly defined variable that will be evaluated at runtime, variableName is the name of a variable with string value that represents a path / URL that will be used for dynamically importing of chunks.

For example, if default URL is https://localhost, chunk name is 0.js and options object is {variableName: "chunkURL" }, while window.chunkURL is defined to be:

window.chunkURL = "https://app.cdn.com/buildXXX/";

the chunk will be fetched from https://app.cdn.com/buildXXX/0.js

methodName

Name of the globaly defined method that will be invoked at runtime, the method should return a path / URL that will be used for dynamically importing of chunks.

For example, if default URL is https://localhost, chunk name is 0.js and options object is {methodName: "getChunkURL" }, while window.getChunkURL is defined to be:

window.getChunkURL = function() {
  if (true) {
    // use any condition to choose the URL
    return "https://app.cdn.com/buildXXX/";
  }
};

the chunk will be fetched from https://app.cdn.com/buildXXX/0.js

If used together with replaceSrcMethodName, chunks URL will be first modified by window[methodName] and then, the modified values are passed as an argument to window[replaceSrcMethodName] function.

NOTE path, methodName and variableName are mutualy exclusive and cannot be used together

NOTE that the method should be defined in a global namespace and should be defined before require.ensure or import() is invoked. See examples below

replaceSrcMethodName

Name of the globaly defined method that will be invoked at runtime; the method receives the full URL of the dynamically required chunk as its argument and should return a string with the new URL.

For example, if default URL is https://localhost, chunk names are 0.js and common.js, options object is {replaceSrcMethodName: "replaceSrc" }, while window.replaceSrc is defined to be:

window.replaceSrc = function(originalSrc) {
  if (originalSrc.match(/common/)) {
    // rename specific chunk
    return originalSrc.replace(/common/, "static");
  }
  return originalSrc;
};

the chunks will be fetched from https://localhost/0.js and https://localhost/static.js

If used together with methodName or variableName, chunks URL will be first modified by window[methodName] or will be modified to window[variableName] and then, the modified values are passed as an argument to window[replaceSrcMethodName] function.

NOTE that the method should be defined in a global namespace and should be defined before require.ensure or import() is invoked.

suppressErrors

default: false. The plugin will invoke console.error when the method name you defined in replaceSrcMethodName, methodName or variableName cannot be detected. Turning this option on will suppress the error messages.

Global methods and variables

When your JS code is executed in browser, the variable/methods whose names you mention as variableName, methodName or replaceSrcMethodName value, should be set before the first call to require.ensure() or import() is executed.

The return value of the methods will be used to build the URL for fetching resources.

For example, let's define veryFirst method to be globally available before you main bundle is being executed.

Add the method definition at the very first line of you bundle:

const window.veryFirst = function () {
 console.log("I am very first!");
}

You can use a separate file and use webpack's entry point list:

// filename: veryFirst.js
const window.veryFirst = function () {
 console.log("I am very first!");
}

// file webpack.config.js
module.exports = {
  entry: {
    ['./veryFirst.js', './index.js']
  }
}

Another approach is to define veryFirst as part of index.html when building it on your server:

// filename: server/app.js
app.get("/", (req, res) =>
  res.render("views/index", {
    cdnPath: "https://qa.cdn.com/|https://prod.cdn.com/"
  })
);
<!-- filename: views/index.ejs -->
<html>
<script>
  const baseCDN = "<%= cdnPath %>";
  window.veryFirst = function () {
      console.log(`${baseCDN}/js/`);
  }
</script>
...
</html>

Web Workers

TL;DR

Use replaceSrcMethodName to provide a method for modifying web-worker loading path. The method must be globally available and defined before import() calls within your web-worker. From example directory:

/* webpack.config.js */
  // ...
  module: {
    rules: [
      {
        test: /worker\.js$/,
        use: {
          loader: `worker-loader`
        }
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin(),
    new RequireFrom({
      replaceSrcMethodName: "getSrc"
    })
  ]
  // ...


/* worker.js */
require("./globals.js");

import("./worker-module.js").then(workerModule => {
  workerModule.default();
  console.log("loaded workerModule");
});

Details

The plugin allows to change the loading path of web-workers.

Do to so, use the worker-loader loader. The loader uses importScripts to dynamically load modules from within your web-worker and support cross-domain web workers. More specifically it:

  1. Creates a new webpack entry that only contains new Worker(workerURL), while workerURL is your main webworker module
  2. Enhances your webworker main module with webpack runtime utilities
  3. Uses importScripts to dynamically load new modules within webworker context (thus avoiding cross-domain limitations)

The plugin monkey-patches importScripts and invokes the method you've defined within replaceSrcMethodName configuration option. The method you've provided will be invoked just before calling importScripts with the required module path as the single argument.

Check out the working example of using the plugin with web-workers at web2fs-notepad by @sushain97.

Troubleshooting

${methodName} is not a function or not available at runtime.

  • Make sure your method name in webpack.config.js matches the method name you define on global window object.

  • Make sure the method is defined before the very first invocation of either require.ensure() or import()

Specify either "methodName" or "path", not together.

  • path and methodName are mutualy exclusive and cannot be used together, use either of them

'${methodName}' does not return string.

  • when using replaceSrcMethodName options the result of invoking window[replaceSrcMethodName] is validated - it should be defined and be a string

  • make sure you return a string value from window[replaceSrcMethodName]

Don't hesitate to open issues.

Tests

yarn test

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].