copy-webpack-plugin: globOptions.ignore not work expected

Bug report

When I use Copy Plugin follow README, it does not work like README said. And I check issues, after use someone’s method, it work, but not work well in sometimes;

Actual Behavior

I use Copy Plugin to copy dir public in my project root dir to webpack output path’s dir public, And I follow the config in README’s globOptions descriptionn

new CopyWebpackPlugin({
			patterns: [
				{
					from: "public/**/*",
					globOptions: {
						ignore: [
							"**/.DS_Store",
							"**/index.html",
							"**/asdasd.md",
						],
					},
				},
			],
		})

it doesn’t work at all. plugin will copy all the files in public;

and after search I found here ignoring-files in Readme.

new CopyWebpackPlugin({
			patterns: [
				{
					from: path.posix.join(
						path.resolve(__dirname, "public").replace(/\\/g, "/"), "**/*"),
					globOptions: {
						ignore: [
							"**/.DS_Store",
							"**/index.html",
							"**/asdasd.md",
						],
					},
				},
			],
		})

but it doesn’t work, still copy all files.

and I found something in issues, try #498 said that I add path.posix function to ignore property, it work, but only work on one file… if I specific more than one file use path.posix, it throw error

new CopyWebpackPlugin({
			patterns: [
				{
					from: "public/**/*",
					globOptions: {
						ignore: [
							"**/.DS_Store",
							path.posix.join(__dirname, "public/index.html"),
							// path.posix.join(__dirname, "public/asdasd.md"), //if open this. will throw following error 
							// `ERROR in unable to locate '/project-root-dir/public/**/*' glob`
						],
					},
				},
			],
		}),

Expected Behavior

It should ignore public/index.html and public/asdasd.md files when copy to webpack output folder.

How Do We Reproduce?

I create a demo repo. just run pnpm install && pnpm run build OR npm install && npm run build.

Please paste the results of npx webpack-cli info here, and mention other relevant information

System: OS: macOS 12.3.1 CPU: (8) x64 Intel® Core™ i7-7700HQ CPU @ 2.80GHz Memory: 38.72 MB / 16.00 GB Binaries: Node: 18.1.0 - ~/.nvs/default/bin/node Yarn: 1.22.18 - /usr/local/bin/yarn npm: 8.8.0 - ~/.nvs/default/bin/npm Browsers: Chrome: 100.0.4896.127 Firefox: 100.0 Safari: 15.4 Packages: copy-webpack-plugin: ^10.2.4 => 10.2.4 webpack: ^5.72.0 => 5.72.0 webpack-cli: ^4.9.2 => 4.9.2

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Comments: 22 (7 by maintainers)

Most upvoted comments

yes, so i need to ignore public/index.html, but globOptions.ignore not working

it’s work for me. just add globalOptions-ignore [**/*.html],

my webpack.common.js

  new webpack.DefinePlugin({
      BASE_URL: JSON.stringify('/'),
    }),
    new HtmlWebpackPlugin({
      // 指定模板,此插件仍然会创建文件
      title: 'Typescript + Vue',
      template: '/public/index.html', // 如果使用自己的模板需要另外引入favicon图标
      //   favicon: '../public/favicon.ico',
    }),

    new CopyPlugin({
      patterns: [
        {
          from: path.resolve(__dirname, '../public'),
          to: path.resolve(__dirname, '../dist'),
          toType: 'dir',
          globOptions: {
            dot: false, // 允许匹配以 . 开头的文件, 比如 .gitignore
            gitignore: false,
            ignore: ['.DS_Store', '**/index.html'],
          },
        },
      ],
    }),

NOTE: html-webpack-plugin, default Specifying the template will also output the html file to the build directory, should you also specify the final output filename

It means globby can’t find directory

Try to experiment, ignore is working, just with some limitations