github.com/gobuffalo/buffalo-cli/v2@v2.0.0-alpha.15.0.20200919213536-a7350c8e6799/cli/internal/plugins/webpack/newapp/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({
    44          patterns: [
    45            { from: "./assets", to: '', globOptions: { ignore: [ "css/**", "js/**", "src/**"] }},
    46          ],
    47        }),
    48        new Webpack.LoaderOptionsPlugin({minimize: true,debug: false}),
    49        new ManifestPlugin({fileName: "manifest.json"}),
    50        new CleanObsoleteChunks()
    51      ];
    52  
    53      return plugins
    54    },
    55  
    56    moduleOptions: function() {
    57      return {
    58        rules: [
    59          {
    60            test: /\.s[ac]ss$/,
    61            use: [
    62              MiniCssExtractPlugin.loader,
    63              { loader: "css-loader", options: {sourceMap: true}},
    64              { loader: "postcss-loader", options: {sourceMap: true}},
    65              { loader: "sass-loader", options: {sourceMap: true}}
    66            ]
    67          },
    68          { test: /\.tsx?$/, use: "ts-loader", exclude: /node_modules/},
    69          { test: /\.jsx?$/,loader: "babel-loader",exclude: /node_modules/ },
    70          { test: /\.(woff|woff2|ttf|svg)(\?v=\d+\.\d+\.\d+)?$/,use: "url-loader"},
    71          { test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,use: "file-loader" },
    72          { test: require.resolve('jquery'), loader: 'expose-loader', options: { exposes: ['$', 'jQuery']}},
    73          { test: /\.go$/, use: "gopherjs-loader"}
    74        ]
    75      }
    76    },
    77  
    78    buildConfig: function(){
    79      // NOTE: If you are having issues with this not being set "properly", make
    80      // sure your GO_ENV is set properly as `buffalo build` overrides NODE_ENV
    81      // with whatever GO_ENV is set to or "development".
    82      const env = process.env.NODE_ENV || "development";
    83  
    84      var config = {
    85        mode: env,
    86        entry: configurator.entries(),
    87        output: {filename: "[name].[hash].js", path: `${__dirname}/public/assets`},
    88        plugins: configurator.plugins(),
    89        module: configurator.moduleOptions(),
    90        resolve: {
    91          extensions: ['.ts', '.js', '.json']
    92        }
    93      }
    94  
    95      if( env === "development" ){
    96        config.plugins.push(new LiveReloadPlugin({appendScriptTag: true}))
    97        return config
    98      }
    99  
   100      const terser = new TerserPlugin({
   101        terserOptions: {
   102          compress: {},
   103          mangle: {
   104            keep_fnames: true
   105          },
   106          output: {
   107            comments: false,
   108          },
   109        },
   110        extractComments: false,
   111      })
   112  
   113      config.optimization = {
   114        minimizer: [terser]
   115      }
   116  
   117      return config
   118    }
   119  }
   120  
   121  module.exports = configurator.buildConfig()