All Projects → rmarscher → Virtual Module Webpack Plugin

rmarscher / Virtual Module Webpack Plugin

Adds the contents of a virtual file to webpack's cached file system without writing it to disk

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Virtual Module Webpack Plugin

Terser Webpack Plugin
Terser Plugin
Stars: ✭ 1,687 (+922.42%)
Mutual labels:  webpack, webpack-plugin
Svg Sprite Loader
Webpack loader for creating SVG sprites.
Stars: ✭ 1,822 (+1004.24%)
Mutual labels:  webpack, webpack-plugin
Rollbar Sourcemap Webpack Plugin
A Webpack plugin to upload sourcemaps to Rollbar
Stars: ✭ 127 (-23.03%)
Mutual labels:  webpack, webpack-plugin
Gas Webpack Plugin
Webpack plugin for Google Apps Script
Stars: ✭ 97 (-41.21%)
Mutual labels:  webpack, webpack-plugin
Obsolete Webpack Plugin
🌈 A Webpack plugin generates a browser-side standalone script that detects browser compatibility based on `Browserslist` and prompts website users to upgrade it.
Stars: ✭ 148 (-10.3%)
Mutual labels:  webpack, webpack-plugin
Webpack Tools
☕️Just a simple webpack sample project.
Stars: ✭ 106 (-35.76%)
Mutual labels:  webpack, webpack-plugin
Size Plugin
Track compressed Webpack asset sizes over time.
Stars: ✭ 1,665 (+909.09%)
Mutual labels:  webpack, webpack-plugin
Jest Webpack
Use jest with webpack.
Stars: ✭ 89 (-46.06%)
Mutual labels:  webpack, webpack-plugin
Bundle Stats
In-depth bundle analyzer for webpack(bundle size, assets, modules, packages)
Stars: ✭ 144 (-12.73%)
Mutual labels:  webpack, webpack-plugin
Worker Plugin
👩‍🏭 Adds native Web Worker bundling support to Webpack.
Stars: ✭ 1,840 (+1015.15%)
Mutual labels:  webpack, webpack-plugin
Uglifyjs Webpack Plugin
[deprecated] UglifyJS Plugin
Stars: ✭ 1,343 (+713.94%)
Mutual labels:  webpack, webpack-plugin
Svg Spritemap Webpack Plugin
SVG spritemap plugin for webpack
Stars: ✭ 160 (-3.03%)
Mutual labels:  webpack, webpack-plugin
Fork Ts Checker Webpack Plugin
Webpack plugin that runs typescript type checker on a separate process.
Stars: ✭ 1,343 (+713.94%)
Mutual labels:  webpack, webpack-plugin
Everything Is A Plugin
Everything is a Plugin: Mastering webpack from the inside out. NgConf 2017
Stars: ✭ 123 (-25.45%)
Mutual labels:  webpack, webpack-plugin
Webpack Core Usage
webpack2完整系列课程,欢迎阅读。同时欢迎移步我的react全家桶文章全集: https://github.com/liangklfangl/react-article-bucket
Stars: ✭ 94 (-43.03%)
Mutual labels:  webpack, webpack-plugin
Webpack Visualizer
Visualize your Webpack bundle
Stars: ✭ 1,664 (+908.48%)
Mutual labels:  webpack, webpack-plugin
Auxpack
A dashboard for monitoring Webpack build stats.
Stars: ✭ 86 (-47.88%)
Mutual labels:  webpack, webpack-plugin
Ngc Webpack
Angular compiler-cli with webpack's loader chain.
Stars: ✭ 87 (-47.27%)
Mutual labels:  webpack, webpack-plugin
Webpack Require From
Webpack plugin that allows to configure path or URL for fetching dynamic imports
Stars: ✭ 142 (-13.94%)
Mutual labels:  webpack, webpack-plugin
Webpack Babel Multi Target Plugin
A Webpack plugin that works with Babel to allow differential loading - production deployment of ES2015 builds targeted to modern browsers, with an ES5 fallback for legacy browsers.
Stars: ✭ 150 (-9.09%)
Mutual labels:  webpack, webpack-plugin

Virtual Module Webpack Plugin Build Status codecov.io npm package

2020 Update: Webpack 5 is out. Instead of adding Webpack 5 support to this project, it seems best to switch to use the webpack-virtual-modules project and deprecate this project. webpack-virtual-modules was inspired by this project and adds hot reloading / file watching and makes it easier to define many virtual modules.

Original Readme:

This is an experimental plugin that adds the contents of a virtual file to Webpack's cached file system without writing it to disk.

This would be used if you generated file contents at build time that needs to be consumed as a module by your source code, but you don't want to write this file to disk.

It uses private APIs of the CachedInputFileSystem of the enhanced-resolve package that Webpack uses as the module resolver. Therefore, it is inherently fragile and subject to be broken if the CachedInputFileSystem changes. Fortunately, the changes have not been too extensive between webpack 1.x - 4.x and this plugin has been updated to be compatible with all.

See https://github.com/webpack/enhanced-resolve/blob/master/lib/CachedInputFileSystem.js

If another webpack plugin clears the CachedInputFileSystem without triggering the resolve event of the resolver plugin lifecycle, the virtual file will no longer be able to be referenced. Based off the issues received in this plugin's history, this does not seem to be an issue.

Difference between val-loader

val-loader is also capable of dynamically generating module code at build time. val-loader is a "loader" and not a "plugin." Webpack loaders require a file to exist in webpack's file system cache. Webpack loads the cache from the files on disk. This virtual-module-webpack-plugin inserts directly into webpack's file system cache.

val-loader is better if you have a file you want to load at build time that contains all of the logic to dynamically fetch and return the source of that file. You also are able to use watch mode in development since there is a physical file to watch.

virtual-module-webpack-plugin is better if you have a build script that is collecting stats, config or other data that you want to be able to reference in the runtime code without every writing that data to a source file on disk.

Usage

In your webpack.config.js, require the plugin:

const VirtualModulePlugin = require('./virtual-module-webpack-plugin');

Then when defining the config object create an instance of the plugin passing in the moduleName and contents and add it to the webpack config's plugins array.

The moduleName should be relative to your webpack config context which defaults to the directory holding the webpack.config.js file.

  plugins: [
    new VirtualModulePlugin({
      moduleName: 'src/mysettings.json',
      contents: JSON.stringify({ greeting: 'Hello!' })
    })
  ]

Then require the file as you would any other module in your source. The file contents will be passed through any loaders you setup that match the moduleName.

If you pass an object to contents, it will automatically be passed through JSON.stringify. You can also pass a function to contents which will be invoked at compile time with no arguments. See pull #10.

See the examples directory for a complete working examples with webpack 1.x, 2.x and 4.x.

If you need to fetch the contents asynchronously, you need to have your webpack.config.js return a Promise. It should first resolve getting your module contents and then return the Webpack config.

A few development attempts were made at letting the plugin resolve the contents on demand, but we were unable to get Webpack to wait for a callback during the resolve stage. See pull requests #11 and #12. Pull requests to solve the problem are welcome, but it needs to work even if the asynchronous content request takes a while. You can uncomment code in test/integration/cases/contents-async/webpack.config.js to test it.

Here is an example of async content fetching inside webpack.config.js:

'use strict';

const VirtualModulePlugin = require('virtual-module-webpack-plugin');

function contents() {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve('a');
    }, 1000);
  });
}

module.exports = contents().then(asyncContents => ({
  entry: './index',
  plugins: [
    new VirtualModulePlugin({
      moduleName: './a.txt',
      contents: asyncContents,
    }),
  ],
}));
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].