-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Scripts: Fix style.css handling in the build and start commands (#23127)
* Scripts: Fix style.css handling in the build and start commands * Scripts: Fix style.css handling in the build and start commands * Docs: Add changelog entry * Update packages/scripts/config/webpack.config.js Co-authored-by: Dominik Schilling <dominikschilling+git@gmail.com> * Docs: Add tweaks to the CSS support description Co-authored-by: Dominik Schilling <dominikschilling+git@gmail.com>
- Loading branch information
Showing
5 changed files
with
65 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
const { ConcatSource } = require( 'webpack-sources' ); | ||
const IgnoreEmitWebpackPlugin = require( 'ignore-emit-webpack-plugin' ); | ||
|
||
// This is a simple webpack plugin to merge the JS files generated for styles by | ||
// MiniCssExtractPlugin into their corresponding entrypoints. | ||
// Despited basically being noop files, they are required to get the real JS | ||
// files to load, silently failing without them. | ||
// @see https://github.com/webpack-contrib/mini-css-extract-plugin/issues/85 | ||
class FixStyleWebpackPlugin { | ||
constructor() { | ||
// Offload removing JS assets for styles the ExternalsPlugin. | ||
this.ignoreEmitPlugin = new IgnoreEmitWebpackPlugin( /style-(.*).js/ ); | ||
} | ||
|
||
apply( compiler ) { | ||
compiler.hooks.compilation.tap( | ||
'FixStyleWebpackPlugin', | ||
( compilation ) => { | ||
compilation.hooks.optimizeChunkAssets.tap( | ||
'FixStyleWebpackPlugin', | ||
( chunks ) => { | ||
for ( const chunk of chunks ) { | ||
if ( ! chunk.canBeInitial() ) { | ||
continue; | ||
} | ||
|
||
const file = `${ chunk.name }.js`; | ||
const styleFile = `style-${ chunk.name }.js`; | ||
const styleFileAsset = compilation.getAsset( | ||
styleFile | ||
); | ||
if ( ! styleFileAsset ) { | ||
continue; | ||
} | ||
compilation.updateAsset( file, ( oldSource ) => { | ||
return new ConcatSource( | ||
oldSource, | ||
'\n\n' + styleFileAsset.source.source() | ||
); | ||
} ); | ||
} | ||
} | ||
); | ||
} | ||
); | ||
|
||
this.ignoreEmitPlugin.apply( compiler ); | ||
} | ||
} | ||
|
||
module.exports = FixStyleWebpackPlugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters