Skip to content

Commit

Permalink
cli: make sure JSS styles receive the highest priority
Browse files Browse the repository at this point in the history
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
  • Loading branch information
Rugvip committed Nov 11, 2021
1 parent f979fcf commit 25f637f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/moody-snails-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@backstage/cli': minor
---

Tweaked style insertion logic to make sure that JSS stylesheets always receive the highest priority.
27 changes: 26 additions & 1 deletion packages/cli/src/lib/bundler/transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@ export const transforms = (options: TransformOptions): Transforms => {

const extraTransforms = isDev ? ['react-hot-loader'] : [];

// This ensures that styles inserted from the style-loader and any
// async style chunks are always given lower priority than JSS styles.
// Note that this function is stringified and executed in the browser
// after transpilation, so stick to simple syntax
function insertBeforeJssStyles(element: any) {
const head = document.head;
// This makes sure that any style elements we insert get put before the
// dynamic styles from JSS, such as the ones from `makeStyles()`.
// TODO(Rugvip): This will likely break in material-ui v5, keep an eye on it.
const firstJssNode = head.querySelector('style[data-jss]');
if (!firstJssNode) {
head.appendChild(element);
} else {
head.insertBefore(element, firstJssNode);
}
}

const loaders = [
{
test: /\.(tsx?)$/,
Expand Down Expand Up @@ -112,7 +129,14 @@ export const transforms = (options: TransformOptions): Transforms => {
{
test: /\.css$/i,
use: [
isDev ? require.resolve('style-loader') : MiniCssExtractPlugin.loader,
isDev
? {
loader: require.resolve('style-loader'),
options: {
insert: insertBeforeJssStyles,
},
}
: MiniCssExtractPlugin.loader,
{
loader: require.resolve('css-loader'),
options: {
Expand All @@ -132,6 +156,7 @@ export const transforms = (options: TransformOptions): Transforms => {
new MiniCssExtractPlugin({
filename: 'static/[name].[contenthash:8].css',
chunkFilename: 'static/[name].[id].[contenthash:8].css',
insert: insertBeforeJssStyles, // Only applies to async chunks
}),
);
}
Expand Down

0 comments on commit 25f637f

Please sign in to comment.