Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CleanWebpackPlugin in wp-scripts removes built files from previous webpack runs #35980

Closed
kasparsd opened this issue Oct 27, 2021 · 1 comment · Fixed by #35986
Closed

CleanWebpackPlugin in wp-scripts removes built files from previous webpack runs #35980

kasparsd opened this issue Oct 27, 2021 · 1 comment · Fixed by #35986
Assignees
Labels
[Status] In Progress Tracking issues with work in progress [Tool] WP Scripts /packages/scripts [Type] Bug An existing feature does not function as intended

Comments

@kasparsd
Copy link
Contributor

Description

In #23135 @ocean90 introduced the CleanWebpackPlugin plugin for webpack which by default has a feature called cleanStaleWebpackAssets that removes all built files from all of the previous webpack runs.

In instances where the webpack config exports several configurations as an array:

const configOne = { entry: ..., output: ... };
const configTwo = { entry: ..., output: ... };

module.exports = [ configOne, configTwo ];

it causes the webpack run of the second config to remove all the built files from the first run.

Since the CleanWebpackPlugin plugin was added with the intention of removing the built files from entry points with multiple items (per #23134), disabling the cleanStaleWebpackAssets feature would still preserve that desired behaviour as it happens during the cleanOnceBeforeBuildPatterns stage here.

Step-by-step reproduction instructions

  1. Require the @wordpress/scripts package as a dependency for a plugin or theme.
  2. Add a custom webpack.config.js file to the project root which returns two sets of webpack configs with different output.path settings.
  3. Run wp-scripts build and confirm that one of the output paths will sometimes be deleted depending on which webpack config runs first.

Screenshots, screen recording, code snippet

No response

Environment info

  • WordPress 5.8.1

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

@kasparsd
Copy link
Contributor Author

I've created a sample repository https://github.com/xwp/wp-scripts-webpack-clean-bug which has the minimum configuration to illustrate the issue. See this GitHub action run that shows that the js/dist/modern directory is empty after the build:

$ find js/dist
js/dist
js/dist/legacy
js/dist/legacy/legacy.js
js/dist/legacy/legacy.asset.php
js/dist/modern

even though webpack built both:

$ npm run build

> wp-scripts-webpack-clean-bug@ build /home/runner/work/wp-scripts-webpack-clean-bug/wp-scripts-webpack-clean-bug
> wp-scripts build

asset legacy.js 9.01 KiB [emitted] [minimized] (name: legacy)
asset legacy.asset.php 95 bytes [emitted] (name: legacy)
Entrypoint legacy 9.1 KiB = legacy.js 9.01 KiB legacy.asset.php 95 bytes
orphan modules 16.3 KiB [orphan] 1 module
./js/src/legacy.js + 1 modules 16.3 KiB [built] [code generated]
./js/src/modern.js 101 bytes [built] [code generated]
webpack 5.60.0 compiled successfully in 1139 ms

asset modern.asset.php 95 bytes [emitted] (name: modern)
asset modern.js 78 bytes [emitted] [minimized] (name: modern)
Entrypoint modern 173 bytes = modern.js 78 bytes modern.asset.php 95 bytes
./js/src/modern.js 101 bytes [built] [code generated]
webpack 5.60.0 compiled successfully in 696 ms

@github-actions github-actions bot added the [Status] In Progress Tracking issues with work in progress label Oct 27, 2021
@gziolo gziolo added [Tool] WP Scripts /packages/scripts [Type] Bug An existing feature does not function as intended labels Oct 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Status] In Progress Tracking issues with work in progress [Tool] WP Scripts /packages/scripts [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants