github.com/segakazzz/buffalo@v0.16.22-0.20210119082501-1f52048d3feb/genny/build/_fixtures/coke/webpack.config.js (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 CleanObsoleteChunks(),
    42        new Webpack.ProvidePlugin({$: "jquery",jQuery: "jquery"}),
    43        new MiniCssExtractPlugin({filename: "[name].[contenthash].css"}),
    44        new CopyWebpackPlugin([{from: "./assets",to: ""}], {copyUnmodified: true,ignore: ["css/**", "js/**", "src/**"] }),
    45        new Webpack.LoaderOptionsPlugin({minimize: true,debug: false}),
    46        new ManifestPlugin({fileName: "manifest.json"})
    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: "sass-loader", options: {sourceMap: true}}
    61            ]
    62          },
    63          { test: /\.tsx?$/, use: "ts-loader", exclude: /node_modules/},
    64          { test: /\.jsx?$/,loader: "babel-loader",exclude: /node_modules/ },
    65          { test: /\.(woff|woff2|ttf|svg)(\?v=\d+\.\d+\.\d+)?$/,use: "url-loader"},
    66          { test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,use: "file-loader" },
    67          { test: require.resolve("jquery"),use: "expose-loader?jQuery!expose-loader?$"},
    68          { test: /\.go$/, use: "gopherjs-loader"}
    69        ]
    70      }
    71    },
    72  
    73    buildConfig: function(){
    74      // NOTE: If you are having issues with this not being set "properly", make
    75      // sure your GO_ENV is set properly as `buffalo build` overrides NODE_ENV
    76      // with whatever GO_ENV is set to or "development".
    77      const env = process.env.NODE_ENV || "development";
    78  
    79      var config = {
    80        mode: env,
    81        entry: configurator.entries(),
    82        output: {filename: "[name].[hash].js", path: `${__dirname}/public/assets`},
    83        plugins: configurator.plugins(),
    84        module: configurator.moduleOptions(),
    85        resolve: {
    86          extensions: ['.ts', '.js', '.json']
    87        }
    88      }
    89  
    90      if( env === "development" ){
    91        config.plugins.push(new LiveReloadPlugin({appendScriptTag: true}))
    92        return config
    93      }
    94  
    95      const terser = new TerserPlugin({
    96        terserOptions: {
    97          compress: {},
    98          mangle: {keep_fnames: true},
    99          output: {
   100            comments: false,
   101          },
   102        },
   103        extractComments: false,
   104      })
   105  
   106      config.optimization = {
   107        minimizer: [terser]
   108      }
   109  
   110      return config
   111    }
   112  }
   113  
   114  module.exports = configurator.buildConfig()