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

Adding support for CSS Modules #65

Merged
merged 3 commits into from
May 22, 2021
Merged
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
162 changes: 150 additions & 12 deletions packages/toolkit/config/__tests__/__snapshots__/webpack.config.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Object {
],
},
Object {
"include": undefined,
"exclude": /\\\\\\.module\\\\\\.css\\$/,
"test": /\\\\\\.css\\$/,
"use": Array [
Object {
Expand All @@ -88,6 +88,7 @@ Object {
],
},
Object {
"exclude": /\\\\\\.module\\\\\\.css\\$/,
"test": /\\\\\\.\\(sc\\|sa\\)ss\\$/,
"use": Array [
Object {
Expand All @@ -100,6 +101,29 @@ Object {
"url": true,
},
},
Object {
"loader": "/node_modules/sass-loader/dist/cjs.js",
"options": Object {
"sourceMap": true,
},
},
],
},
Object {
"test": /\\\\\\.module\\\\\\.css\\$/,
"use": Array [
Object {
"loader": "/node_modules/mini-css-extract-plugin/dist/loader.js",
},
Object {
"loader": "/node_modules/css-loader/dist/cjs.js",
"options": Object {
"import": false,
"modules": true,
"sourceMap": true,
"url": true,
},
},
Object {
"loader": "/node_modules/postcss-loader/dist/cjs.js",
"options": Object {
Expand Down Expand Up @@ -203,10 +227,7 @@ Object {
],
},
Object {
"include": Array [
"/assets2/css",
"/includes2/blocks",
],
"exclude": /\\\\\\.module\\\\\\.css\\$/,
"test": /\\\\\\.css\\$/,
"use": Array [
Object {
Expand All @@ -228,6 +249,7 @@ Object {
],
},
Object {
"exclude": /\\\\\\.module\\\\\\.css\\$/,
"test": /\\\\\\.\\(sc\\|sa\\)ss\\$/,
"use": Array [
Object {
Expand All @@ -240,6 +262,29 @@ Object {
"url": false,
},
},
Object {
"loader": "/node_modules/sass-loader/dist/cjs.js",
"options": Object {
"sourceMap": true,
},
},
],
},
Object {
"test": /\\\\\\.module\\\\\\.css\\$/,
"use": Array [
Object {
"loader": "/node_modules/mini-css-extract-plugin/dist/loader.js",
},
Object {
"loader": "/node_modules/css-loader/dist/cjs.js",
"options": Object {
"import": false,
"modules": true,
"sourceMap": true,
"url": false,
},
},
Object {
"loader": "/node_modules/postcss-loader/dist/cjs.js",
"options": Object {
Expand Down Expand Up @@ -389,7 +434,7 @@ Object {
],
},
Object {
"include": undefined,
"exclude": /\\\\\\.module\\\\\\.css\\$/,
"test": /\\\\\\.css\\$/,
"use": Array [
Object {
Expand All @@ -413,6 +458,7 @@ Object {
],
},
Object {
"exclude": /\\\\\\.module\\\\\\.css\\$/,
"test": /\\\\\\.\\(sc\\|sa\\)ss\\$/,
"use": Array [
Object {
Expand All @@ -425,6 +471,29 @@ Object {
"url": true,
},
},
Object {
"loader": "/node_modules/sass-loader/dist/cjs.js",
"options": Object {
"sourceMap": true,
},
},
],
},
Object {
"test": /\\\\\\.module\\\\\\.css\\$/,
"use": Array [
Object {
"loader": "/node_modules/mini-css-extract-plugin/dist/loader.js",
},
Object {
"loader": "/node_modules/css-loader/dist/cjs.js",
"options": Object {
"import": false,
"modules": true,
"sourceMap": true,
"url": true,
},
},
Object {
"loader": "/node_modules/postcss-loader/dist/cjs.js",
"options": Object {
Expand Down Expand Up @@ -565,7 +634,7 @@ Object {
],
},
Object {
"include": undefined,
"exclude": /\\\\\\.module\\\\\\.css\\$/,
"test": /\\\\\\.css\\$/,
"use": Array [
Object {
Expand All @@ -589,6 +658,7 @@ Object {
],
},
Object {
"exclude": /\\\\\\.module\\\\\\.css\\$/,
"test": /\\\\\\.\\(sc\\|sa\\)ss\\$/,
"use": Array [
Object {
Expand All @@ -601,6 +671,29 @@ Object {
"url": true,
},
},
Object {
"loader": "/node_modules/sass-loader/dist/cjs.js",
"options": Object {
"sourceMap": true,
},
},
],
},
Object {
"test": /\\\\\\.module\\\\\\.css\\$/,
"use": Array [
Object {
"loader": "/node_modules/mini-css-extract-plugin/dist/loader.js",
},
Object {
"loader": "/node_modules/css-loader/dist/cjs.js",
"options": Object {
"import": false,
"modules": true,
"sourceMap": true,
"url": true,
},
},
Object {
"loader": "/node_modules/postcss-loader/dist/cjs.js",
"options": Object {
Expand Down Expand Up @@ -754,7 +847,7 @@ Object {
],
},
Object {
"include": undefined,
"exclude": /\\\\\\.module\\\\\\.css\\$/,
"test": /\\\\\\.css\\$/,
"use": Array [
Object {
Expand All @@ -778,6 +871,7 @@ Object {
],
},
Object {
"exclude": /\\\\\\.module\\\\\\.css\\$/,
"test": /\\\\\\.\\(sc\\|sa\\)ss\\$/,
"use": Array [
Object {
Expand All @@ -790,6 +884,29 @@ Object {
"url": true,
},
},
Object {
"loader": "/node_modules/sass-loader/dist/cjs.js",
"options": Object {
"sourceMap": true,
},
},
],
},
Object {
"test": /\\\\\\.module\\\\\\.css\\$/,
"use": Array [
Object {
"loader": "/node_modules/mini-css-extract-plugin/dist/loader.js",
},
Object {
"loader": "/node_modules/css-loader/dist/cjs.js",
"options": Object {
"import": false,
"modules": true,
"sourceMap": true,
"url": true,
},
},
Object {
"loader": "/node_modules/postcss-loader/dist/cjs.js",
"options": Object {
Expand Down Expand Up @@ -913,10 +1030,7 @@ Object {
],
},
Object {
"include": Array [
"/assets2/css",
"/includes2/blocks",
],
"exclude": /\\\\\\.module\\\\\\.css\\$/,
"test": /\\\\\\.css\\$/,
"use": Array [
Object {
Expand All @@ -940,6 +1054,7 @@ Object {
],
},
Object {
"exclude": /\\\\\\.module\\\\\\.css\\$/,
"test": /\\\\\\.\\(sc\\|sa\\)ss\\$/,
"use": Array [
Object {
Expand All @@ -952,6 +1067,29 @@ Object {
"url": false,
},
},
Object {
"loader": "/node_modules/sass-loader/dist/cjs.js",
"options": Object {
"sourceMap": true,
},
},
],
},
Object {
"test": /\\\\\\.module\\\\\\.css\\$/,
"use": Array [
Object {
"loader": "/node_modules/mini-css-extract-plugin/dist/loader.js",
},
Object {
"loader": "/node_modules/css-loader/dist/cjs.js",
"options": Object {
"import": false,
"modules": true,
"sourceMap": true,
"url": false,
},
},
Object {
"loader": "/node_modules/postcss-loader/dist/cjs.js",
"options": Object {
Expand Down
66 changes: 43 additions & 23 deletions packages/toolkit/config/webpack/modules.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,17 @@
const path = require('path');
const MiniCSSExtractPlugin = require('mini-css-extract-plugin');

const { hasBabelConfig, hasPostCSSConfig, fromConfigRoot } = require('../../utils');

module.exports = ({
isProduction,
isPackage,
defaultTargets,
projectConfig: { wordpress, paths },
}) => {
const cssLoaders = [
const getCSSLoaders = ({ options, postcss, sass }) => {
return [
{
loader: MiniCSSExtractPlugin.loader,
},
{
loader: require.resolve('css-loader'),
options: {
sourceMap: !isProduction,
// Local files like fonts etc. are copied using CopyWebpackPlugin when in project mode.
url: isPackage,
},
options,
},
{
postcss && {
loader: require.resolve('postcss-loader'),
options: {
postcssOptions: {
Expand All @@ -33,8 +23,16 @@ module.exports = ({
},
},
},
];
sass && {
loader: require.resolve('sass-loader'),
options: {
sourceMap: options ? options.sourceMap : false,
},
},
].filter(Boolean);
};

module.exports = ({ isProduction, isPackage, defaultTargets, projectConfig: { wordpress } }) => {
return {
rules: [
{
Expand Down Expand Up @@ -78,21 +76,43 @@ module.exports = ({
},
{
test: /\.css$/,
include: isPackage
? undefined
: paths.cssLoaderPaths.map((cssPath) => path.resolve(process.cwd(), cssPath)),
use: cssLoaders,
use: getCSSLoaders({
options: {
sourceMap: !isProduction,
url: isPackage,
},
postcss: true,
sass: false,
}),
exclude: /\.module\.css$/,
},
{
test: /\.(sc|sa)ss$/,
use: [
...cssLoaders,
{
loader: require.resolve('sass-loader'),
...getCSSLoaders({
options: {
sourceMap: !isProduction,
url: isPackage,
},
},
postcss: false,
sass: true,
}),
],
exclude: /\.module\.css$/,
},
{
test: /\.module\.css$/,
use: [
...getCSSLoaders({
options: {
sourceMap: !isProduction,
url: isPackage,
import: false,
modules: true,
},
postcss: true,
sass: true,
}),
],
},
// when in package module only include referenced resources
Expand Down
4 changes: 4 additions & 0 deletions projects/10up-theme/assets/js/shared/shared.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// eslint-disable-next-line import/no-unresolved
import Accordion from '@10up/component-accordion';
import styles from './shared.module.css';

console.log(styles);

new Accordion('.test', {});
Loading