You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
4.1 KiB

3 months ago
  1. # Webpack Virtual Modules
  2. [![Build Status](https://travis-ci.org/sysgears/webpack-virtual-modules.svg?branch=master)](https://travis-ci.org/sysgears/webpack-virtual-modules)
  3. [![Twitter Follow](https://img.shields.io/twitter/follow/sysgears.svg?style=social)](https://twitter.com/sysgears)
  4. **Webpack Virtual Modules** is a plugin that allows for dynamical generation of in-memory virtual modules for JavaScript
  5. builds created with webpack. When virtual module is created all the parent virtual dirs that lead to the module filename are created too. This plugin supports watch mode meaning any write to a virtual module is seen by webpack as
  6. if a real file stored on disk has changed.
  7. ## Installation
  8. Use NPM or Yarn to install Webpack Virtual Modules as a development dependency:
  9. ```bash
  10. # with NPM
  11. npm install webpack-virtual-modules --save-dev
  12. # with Yarn
  13. yarn add webpack-virtual-modules --dev
  14. ```
  15. ## Usage
  16. You can use Webpack Virtual Modules with webpack 5, 4 and 3. The examples below show the usage with webpack 5 or 4. If you want to use our plugin with webpack 3, check out a dedicated doc:
  17. * [Webpack Virtual Modules with Webpack 3]
  18. ### Generating static virtual modules
  19. Require the plugin in the webpack configuration file, then create and add virtual modules in the `plugins` array in the
  20. webpack configuration object:
  21. ```js
  22. var VirtualModulesPlugin = require('webpack-virtual-modules');
  23. var virtualModules = new VirtualModulesPlugin({
  24. 'node_modules/module-foo.js': 'module.exports = { foo: "foo" };',
  25. 'node_modules/module-bar.js': 'module.exports = { bar: "bar" };'
  26. });
  27. module.exports = {
  28. // ...
  29. plugins: [
  30. virtualModules
  31. ]
  32. };
  33. ```
  34. You can now import your virtual modules anywhere in the application and use them:
  35. ```js
  36. var moduleFoo = require('module-foo');
  37. // You can now use moduleFoo
  38. console.log(moduleFoo.foo);
  39. ```
  40. ### Generating dynamic virtual modules
  41. You can generate virtual modules **_dynamically_** with Webpack Virtual Modules.
  42. Here's an example of dynamic generation of a module. All you need to do is create new virtual modules using the plugin
  43. and add them to the `plugins` array. After that, you need to add a webpack hook. For using hooks, consult [webpack
  44. compiler hook documentation].
  45. ```js
  46. var webpack = require('webpack');
  47. var VirtualModulesPlugin = require('webpack-virtual-modules');
  48. // Create an empty set of virtual modules
  49. const virtualModules = new VirtualModulesPlugin();
  50. var compiler = webpack({
  51. // ...
  52. plugins: [
  53. virtualModules
  54. ]
  55. });
  56. compiler.hooks.compilation.tap('MyPlugin', function(compilation) {
  57. virtualModules.writeModule('node_modules/module-foo.js', '');
  58. });
  59. compiler.watch();
  60. ```
  61. In other module or a Webpack plugin, you can write to the module `module-foo` whatever you need. After this write,
  62. webpack will "see" that `module-foo.js` has changed and will restart compilation.
  63. ```js
  64. virtualModules.writeModule(
  65. 'node_modules/module-foo.js',
  66. 'module.exports = { foo: "foo" };'
  67. );
  68. ```
  69. ## More Examples
  70. - [Swagger and JSDoc Example with Webpack 5]
  71. - [Swagger and JSDoc Example with Webpack 4]
  72. - [Swagger and JSDoc Example with Webpack 3]
  73. ## API Reference
  74. - [API Reference]
  75. ## Inspiration
  76. This project is inspired by [virtual-module-webpack-plugin].
  77. ## License
  78. Copyright © 2017 [SysGears INC]. This source code is licensed under the [MIT] license.
  79. [webpack virtual modules with webpack 3]: https://github.com/sysgears/webpack-virtual-modules/tree/master/docs/webpack3.md
  80. [webpack compiler hook documentation]: https://webpack.js.org/api/compiler-hooks/
  81. [swagger and jsdoc example with webpack 3]: https://github.com/sysgears/webpack-virtual-modules/tree/master/examples/swagger-webpack3
  82. [swagger and jsdoc example with webpack 4]: https://github.com/sysgears/webpack-virtual-modules/tree/master/examples/swagger-webpack4
  83. [swagger and jsdoc example with webpack 5]: https://github.com/sysgears/webpack-virtual-modules/tree/master/examples/swagger-webpack5
  84. [api reference]: https://github.com/sysgears/webpack-virtual-modules/tree/master/docs/API%20Reference.md
  85. [virtual-module-webpack-plugin]: https://github.com/rmarscher/virtual-module-webpack-plugin
  86. [MIT]: LICENSE
  87. [SysGears INC]: http://sysgears.com