Webpack HTTP Resource Loader
raw JSON →webpack-require-http is a webpack plugin designed to enable the direct `require()` of HTTP and HTTPS resources within JavaScript code. This functionality is achieved by configuring it within webpack's `externals` option, where it transforms remote URL imports into asynchronous operations that fetch and inject the external scripts or stylesheets into the browser environment. The `require()` call then returns a `Promise` which resolves upon successful loading of the resource. The package, currently at version 0.4.3, was last updated in 2017, indicating it is no longer actively maintained. A key differentiator is its ability to accept custom rules (using regular expressions or functions) to rewrite or re-map requested URLs, offering flexibility in handling external resource paths. While it addressed a specific need for loading remote assets directly via `require` in earlier webpack versions, modern webpack (v5+) provides more robust and integrated solutions for handling external dependencies and remote modules, making this package largely superseded.
Common errors
error Module not found: Error: Can't resolve 'http://...' in '...' ↓
externals: [require('webpack-require-http')] (or require('webpack-require-http').custom(...)) is correctly placed within your webpack configuration's top-level object. error ReferenceError: [global_variable] is not defined ↓
require() Promise to resolve before attempting to use any global variables or side effects exposed by the remote script. Wrap usage in a .then() block or use await. error TypeError: require is not a function ↓
require calls. This package is not compatible with native ESM import statements for remote resources. Warnings
breaking Incompatibility with Webpack 5+. Modern Webpack versions (5.x and later) have significantly changed their plugin API and module resolution mechanisms. This package, last updated in 2017, is highly likely to be incompatible, leading to build failures or unexpected runtime behavior. ↓
gotcha Asynchronous `require` behavior. Unlike standard CommonJS `require`, which is synchronous and returns the module's exports, `webpack-require-http`'s `require()` returns a `Promise`. Developers must use `.then()` or `await` to access resources, which can be a source of confusion if not expected. ↓
deprecated Usage of `externals` for dynamic remote scripts is largely superseded. Modern Webpack offers more robust and integrated solutions for remote dependencies, such as Module Federation for sharing modules across applications, or native dynamic `import()` for loading remote scripts, making this `externals`-based approach less advisable and potentially less performant or secure. ↓
gotcha Potential security risks due to lack of maintenance and direct remote script loading. As an unmaintained package loading external resources directly into your application, there's an increased risk of supply chain attacks if the remote servers are compromised or if the package itself contains unpatched vulnerabilities. ↓
Install
npm install webpack-require-http yarn add webpack-require-http pnpm add webpack-require-http Imports
- webpack-require-http wrong
import webpackRequireHttp from 'webpack-require-http';correctconst webpackRequireHttp = require('webpack-require-http'); - custom wrong
import { custom } from 'webpack-require-http';correctconst customHttpResolver = require('webpack-require-http').custom;
Quickstart
const webpack = require('webpack');
const path = require('path');
const webpackRequireHttp = require('webpack-require-http');
// Basic webpack configuration demonstrating `webpack-require-http`
const config = {
mode: 'development',
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
},
externals: [
webpackRequireHttp
],
};
// Example content for src/index.js
// (This code would typically be in your source file, not directly in webpack.config.js)
/*
console.log('Loading remote resources...');
Promise.all([
require('http://codemirror.net/lib/codemirror.css'),
require('http://codemirror.net/lib/codemirror.js'),
require('http://codemirror.net/addon/edit/matchbrackets.js'),
require('http://codemirror.net/addon/comment/continuecomment.js'),
require('http://codemirror.net/addon/comment/comment.js'),
require('http://codemirror.net/mode/javascript/javascript.js')
]).then(function() {
// CodeMirror would be available globally after these scripts load
console.log('CodeMirror loaded:', typeof CodeMirror !== 'undefined');
// You could then initialize CodeMirror here
}).catch(error => {
console.error('Failed to load resources:', error);
});
*/
module.exports = config;