簡體   English   中英

作為 Webpack 構建中的步驟之一,如何將文件從一個位置復制到另一個位置?

[英]How can I copy files from one location to another as one of the steps in a Webpack build?

在 webpack 檢測到文件更改並創建bundle.js 后,我還希望 webpack 像這樣復制文件:

cp dir1/bundle.js dir2/bundle.js

我已經手動測試了 cp 命令,但我希望 webpack 為我做這件事。

目前我的配置文件看起來像這樣,它也能正常工作。

const path = require('path');
const SRC_DIR = path.join(__dirname, '/source');
const DIST_DIR = path.join(__dirname, '/dist');
const webpack = require('webpack');
const exportFunc = ( env ) => {
  return {
    entry: `${SRC_DIR}/index.jsx`,
    output: {
      filename: 'bundle.js',
      path: DIST_DIR
    },
    module: {
      rules: [
        {
          test: /\.css$/,
          use: [ 'style-loader', 'css-loader' ]
        },
        {
          test: /\.jsx?/,
          include: SRC_DIR,
          use: {
            loader: 'babel-loader',
            options: {
              presets: ['@babel/preset-env', '@babel/preset-react'],
              plugins: ['@babel/plugin-proposal-object-rest-spread', '@babel/plugin-proposal-class-properties'],
            }
          }
        }
      ]
    }
  };
};

module.exports = exportFunc;

似乎復制插件就是你所需要的。 文檔: https : //webpack.js.org/plugins/copy-webpack-plugin/

在您的配置中,您將添加:

return {
  plugins: [
    new CopyPlugin({
      {from: 'dir1/bundle.js', to: 'dir2'}
    });
  ]
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM