github.com/wawandco/oxpecker@v1.5.7-0.20210910201653-5958d4afdd89/tools/buffalo/assets/templates/webpack.config.js.tmpl (about)

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