Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. const path = require('path');
  2. const HtmlWebpackPlugin = require('html-webpack-plugin');
  3. const TerserPlugin = require('terser-webpack-plugin');
  4. const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
  5.  
  6. const env = process.env.NODE_ENV;
  7. const publicPath = process.env.PUBLIC_PATH ? `${process.env.PUBLIC_PATH}/` : process.env.PUBLIC_PATH;
  8.  
  9. if (!['development', 'production'].includes(env)) {
  10. throw new Error('Invalid NODE_ENV');
  11. }
  12.  
  13. if (!process.env.LIB_NAME) {
  14. throw new Error('Invalid LIB_NAME');
  15. }
  16.  
  17. module.exports = {
  18. mode: env,
  19. devtool: 'source-map',
  20. entry: path.resolve(__dirname, `./src/${process.env.LIB_NAME}/index.js`),
  21. module: {
  22. rules: [
  23. {
  24. test: /\.js$/i,
  25. loader: 'babel-loader',
  26. exclude: [/node_modules/],
  27. options: {
  28. presets: [
  29. [
  30. '@babel/preset-env',
  31. {
  32. useBuiltIns: 'usage',
  33. },
  34. ],
  35. ],
  36. },
  37. },
  38. {
  39. test: /\.css$/i,
  40. use: ['style-loader', 'css-loader?sourceMap'],
  41. },
  42. {
  43. test: /\.(svg|png|jpe?g|gif)$/i,
  44. use: [
  45. {
  46. loader: 'file-loader',
  47. options: {},
  48. },
  49. ],
  50. },
  51. {
  52. test: /\.(ttf|eot|woff|woff2)$/i,
  53. use: {
  54. loader: 'file-loader',
  55. options: {
  56. name: 'fonts/[name].[ext]',
  57. },
  58. },
  59. },
  60. {
  61. test: /\.html$/,
  62. exclude: /node_modules/,
  63. use: [
  64. {
  65. loader: 'html-loader',
  66. options: {
  67. ignoreCustomFragments: [/\{\{.*?}}/],
  68. root: path.resolve(__dirname, 'src'),
  69. attrs: ['img:src', 'link:href'],
  70. },
  71. },
  72. ],
  73. },
  74. ],
  75. },
  76. output: {
  77. chunkFilename: '[id].[contenthash:8].js',
  78. path: path.resolve(__dirname, `./dist-${process.env.LIB_NAME}`),
  79. publicPath,
  80. library: 'MobileStories',
  81. },
  82. plugins: [
  83. new HtmlWebpackPlugin({
  84. minify: {
  85. removeComments: true,
  86. sortAttributes: true,
  87. keepClosingSlash: true,
  88. collapseWhitespace: true,
  89. },
  90. filename: 'index.html',
  91. template: path.resolve(__dirname, `./src/${process.env.LIB_NAME}/index.html`),
  92. }),
  93. ],
  94. optimization: {
  95. minimizer: [new TerserPlugin(), new OptimizeCSSAssetsPlugin({})],
  96. },
  97. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement