Skip to content

Commit

Permalink
Build CSS assets with Webpack (#26820)
Browse files Browse the repository at this point in the history
* Divide entrypoint and chunk assets into CSS and JS when inserting them into index.html

Until now, Webpack generated only JS assets, but now there are also CSS. When generating index.html
on the server, each type needs a different tag: `<link rel="stylesheet">` vs `<script>`. Divide the
assets into groups and insert appropriate tags.

* Remove unneeded eslint comment

* Switch stylesheets from Webpack chunks

* Use a forked css-extract-plugin that adds data-webpack attribute to links

* Include link to Webpack main stylesheet in desktop

* Webpackify the login section CSS

* Rename and document the flipRTL function
  • Loading branch information
jsnajdr authored Sep 21, 2018
1 parent 86a870f commit 40d13b3
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 64 deletions.
5 changes: 0 additions & 5 deletions assets/stylesheets/sections/login.scss

This file was deleted.

6 changes: 6 additions & 0 deletions client/document/desktop.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ class Desktop extends React.Component {
}
type="text/css"
/>
<link
rel="stylesheet"
type="text/css"
data-webpack={ true }
href={ `/calypso/build.${ isRTL ? 'rtl.css' : 'css' }` }
/>
<link rel="stylesheet" id="desktop-css" href="/desktop/wordpress-desktop.css" />
</Head>
<body className={ classNames( { rtl: isRTL } ) }>
Expand Down
12 changes: 10 additions & 2 deletions client/document/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import getStylesheet from './utils/stylesheet';
import WordPressLogo from 'components/wordpress-logo';
import { jsonStringifyForHtml } from '../../server/sanitize';

const cssChunkLink = asset => (
<link key={ asset } rel="stylesheet" type="text/css" data-webpack={ true } href={ asset } />
);

class Document extends React.Component {
render() {
const {
Expand Down Expand Up @@ -57,6 +61,8 @@ class Document extends React.Component {
inlineScriptNonce,
} = this.props;

const csskey = isRTL ? 'css.rtl' : 'css.ltr';

const inlineScript =
`COMMIT_SHA = ${ jsonStringifyForHtml( commitSha ) };\n` +
( user ? `var currentUser = ${ jsonStringifyForHtml( user ) };\n` : '' ) +
Expand Down Expand Up @@ -93,6 +99,8 @@ class Document extends React.Component {
}
type="text/css"
/>
{ entrypoint[ csskey ].map( cssChunkLink ) }
{ chunkFiles[ csskey ].map( cssChunkLink ) }
{ sectionCss && (
<link
rel="stylesheet"
Expand Down Expand Up @@ -171,10 +179,10 @@ class Document extends React.Component {
} }
/>
) }
{ entrypoint.map( asset => (
{ entrypoint.js.map( asset => (
<script key={ asset } src={ asset } />
) ) }
{ chunkFiles.map( chunk => (
{ chunkFiles.js.map( chunk => (
<script key={ chunk } src={ chunk } />
) ) }
<script nonce={ inlineScriptNonce } type="text/javascript">
Expand Down
34 changes: 34 additions & 0 deletions client/lib/i18n-utils/switch-locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import request from 'superagent';
import i18n from 'i18n-calypso';
import debugFactory from 'debug';
import { map } from 'lodash';

/**
* Internal dependencies
Expand All @@ -30,6 +31,7 @@ function setLocaleInDOM( localeSlug, isRTL ) {
const cssUrl = window.app.staticUrls[ `style${ debugFlag }${ directionFlag }.css` ];

switchCSS( 'main-css', cssUrl );
switchWebpackCSS( isRTL );
}

let lastRequestedLocale = null;
Expand Down Expand Up @@ -105,6 +107,38 @@ export async function switchCSS( elementId, cssUrl ) {
newLink.id = elementId;
}

/*
* CSS links come in two flavors: either RTL stylesheets with `.rtl.css` suffix, or LTR ones
* with `.css` suffix. This function sets a desired `isRTL` flag on the supplied URL, i.e., it
* changes the extension if necessary.
*/
function setRTLFlagOnCSSLink( url, isRTL ) {
if ( isRTL ) {
return url.endsWith( '.rtl.css' ) ? url : url.replace( /\.css$/, '.rtl.css' );
}

return ! url.endsWith( '.rtl.css' ) ? url : url.replace( /\.rtl.css$/, '.css' );
}

function switchWebpackCSS( isRTL ) {
const currentLinks = document.querySelectorAll( 'link[rel="stylesheet"][data-webpack]' );

return map( currentLinks, async currentLink => {
const currentHref = currentLink.getAttribute( 'href' );
const newHref = setRTLFlagOnCSSLink( currentHref, isRTL );
if ( currentHref === newHref ) {
return;
}

const newLink = await loadCSS( newHref, currentLink );
newLink.setAttribute( 'data-webpack', true );

if ( currentLink.parentElement ) {
currentLink.parentElement.removeChild( currentLink );
}
} );
}

/**
* Loads a CSS stylesheet into the page.
*
Expand Down
5 changes: 5 additions & 0 deletions client/login/magic-login/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ import RequestLoginEmailForm from './request-login-email-form';
import GlobalNotices from 'components/global-notices';
import Gridicon from 'gridicons';

/**
* Style dependencies
*/
import './style.scss';

class MagicLogin extends React.Component {
static propTypes = {
path: PropTypes.string.isRequired,
Expand Down
5 changes: 5 additions & 0 deletions client/login/wp-login/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ import {
} from 'state/analytics/actions';
import { withEnhancers } from 'state/utils';

/**
* Style dependencies
*/
import './style.scss';

export class Login extends React.Component {
static propTypes = {
clientId: PropTypes.string,
Expand Down
1 change: 0 additions & 1 deletion client/sections-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export function getSections() {
}

function maybeLoadCSS( sectionName ) {
//eslint-disable-line no-unused-vars
const section = find( sections, { name: sectionName } );

if ( ! ( section && section.css ) ) {
Expand Down
1 change: 0 additions & 1 deletion client/wordpress-com.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,6 @@ sections.push( {
enableLoggedOut: true,
secondary: false,
isomorphic: true,
css: 'login',
} );

sections.push( {
Expand Down
83 changes: 35 additions & 48 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
"lru": "3.1.0",
"lunr": "2.3.3",
"marked": "0.5.0",
"mini-css-extract-plugin-with-rtl": "0.1.3",
"mini-css-extract-plugin-with-rtl": "github:Automattic/mini-css-extract-plugin-with-rtl",
"mkdirp": "0.5.1",
"moment": "2.22.2",
"morgan": "1.9.1",
Expand Down
Loading

0 comments on commit 40d13b3

Please sign in to comment.