github.com/segakazzz/buffalo@v0.16.22-0.20210119082501-1f52048d3feb/genny/assets/webpack/templates/webpack.config.js.tmpl (about)

     1  const Webpack = require("webpack");
     2  const Glob = require("glob");
     3  const CopyWebpackPlugin = require("copy-webpack-plugin");
     4  const MiniCssExtractPlugin = require("mini-css-extract-plugin");
     5  const ManifestPlugin = require("webpack-manifest-plugin");
     6  const CleanObsoleteChunks = require("webpack-clean-obsolete-chunks");
     7  const TerserPlugin = require("terser-webpack-plugin");
     8  const LiveReloadPlugin = require("webpack-livereload-plugin");
     9  
    10  const configurator = {
    11    entries: function(){
    12      var entries = {
    13        application: [
    14          './node_modules/jquery-ujs/src/rails.js',
    15          './assets/css/application.scss',
    16        ],
    17      }
    18  
    19      Glob.sync("./assets/*/*.*").forEach((entry) => {
    20        if (entry === './assets/css/application.scss') {
    21          return
    22        }
    23  
    24        let key = entry.replace(/(\.\/assets\/(src|js|css|go)\/)|\.(ts|js|s[ac]ss|go)/g, '')
    25        if(key.startsWith("_") || (/(ts|js|s[ac]ss|go)$/i).test(entry) == false) {
    26          return
    27        }
    28  
    29        if( entries[key] == null) {
    30          entries[key] = [entry]
    31          return
    32        }
    33  
    34        entries[key].push(entry)
    35      })
    36      return entries
    37    },
    38  
    39    plugins() {
    40      var plugins = [
    41        new Webpack.ProvidePlugin({$: "jquery",jQuery: "jquery"}),
    42        new MiniCssExtractPlugin({filename: "[name].[contenthash].css"}),
    43        new CopyWebpackPlugin([{from: "./assets",to: ""}], {copyUnmodified: true,ignore: ["css/**", "js/**", "src/**"] }),
    44        new Webpack.LoaderOptionsPlugin({minimize: true,debug: false}),
    45        new ManifestPlugin({fileName: "manifest.json"}),
    46        new CleanObsoleteChunks()
    47      ];
    48  
    49      return plugins
    50    },
    51  
    52    moduleOptions: function() {
    53      return {
    54        rules: [
    55          {
    56            test: /\.s[ac]ss$/,
    57            use: [
    58              MiniCssExtractPlugin.loader,
    59              { loader: "css-loader", options: {sourceMap: true}},
    60              { loader: "postcss-loader", options: {sourceMap: true}},
    61              { loader: "sass-loader", options: {sourceMap: true}}
    62            ]
    63          },
    64          { test: /\.tsx?$/, use: "ts-loader", exclude: /node_modules/},
    65          { test: /\.jsx?$/,loader: "babel-loader",exclude: /node_modules/ },
    66          { test: /\.(woff|woff2|ttf|svg)(\?v=\d+\.\d+\.\d+)?$/,use: "url-loader"},
    67          { test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,use: "file-loader" },
    68          { test: require.resolve("jquery"),use: "expose-loader?jQuery!expose-loader?$"},
    69          { test: /\.go$/, use: "gopherjs-loader"}
    70        ]
    71      }
    72    },
    73  
    74    buildConfig: function(){
    75      // NOTE: If you are having issues with this not being set "properly", make
    76      // sure your GO_ENV is set properly as `buffalo build` overrides NODE_ENV
    77      // with whatever GO_ENV is set to or "development".
    78      const env = process.env.NODE_ENV || "development";
    79  
    80      var config = {
    81        mode: env,
    82        entry: configurator.entries(),
    83        output: {filename: "[name].[hash].js", path: `${__dirname}/public/assets`},
    84        plugins: configurator.plugins(),
    85        module: configurator.moduleOptions(),
    86        resolve: {
    87          extensions: ['.ts', '.js', '.json']
    88        }
    89      }
    90  
    91      if( env === "development" ){
    92        config.plugins.push(new LiveReloadPlugin({appendScriptTag: true}))
    93        return config
    94      }
    95  
    96      const terser = new TerserPlugin({
    97        terserOptions: {
    98          compress: {},
    99          mangle: {
   100            keep_fnames: true
   101          },
   102          output: {
   103            comments: false,
   104          },
   105        },
   106        extractComments: false,
   107      })
   108  
   109      config.optimization = {
   110        minimizer: [terser]
   111      }
   112  
   113      return config
   114    }
   115  }
   116  
   117  module.exports = configurator.buildConfig()