BVB Source Codes

storybook Show middleware.js Source code

Return Download storybook: download middleware.js Source code - Download storybook Source code - Type:.js
  1. import fs from 'fs';
  2. import path from 'path';
  3. import { Router } from 'express';
  4. import webpack from 'webpack';
  5. import webpackDevMiddleware from 'webpack-dev-middleware';
  6. import webpackHotMiddleware from 'webpack-hot-middleware';
  7. import baseConfig from './config/webpack.config';
  8. import baseProductionConfig from './config/webpack.config.prod';
  9. import loadConfig from './config';
  10. import getIndexHtml from './index.html';
  11.  
  12. function getMiddleware(configDir) {
  13.   const middlewarePath = path.resolve(configDir, 'middleware.js');
  14.   if (fs.existsSync(middlewarePath)) {
  15.     let middlewareModule = require(middlewarePath);
  16.     if (middlewareModule.__esModule) {
  17.       middlewareModule = middlewareModule.default;
  18.     }
  19.     return middlewareModule;
  20.   }
  21.   return function() {};
  22. }
  23.  
  24. export default function({ projectDir, configDir, ...options }) {
  25.   // Build the webpack configuration using the `baseConfig`
  26.   // custom `.babelrc` file and `webpack.config.js` files
  27.   const environment = options.environment || 'DEVELOPMENT';
  28.   const isProd = environment === 'PRODUCTION';
  29.   const currentWebpackConfig = isProd ? baseProductionConfig : baseConfig;
  30.   const config = loadConfig(environment, currentWebpackConfig, projectDir, configDir);
  31.  
  32.   // remove the leading '/'
  33.   let publicPath = config.output.publicPath;
  34.   if (publicPath[0] === '/') {
  35.     publicPath = publicPath.slice(1);
  36.   }
  37.  
  38.   const compiler = webpack(config);
  39.   const devMiddlewareOptions = {
  40.     noInfo: true,
  41.     publicPath: config.output.publicPath,
  42.     watchOptions: config.watchOptions || {},
  43.   };
  44.  
  45.   const router = new Router();
  46.   const middlewareFn = getMiddleware(configDir);
  47.   middlewareFn(router);
  48.  
  49.   router.use(webpackDevMiddleware(compiler, devMiddlewareOptions));
  50.  
  51.   if (!isProd) {
  52.     router.use(webpackHotMiddleware(compiler));
  53.   }
  54.  
  55.   router.get('/', (req, res) => {
  56.     res.send(
  57.       getIndexHtml(publicPath, {
  58.         manualId: options.manualId,
  59.         secured: options.secured,
  60.       })
  61.     );
  62.   });
  63.  
  64.   return router;
  65. }
  66.  
downloadmiddleware.js Source code - Download storybook Source code
Related Source Codes/Software:
ionicons - The premium icon font for Ionic ... 2017-06-07
AsyncDisplayKit - Smooth asynchronous user interfaces for iOS apps. ... 2017-06-07
lottie-android - Render After Effects animations natively on Androi... 2017-06-07
parse-server - Parse-compatible API server module for Node/Expres... 2017-06-07
prettier - Prettier is an opinionated JavaScript formatter. ... 2017-06-08
inferno - An extremely fast, React-like JavaScript library f... 2017-06-08
guetzli - Perceptual JPEG encoder 2017-06-08
cs-video-courses - List of Computer Science courses with video lectur... 2017-06-08
interviews - Everything you need to know to get the job. 2017-06-08
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top