CC 4.0 授權

本節的內容衍生自以下連結的內容,並受 CC BY 4.0 授權約束。

如果沒有特別說明,以下內容可以假定是在原始內容基礎上進行修改和刪除的結果。

CopyRspackPlugin

Rspack 專屬

將個別檔案或整個已存在的目錄複製到建置目錄。

new rspack.CopyRspackPlugin(options);
  • 選項

    • 類型
    export type CopyRspackPluginOptions = {
      patterns: (
        | string // If input is string, it's the same as { from: `your input string` }
        | {
            from: string;
            to?:
              | string
              | ((pathData: {
                  context: string;
                  absoluteFilename?: string;
                }) => string); // Determine based on `from`
            context?: string; // Default to `context` in Rspack config
            toType?: 'dir' | 'file' | 'template'; // Determine based on `from`
            noErrorOnMissing?: boolean; // Default to false
            force?: boolean; // Default to false
            priority?: number; // Default to 0
            globOptions?: {
              caseSensitiveMatch?: boolean; // Default to true
              dot?: boolean; // Default to true
              ignore?: string[]; // ignore specific path
            };
            transform?: (
              input: Buffer,
              absoluteFilename: string,
            ) => string | Buffer | Promise<string> | Promise<Buffer>;
          }
      )[];
    };
    • 預設值: undefined
    名稱類型預設值描述
    from字串undefined複製操作的來源路徑,可以是絕對路徑、相對路徑或 glob 搜尋字串。它可以指向檔案或目錄。如果傳遞相對路徑,則它是相對於 context 設定的。
    to字串 | ((pathData: { context: string; absoluteFilename?: string }) => string)undefined複製操作的目的地,可以是絕對路徑、相對路徑或樣板字串,例如 '[name].[hash][ext]'。如果未指定,則等於輸出路徑。
    context字串undefined此設定決定了「from」路徑的匹配方式以及複製後產生的結構。
    toType'dir'|'file'|'template'undefined指定 to 的類型,可以是目錄、檔案或 rspack 中的樣板名稱。如果未指定,它將自動推斷。
    noErrorOnMissing布林值false如果遺失檔案或目錄,則忽略錯誤。
    force布林值false是否覆寫已存在的資源。
    priority數字0force 設定為 true 時,如果找到匹配的檔案,則優先順序較高的檔案將覆寫優先順序較低的檔案。
    globOptions物件undefinedglob 查詢的設定:caseSensitiveMatch 決定匹配是否區分大小寫,dot 決定是否匹配以 . 開頭的檔案。ignore 是一個 glob 格式的字串陣列,可用於忽略特定路徑。
    transform函式undefined允許修改檔案內容。

例如

rspack.config.js
const rspack = require('@rspack/core');
module.exports = {
  entry: './src/index.js',
  plugins: [
    new rspack.CopyRspackPlugin({
      patterns: [
        {
          from: 'file.txt',
        },
      ],
    }),
  ],
};

使用上述設定執行的結果將會是:"dist/file.txt"

rspack.config.js
const rspack = require('@rspack/core');
module.exports = {
  entry: './src/index.js',
  plugins: [
    new rspack.CopyRspackPlugin({
      patterns: [
        {
          from: 'directory',
        },
      ],
    }),
  ],
};

使用上述設定執行的結果將會是:directory 內部的檔案和目錄將被放置在輸出路徑。

rspack.config.js
const rspack = require('@rspack/core');
module.exports = {
  entry: './src/index.js',
  plugins: [
    new rspack.CopyRspackPlugin({
      patterns: [
        {
          from: 'directory/**/*',
          to: 'newdirectory',
        },
      ],
    }),
  ],
};

使用上述設定執行的結果將會是:directory 資料夾將被移動到輸出資料夾內的 newdirectory 資料夾中,例如 dist/newdirectory/directory/foo