Skip to content

Commit

Permalink
ENH Update css webpack config
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Nov 29, 2022
1 parent b441541 commit 36ad197
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 33 deletions.
27 changes: 27 additions & 0 deletions configMeta/cssWebpackConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const BaseWebpackConfig = require('./baseWebpackConfig');
const moduleCSS = require('../css/modules');
const pluginCSS = require('../css/plugins');

/**
* Dynamically generates webpack config for transpiling sass to css using Silverstripe default settings.
*
* IMPORTANT: Instead of setting the output name in the output key, add it as the filename in this constructor.
* Otherwise you will get errors because webpack wants to create a js file for each css file. The js files
* aren't emitted thanks to the IgnoreEmitPlugin, but this happens after chunk validation.
*/
module.exports = class CssWebpackConfig extends BaseWebpackConfig {
constructor(name, ENV, PATHS, filename = 'styles/[name].css') {
super();
this.config = {
name,
output: {
path: PATHS.DIST,
},
devtool: (ENV !== 'production') ? 'source-map' : '',
module: moduleCSS(ENV, PATHS),
plugins: [
...pluginCSS(filename, ENV, PATHS),
],
};
}
}
46 changes: 21 additions & 25 deletions css/modules.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,27 @@
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const Path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

/**
* Exports the settings for css modules in webpack.config
*
* @param {string} ENV Environment to build for, expects 'production' for production and
* anything else for non-production
* @param {string} FILES_PATH The relative path from dist-css file to dist-images/dist-fonts
* @param {string} SRC The path to the source scss files
* @param {string} ROOT The path to the root of the project, this is so we can scope for
* silverstripe-admin variables.scss
* importing silverstripe-admin sass from other modules
* @returns {{rules: [*,*,*,*]}}
*/
module.exports = (ENV, { FILES_PATH, SRC, ROOT }) => {
module.exports = (ENV, { SRC, ROOT }) => {
const useSourceMap = ENV !== 'production';

const cssLoaders = [
{
loader: 'style-loader',
options: {
sourceMap: useSourceMap,
},
},
{
loader: MiniCssExtractPlugin.loader,
},
{
loader: 'css-loader',
options: {
sourceMap: useSourceMap,
minimize: true,
discardComments: true,
},
},
{
Expand All @@ -39,13 +30,14 @@ module.exports = (ENV, { FILES_PATH, SRC, ROOT }) => {
sourceMap: useSourceMap,
postcssOptions: {
plugins: [
require('autoprefixer'),
require('postcss-custom-properties'),
'autoprefixer',
'postcss-custom-properties',
],
},
},
},
].filter(loader => loader);
];

const scssLoaders = [
...cssLoaders,
{
Expand All @@ -65,8 +57,7 @@ module.exports = (ENV, { FILES_PATH, SRC, ROOT }) => {
Path.resolve(ROOT, '../../silverstripe/admin/client/src/styles'),
],
},
implementation: require('sass'),
sourceMap: useSourceMap,
sourceMap: true, // required for resolve-url-loader to be happy
},
},
];
Expand All @@ -84,17 +75,22 @@ module.exports = (ENV, { FILES_PATH, SRC, ROOT }) => {
{
test: /\.(png|gif|jpe?g|svg)$/,
exclude: /fonts[\/\\]([\w_-]+)\.svg$/,
loader: 'url-loader',
options: {
limit: 10000,
name: 'images/[name].[ext]',
type: 'asset',
parser: {
dataUrlCondition: {
maxSize: 10 * 1024 // 10kb
}
},
generator: {
filename: 'images/[name][ext]',
},
},
{
test: /fonts[\/\\]([\w_-]+)\.(woff2?|eot|ttf|svg)$/,
loader: 'file-loader',
options: {
name: 'fonts/[name].[ext]?h=[contenthash]',
// Any .woff, woff2, eot, ttf, or otf file - or svgs specifically in a fonts folder.
test: /(\.(woff2?|eot|ttf|otf)|fonts[\/\\]([\w_-]+)\.svg)$/,
type: 'asset/resource',
generator: {
filename: 'fonts/[name][ext]?h=[contenthash]',
},
},
],
Expand Down
8 changes: 6 additions & 2 deletions css/plugins.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const IgnoreEmitPlugin = require('ignore-emit-webpack-plugin');

/**
* Exports the settings for plugins in webpack.config
* @param {string} filename determines the name of each output CSS file.
* See https://webpack.js.org/plugins/mini-css-extract-plugin/#filename
*/
module.exports = () => ([
module.exports = (filename) => ([
new MiniCssExtractPlugin({
filename: 'styles/[name].css',
filename,
}),
new IgnoreEmitPlugin(/\.js$/),
]);
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ module.exports = {
resolveJS: require('./js/resolve'),

JavascriptWebpackConfig: require('./configMeta/javascriptWebpackConfig'),
CssWebpackConfig: require('./configMeta/cssWebpackConfig'),
};
8 changes: 2 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@
"@sect/modernizr-loader": "^1.0.3",
"autoprefixer": "^10.4.13",
"babel-loader": "^9.0.1",
"core-js": "^3.26.0",
"css-loader": "^6.7.1",
"core-js": "^3.26.0",
"expose-loader": "^4.0.0",
"extract-loader": "^5.1.0",
"file-loader": "^6.2.0",
"ignore-emit-webpack-plugin": "^2.0.6",
"imports-loader": "^4.0.1",
"json-loader": "^0.5.7",
"lodash": "^4.17.21",
Expand All @@ -45,13 +44,10 @@
"postcss-custom-properties": "^12.1.10",
"postcss-load-config": "^4.0.1",
"postcss-loader": "^7.0.1",
"raw-loader": "^4.0.2",
"resolve-url-loader": "^5.0.0",
"sass": "^1.55.0",
"sass-loader": "^13.1.0",
"style-loader": "3.3.1",
"stylelint": "^14.14.0",
"url-loader": "^4.1.1",
"webpack": "^5.74.0",
"webpack-bundle-analyzer": "^4.7.0"
},
Expand Down

0 comments on commit 36ad197

Please sign in to comment.