Advertisement
Guest User

Untitled

a guest
May 27th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JSON 6.18 KB | None | 0 0
  1. const path = require('path');
  2. const dotenv = require('dotenv');
  3. const fs = require('fs');
  4. const webpack = require('webpack');
  5. const getApiURL = require('./webpack/conf/api.conf');
  6.  
  7. const ExtractTextPlugin = require('extract-text-webpack-plugin');
  8. const HtmlWebpackPlugin = require('html-webpack-plugin');
  9. const HtmlWebpackHarddiskPlugin = require('html-webpack-harddisk-plugin');
  10. const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
  11.  
  12. const extractLess = new ExtractTextPlugin({
  13.   filename: '[name].[contenthash].css',
  14.   disable: process.env.NODE_ENV === 'development'
  15. });
  16. // const indexHTMLFile = new HtmlWebpackPlugin({
  17. //   template: 'index.pug',
  18. //   filename: '../index.html'
  19. // });
  20.  
  21. const isProd = process.env.NODE_ENV === 'production';
  22. const isDev = !isProd;
  23. const isLocal = process.env.LOCAL ? process.env.LOCAL === 'true' : true;
  24. const sourceRoot = './src';
  25. const publicPath = '/assets/';
  26.  
  27. //* ENVIROMENT SETTINGS
  28. const dotEnvPath = path.resolve(__dirname, '.env');
  29. if (!fs.existsSync(dotEnvPath)) {
  30.   fs.writeFileSync(dotEnvPath, '');
  31. }
  32. const dotEnvConfig = dotenv.load();
  33. const ENV = dotEnvConfig.parsed;
  34.  
  35. if (!ENV.REDUX_STATE_VERSION) ENV.REDUX_STATE_VERSION = 0;
  36. fs.writeFileSync(dotEnvPath, ''); // clear file
  37.  
  38. Object.keys(ENV).forEach((key) => {
  39.   let value;
  40.   if (key === 'REDUX_STATE_VERSION') {
  41.     value = Number(ENV[key], 10) + 1;
  42.   } else {
  43.     value = ENV[key];
  44.   }
  45.   fs.appendFileSync(dotEnvPath, `${key}=${value}\n`);
  46. });
  47.  
  48. const setEnvironment = new webpack.DefinePlugin({
  49.   'process.env.API_URL': getApiURL(isDev, isLocal),
  50.   'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
  51.   'process.env.REDUX_STATE_VERSION': ENV.REDUX_STATE_VERSION
  52. });
  53. //* ENVIROMENT SETTINGS
  54.  
  55. module.exports = {
  56.   context: path.resolve(__dirname, sourceRoot),
  57.   entry: {
  58.     index: './index.ejs',
  59.     vendor: [
  60.       'babel-polyfill',
  61.       'react',
  62.       'react-dom',
  63.       'react-redux',
  64.       'react-router',
  65.       'redux',
  66.       'redux-thunk',
  67.       'chart.js',
  68.       'redux-form',
  69.       'react-datepicker',
  70.       'react-bootstrap'
  71.     ]
  72.   },
  73.   watchOptions: {
  74.     ignored: /node_modules/
  75.   },
  76.   resolve: {
  77.     extensions: ['.js', '.less'],
  78.     modules: [path.resolve(__dirname, `${sourceRoot}/js`), 'node_modules'],
  79.     alias: {
  80.       img: path.resolve(__dirname, `${sourceRoot}/img`),
  81.       styles: path.resolve(__dirname, `${sourceRoot}/styles/share`)
  82.     }
  83.   },
  84.   devtool: isDev ? 'source-map' : '',
  85.   output: {
  86.     path: path.resolve(__dirname, './dist/assets'),
  87.     filename: 'index.[hash].js',
  88.     publicPath
  89.   },
  90.   devServer: {
  91.     contentBase: path.resolve(__dirname, sourceRoot)
  92.   },
  93.   module: {
  94.     rules: [
  95.       {
  96.         test: /\.jsx?$/,
  97.         exclude: /node_modules/,
  98.         loader: 'babel',
  99.  
  100.         query: {
  101.           presets: ['es2015', 'react', 'stage-2']
  102.         }
  103.       },
  104.       { test: /\.ejs$/, loader: 'ejs-loader' },
  105.       {
  106.         test: /\.js$/,
  107.         exclude: [/node_modules/],
  108.         use: [
  109.           {
  110.             loader: 'babel-loader',
  111.             options: {
  112.               plugins: [
  113.                 'transform-decorators-legacy',
  114.                 'transform-class-properties'
  115.               ],
  116.               presets: ['latest', 'stage-0', 'react', 'es2015']
  117.             }
  118.           }
  119.         ]
  120.       },
  121.       {
  122.         test: /\.less$/,
  123.         include: [path.resolve(__dirname, sourceRoot, 'js')],
  124.         use: [
  125.           {
  126.             loader: 'style-loader', // creates style nodes from JS strings
  127.             options: {
  128.               sourceMap: true
  129.             }
  130.           },
  131.           {
  132.             loader: 'css-loader', // translates CSS into CommonJS
  133.             options: {
  134.               modules: true,
  135.               localIdentName: '[local]___[hash:base64:5]'
  136.             }
  137.           },
  138.           {
  139.             loader: 'less-loader' // compiles Less
  140.           }
  141.         ]
  142.       },
  143.       {
  144.         test: /\.less$/,
  145.         exclude: [path.resolve(__dirname, sourceRoot, 'js')],
  146.         use: [
  147.           { loader: 'style-loader' ,options: {
  148.             insertAt:'top'
  149.           }},
  150.           { loader: 'css-loader' },
  151.           { loader: 'less-loader' }
  152.         ]
  153.       },
  154.       {
  155.         test: /.(ttf|otf|eot|woff(2)?)(\?[a-z0-9]+)?$/,
  156.         loader: 'file-loader?name=fonts/[name].[ext]'
  157.       },
  158.       {
  159.         test: /\.(jpe?g|png|gif|svg)$/i,
  160.         use: [
  161.           {
  162.             loader: 'file-loader',
  163.             query: {
  164.               name: 'images/[name]-[hash].[ext]',
  165.               publicPath
  166.             }
  167.           },
  168.           {
  169.             loader: 'image-webpack-loader',
  170.             query: {
  171.               mozjpeg: {
  172.                 progressive: true
  173.               },
  174.               gifsicle: {
  175.                 interlaced: false
  176.               },
  177.               optipng: {
  178.                 optimizationLevel: 4
  179.               },
  180.               pngquant: {
  181.                 quality: '75-90',
  182.                 speed: 3
  183.               }
  184.             }
  185.           }
  186.         ]
  187.       },
  188.       {
  189.         test: /\.pug$/,
  190.         loader: 'pug-loader'
  191.       }
  192.     ]
  193.   },
  194.   plugins: [
  195.     extractLess,
  196.     setEnvironment,
  197.     new HtmlWebpackPlugin({
  198.       template: 'index.ejs',
  199.       inject: true,
  200.     }),
  201.     new HtmlWebpackHarddiskPlugin({
  202.       outputPath: path.resolve(__dirname, sourceRoot)
  203.     }),
  204.     new webpack.optimize.CommonsChunkPlugin({
  205.       name: 'vendor',
  206.       filename: 'vendor.[chunkhash].js',
  207.       minChunks: Infinity
  208.     }),
  209.     new webpack.optimize.CommonsChunkPlugin({ name: 'meta', chunks: ['vendor'], filename: 'meta.[hash].js' }),
  210.     new webpack.NamedModulesPlugin(),
  211.     new HtmlWebpackPlugin(),
  212.     new webpack.optimize.ModuleConcatenationPlugin(),
  213.     // new webpack.optimize.CommonsChunkPlugin({
  214.     //   name: 'vendor',
  215.     //   filename: 'vendor.[hash].js',
  216.     //   minChunks(module) {
  217.     //     return module.context &&
  218.     //            module.context.indexOf('node_modules') >= 0;
  219.     //   }
  220.     // }),
  221.     new webpack.DefinePlugin({
  222.       'process.env': {
  223.           'NODE_ENV': JSON.stringify('production')
  224.       }
  225.   }),
  226.     new UglifyJsPlugin()
  227.    
  228.   //   new webpack.HashedModuleIdsPlugin()
  229.   ]
  230. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement