{"id":26672423,"url":"https://github.com/rmarscher/virtual-module-webpack-plugin","last_synced_at":"2025-04-09T06:12:04.087Z","repository":{"id":37445023,"uuid":"54988726","full_name":"rmarscher/virtual-module-webpack-plugin","owner":"rmarscher","description":"Adds the contents of a virtual file to webpack's cached file system without writing it to disk","archived":false,"fork":false,"pushed_at":"2020-10-28T01:48:22.000Z","size":245,"stargazers_count":174,"open_issues_count":5,"forks_count":17,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-04-27T01:03:39.371Z","etag":null,"topics":["webpack","webpack-plugin"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rmarscher.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-03-29T15:44:42.000Z","updated_at":"2023-11-06T02:13:47.000Z","dependencies_parsed_at":"2022-07-20T12:17:39.519Z","dependency_job_id":null,"html_url":"https://github.com/rmarscher/virtual-module-webpack-plugin","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rmarscher%2Fvirtual-module-webpack-plugin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rmarscher%2Fvirtual-module-webpack-plugin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rmarscher%2Fvirtual-module-webpack-plugin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rmarscher%2Fvirtual-module-webpack-plugin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rmarscher","download_url":"https://codeload.github.com/rmarscher/virtual-module-webpack-plugin/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247987285,"owners_count":21028895,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["webpack","webpack-plugin"],"created_at":"2025-03-26T00:50:28.267Z","updated_at":"2025-04-09T06:12:04.065Z","avatar_url":"https://github.com/rmarscher.png","language":"JavaScript","readme":"# Virtual Module Webpack Plugin [![Build Status](https://travis-ci.org/rmarscher/virtual-module-webpack-plugin.svg?branch=master)](https://travis-ci.org/rmarscher/virtual-module-webpack-plugin) [![codecov.io](https://codecov.io/github/rmarscher/virtual-module-webpack-plugin/coverage.svg?branch=master)](https://codecov.io/github/rmarscher/virtual-module-webpack-plugin?branch=master) [![npm package](https://badge.fury.io/js/virtual-module-webpack-plugin.svg)](https://www.npmjs.com/package/virtual-module-webpack-plugin)\n\n**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](https://github.com/sysgears/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.\n\n**Original Readme**:\n\nThis is an experimental plugin that adds the contents of a virtual file to\nWebpack's cached file system without writing it to disk.\n\nThis would be used if you generated file contents at build time that needs to\nbe consumed as a module by your source code, but you don't want to write this\nfile to disk.\n\nIt uses private APIs of the CachedInputFileSystem of the `enhanced-resolve`\npackage that Webpack uses as the module resolver. Therefore, it is inherently\nfragile and subject to be broken if the CachedInputFileSystem changes. Fortunately,\nthe changes have not been too extensive between webpack 1.x - 4.x and this plugin\nhas been updated to be compatible with all.\n\nSee https://github.com/webpack/enhanced-resolve/blob/master/lib/CachedInputFileSystem.js\n\nIf another webpack plugin clears the CachedInputFileSystem without triggering the\nresolve event of the resolver plugin lifecycle, the virtual file will no longer be able\nto be referenced. Based off the issues received in this plugin's history, this does\nnot seem to be an issue.\n\n## Difference between val-loader\n\n[val-loader](https://github.com/webpack-contrib/val-loader) is also capable of dynamically\ngenerating module code at build time. val-loader is a \"loader\" and not a \"plugin.\" Webpack\nloaders require a file to exist in webpack's file system cache. Webpack loads the cache\nfrom the files on disk. This virtual-module-webpack-plugin inserts directly into webpack's\nfile system cache.\n\nval-loader is better if you have a file you want to load at build time that contains all\nof the logic to dynamically fetch and return the source of that file. You also are able\nto use watch mode in development since there is a physical file to watch.\n\nvirtual-module-webpack-plugin is better if you have a build script that is collecting\nstats, config or other data that you want to be able to reference in the runtime code\nwithout every writing that data to a source file on disk.\n\n## Usage\n\nIn your webpack.config.js, require the plugin:\n\n```js\nconst VirtualModulePlugin = require('./virtual-module-webpack-plugin');\n```\n\nThen when defining the config object create an instance of the plugin\npassing in the `moduleName` and `contents` and add it to the webpack\nconfig's plugins array.\n\nThe `moduleName` should be relative to your webpack config context\nwhich defaults to the directory holding the webpack.config.js file.\n\n```js\n  plugins: [\n    new VirtualModulePlugin({\n      moduleName: 'src/mysettings.json',\n      contents: JSON.stringify({ greeting: 'Hello!' })\n    })\n  ]\n```\n\nThen require the file as you would any other module in your source.\nThe file contents will be passed through any loaders you setup\nthat match the moduleName.\n\nIf you pass an object to contents, it will automatically be passed through\n`JSON.stringify`. You can also pass a function to contents which will be\ninvoked at compile time with no arguments. See [pull #10](https://github.com/rmarscher/virtual-module-webpack-plugin/pull/10).\n\nSee the examples directory for a complete working examples with webpack 1.x,\n2.x and 4.x.\n\nIf you need to fetch the contents asynchronously, you need to have your `webpack.config.js`\nreturn a Promise. It should first resolve getting your module contents and then\nreturn the Webpack config.\n\nA few development attempts were made at letting the plugin resolve the contents\non demand, but we were unable to get Webpack to wait for a callback during the\nresolve stage. See pull requests [#11](https://github.com/rmarscher/virtual-module-webpack-plugin/pull/11)\nand [#12](https://github.com/rmarscher/virtual-module-webpack-plugin/pull/12).\nPull requests to solve the problem are welcome, but it needs to work even if the\nasynchronous content request takes a while. You can uncomment code in\n`test/integration/cases/contents-async/webpack.config.js` to test it.\n\nHere is an example of async content fetching inside webpack.config.js:\n\n```\n'use strict';\n\nconst VirtualModulePlugin = require('virtual-module-webpack-plugin');\n\nfunction contents() {\n  return new Promise(resolve =\u003e {\n    setTimeout(() =\u003e {\n      resolve('a');\n    }, 1000);\n  });\n}\n\nmodule.exports = contents().then(asyncContents =\u003e ({\n  entry: './index',\n  plugins: [\n    new VirtualModulePlugin({\n      moduleName: './a.txt',\n      contents: asyncContents,\n    }),\n  ],\n}));\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frmarscher%2Fvirtual-module-webpack-plugin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frmarscher%2Fvirtual-module-webpack-plugin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frmarscher%2Fvirtual-module-webpack-plugin/lists"}