A
A
Andy Oker2021-07-26 20:34:58
webpack
Andy Oker, 2021-07-26 20:34:58

How to make a text file appear in the final assembly ([email protected])?

The file structure has:

dist
src
      -static
            -file.txt


webpack configuration
const webpack = require('webpack');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const merge = require('webpack-merge');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const isProduction = process.env.NODE_ENV === 'production';
const path = require('path');


let config = {
  mode: isProduction ? 'production' : 'development',
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader'
      },
      {
        test: /\.scss$/,
        use: [
          'vue-style-loader',
          'css-loader',
          'sass-loader'
        ]
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: file => /node_modules/.test(file) && !/\.vue\.js/.test(file),
      },
      {
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
        use: {
          loader: 'url-loader',
          options: {
            limit: 10000,
            name: 'images/[name].[hash:8].[ext]',
            publicPath: 'dist/',
          },
        },
      },
      {
        test: /\.(eot|ttf|woff|woff2)$/,
        loader: 'url-loader',
        options: {
            limit: 10000,
            name: '[name].[hash:8].[ext]',
            publicPath: 'dist/',
        },
      }
    ],
  },
  plugins: [
    new VueLoaderPlugin(),
    new webpack.DefinePlugin({
      'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
    }),
    new rawLoader()
  ],
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "./src"), // After this configuration @ can point to the src directory
    },
  }
};

if (isProduction) {
  config = merge(config, {
    optimization: {
      minimize: true,
      minimizer: [new OptimizeCSSAssetsPlugin(), new TerserPlugin()],
    },
  });
}


module.exports = config;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Egor Danchenko, 2021-07-26
@YahorDanchanka

Solution for Webpack 5

module: {
  rules: [
    {
      test: /\.txt/,
      type: 'asset/source',
    }
  ]
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question