Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extending wp scripts to handle scss #14847

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 74 additions & 6 deletions packages/scripts/config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
const { BundleAnalyzerPlugin } = require( 'webpack-bundle-analyzer' );
const LiveReloadPlugin = require( 'webpack-livereload-plugin' );
const path = require( 'path' );
const postcssPresetEnv = require( 'postcss-preset-env' );
const MiniCssExtractPlugin = require( 'mini-css-extract-plugin' );

/**
* Internal dependencies
Expand Down Expand Up @@ -66,11 +68,14 @@ const externals = [
const isProduction = process.env.NODE_ENV === 'production';
const mode = isProduction ? 'production' : 'development';

const getBabelLoaderOptions = () => hasBabelConfig() ? {} : {
babelrc: false,
configFile: false,
presets: [ require.resolve( '@wordpress/babel-preset-default' ) ],
};
const getBabelLoaderOptions = () =>
hasBabelConfig() ?
{} :
{
babelrc: false,
configFile: false,
presets: [ require.resolve( '@wordpress/babel-preset-default' ) ],
};

const config = {
mode,
Expand All @@ -87,6 +92,29 @@ const config = {
'lodash-es': 'lodash',
},
},
optimization: {
splitChunks: {
chunks: 'all',
cacheGroups: {
editor: {
name: 'editor',
test: /editor\.(sc|sa|c)ss$/,
enforce: true,
},
style: {
name: 'styles',
test: /style\.(sc|sa|c)ss$/,
enforce: true,
},
theme: {
name: 'theme',
test: /theme\.(sc|sa|c)ss$/,
enforce: true,
},
default: false,
},
},
},
module: {
rules: [
{
Expand All @@ -102,15 +130,55 @@ const config = {
options: getBabelLoaderOptions(),
},
},
{
test: /\.(sc|sa|c)ss$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
},
{
loader: require.resolve( 'css-loader' ),
},
{
loader: require.resolve( 'sass-loader' ),
},
{
loader: require.resolve( 'postcss-loader' ),
options: {
ident: 'postcss',
plugins: () => [
postcssPresetEnv( {
// Start with a stage.
stage: 3,
// Override specific features you do (or don't) want.
features: {
// And, optionally, configure rules/features as needed.
'custom-media-queries': {
preserve: false,
},
'custom-properties': {
preserve: true,
},
'nesting-rules': true,
},
} ),
],
},
},
],
},
],
},
plugins: [
// WP_BUNDLE_ANALYZER global variable enables utility that represents bundle content
// as convenient interactive zoomable treemap.
process.env.WP_BUNDLE_ANALYZER && new BundleAnalyzerPlugin(),
// MiniCssExtractPlugin to extract the css thats gets imported into javascript
new MiniCssExtractPlugin(),
// WP_LIVE_RELOAD_PORT global variable changes port on which live reload works
// when running watch mode.
! isProduction && new LiveReloadPlugin( { port: process.env.WP_LIVE_RELOAD_PORT || 35729 } ),
! isProduction &&
new LiveReloadPlugin( { port: process.env.WP_LIVE_RELOAD_PORT || 35729 } ),
].filter( Boolean ),
stats: {
children: false,
Expand Down
6 changes: 6 additions & 0 deletions packages/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,19 @@
"chalk": "^2.4.1",
"check-node-version": "^3.1.1",
"cross-spawn": "^5.1.0",
"css-loader": "^2.1.1",
"eslint": "^5.12.1",
"jest": "^24.1.0",
"jest-puppeteer": "^4.0.0",
"mini-css-extract-plugin": "^0.6.0",
"node-sass": "^4.11.0",
"npm-package-json-lint": "^3.6.0",
"postcss-loader": "^3.0.0",
"postcss-preset-env": "^6.6.0",
"puppeteer": "1.6.1",
"read-pkg-up": "^1.0.1",
"resolve-bin": "^0.4.0",
"sass-loader": "^7.1.0",
"source-map-loader": "^0.2.4",
"stylelint": "^9.10.1",
"stylelint-config-wordpress": "^13.1.0",
Expand Down