Resolve custom application stylesheet with Webpack (#3373)
This implementation is a bit smaller and still has the following benefits: * No need of app/javascript/packs/custom.js For custom stylesheet, it typically has only "require('../styles/custom.scss')" and is redundant. * No need to extract vendor stylesheet to another asset Extracting vendor stylesheet could be forgotten by developers who do not use custom stylesheet.
This commit is contained in:
parent
2212dc4aaa
commit
e98559c3ff
|
@ -1,17 +0,0 @@
|
||||||
# frozen_string_literal: true
|
|
||||||
|
|
||||||
module StyleHelper
|
|
||||||
def stylesheet_for_layout
|
|
||||||
if asset_exist? 'custom.css'
|
|
||||||
'custom'
|
|
||||||
else
|
|
||||||
'application'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def asset_exist?(path)
|
|
||||||
true if Webpacker::Manifest.lookup(path)
|
|
||||||
rescue Webpacker::FileLoader::NotFoundError
|
|
||||||
false
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -2,7 +2,7 @@ const perf = require('./performance');
|
||||||
|
|
||||||
// import default stylesheet with variables
|
// import default stylesheet with variables
|
||||||
require('font-awesome/css/font-awesome.css');
|
require('font-awesome/css/font-awesome.css');
|
||||||
require('../styles/application.scss');
|
require('mastodon-application-style');
|
||||||
|
|
||||||
function onDomContentLoaded(callback) {
|
function onDomContentLoaded(callback) {
|
||||||
if (document.readyState !== 'loading') {
|
if (document.readyState !== 'loading') {
|
||||||
|
|
|
@ -18,8 +18,7 @@
|
||||||
= ' - '
|
= ' - '
|
||||||
= title
|
= title
|
||||||
|
|
||||||
= stylesheet_pack_tag 'common', media: 'all'
|
= stylesheet_pack_tag 'application', media: 'all'
|
||||||
= stylesheet_pack_tag stylesheet_for_layout, media: 'all'
|
|
||||||
= javascript_pack_tag 'common', integrity: true, crossorigin: 'anonymous'
|
= javascript_pack_tag 'common', integrity: true, crossorigin: 'anonymous'
|
||||||
= javascript_pack_tag "locale_#{I18n.locale}", integrity: true, crossorigin: 'anonymous'
|
= javascript_pack_tag "locale_#{I18n.locale}", integrity: true, crossorigin: 'anonymous'
|
||||||
= csrf_meta_tags
|
= csrf_meta_tags
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
/* eslint global-require: 0 */
|
/* eslint global-require: 0 */
|
||||||
/* eslint import/no-dynamic-require: 0 */
|
/* eslint import/no-dynamic-require: 0 */
|
||||||
|
|
||||||
|
const { existsSync } = require('fs');
|
||||||
const webpack = require('webpack');
|
const webpack = require('webpack');
|
||||||
const { basename, dirname, join, relative, resolve, sep } = require('path');
|
const { basename, dirname, join, relative, resolve, sep } = require('path');
|
||||||
const { sync } = require('glob');
|
const { sync } = require('glob');
|
||||||
|
@ -16,6 +17,9 @@ const extensionGlob = `**/*{${paths.extensions.join(',')}}*`;
|
||||||
const packPaths = sync(join(paths.source, paths.entry, extensionGlob));
|
const packPaths = sync(join(paths.source, paths.entry, extensionGlob));
|
||||||
const entryPacks = [].concat(packPaths).concat(localePackPaths);
|
const entryPacks = [].concat(packPaths).concat(localePackPaths);
|
||||||
|
|
||||||
|
const customApplicationStyle = resolve(join(paths.source, 'styles/custom.scss'));
|
||||||
|
const originalApplicationStyle = resolve(join(paths.source, 'styles/application.scss'));
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
entry: entryPacks.reduce(
|
entry: entryPacks.reduce(
|
||||||
(map, entry) => {
|
(map, entry) => {
|
||||||
|
@ -48,17 +52,13 @@ module.exports = {
|
||||||
name: 'common',
|
name: 'common',
|
||||||
minChunks: (module, count) => {
|
minChunks: (module, count) => {
|
||||||
const reactIntlPathRegexp = new RegExp(`node_modules\\${sep}react-intl`);
|
const reactIntlPathRegexp = new RegExp(`node_modules\\${sep}react-intl`);
|
||||||
|
|
||||||
if (module.resource && reactIntlPathRegexp.test(module.resource)) {
|
if (module.resource && reactIntlPathRegexp.test(module.resource)) {
|
||||||
// skip react-intl because it's useless to put in the common chunk,
|
// skip react-intl because it's useless to put in the common chunk,
|
||||||
// e.g. because "shared" modules between zh-TW and zh-CN will never
|
// e.g. because "shared" modules between zh-TW and zh-CN will never
|
||||||
// be loaded together
|
// be loaded together
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const fontAwesomePathRegexp = new RegExp(`node_modules\\${sep}font-awesome`);
|
|
||||||
if (module.resource && fontAwesomePathRegexp.test(module.resource)) {
|
|
||||||
// extract vendor css into common module
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return count >= 2;
|
return count >= 2;
|
||||||
},
|
},
|
||||||
|
@ -66,6 +66,10 @@ module.exports = {
|
||||||
],
|
],
|
||||||
|
|
||||||
resolve: {
|
resolve: {
|
||||||
|
alias: {
|
||||||
|
'mastodon-application-style': existsSync(customApplicationStyle) ?
|
||||||
|
customApplicationStyle : originalApplicationStyle,
|
||||||
|
},
|
||||||
extensions: paths.extensions,
|
extensions: paths.extensions,
|
||||||
modules: [
|
modules: [
|
||||||
resolve(paths.source),
|
resolve(paths.source),
|
||||||
|
|
Reference in a new issue