webpack.config.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. const path = require('path');
  2. const VirtualModulesPlugin = require('../..');
  3. const swaggerJsDoc = require('swagger-jsdoc');
  4. function SwaggerPlugin() {
  5. }
  6. SwaggerPlugin.prototype.apply = function(compiler) {
  7. const pkgJsonModule = './package.json';
  8. const pkgJsonPath = require.resolve(pkgJsonModule);
  9. const pkgJson = require(pkgJsonModule);
  10. // Path on file system for `swagger.json`, where webpack should "see" it
  11. const swaggerJsonPath = path.join(path.dirname(pkgJsonPath), 'node_modules', 'swagger.json');
  12. // Creating virtual module with initial contents
  13. const virtualModules = new VirtualModulesPlugin({[swaggerJsonPath]: JSON.stringify({
  14. openapi: '3.0.0',
  15. info: { title: pkgJson.name, version: pkgJson.version, description: pkgJson.description }
  16. })});
  17. virtualModules.apply(compiler);
  18. compiler.plugin('compilation', function(compilation) {
  19. try {
  20. const swaggerJson = swaggerJsDoc({
  21. swaggerDefinition: {
  22. openapi: '3.0.0',
  23. info: { title: pkgJson.name, version: pkgJson.version, description: pkgJson.description }
  24. },
  25. apis: ['*.js', '!(node_modules)/**/*.js']
  26. });
  27. virtualModules.writeModule(swaggerJsonPath, JSON.stringify(swaggerJson));
  28. } catch (e) {
  29. compilation.errors.push(e);
  30. }
  31. });
  32. }
  33. module.exports = {
  34. entry: './index.js',
  35. plugins: [new SwaggerPlugin()],
  36. module: {
  37. rules: [
  38. {
  39. test: /\.css$/,
  40. use: ['style-loader', 'css-loader']
  41. }
  42. ]
  43. }
  44. };