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

feat(storybook-config): added webpack sass to storybook config #7471

Merged
merged 3 commits into from
Dec 17, 2020
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
90 changes: 86 additions & 4 deletions config/storybook-preset-carbon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@

'use strict';

const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const customProperties = require('postcss-custom-properties');
const rtlcss = require('rtlcss');

const {
CARBON_REACT_STORYBOOK_USE_CUSTOM_PROPERTIES = 'false',
CARBON_REACT_STORYBOOK_USE_RTL,
NODE_ENV = 'development',
} = process.env;

const useExternalCss = NODE_ENV === 'production';
const useRtl = CARBON_REACT_STORYBOOK_USE_RTL === 'true';

module.exports = {
addons: [
'@storybook/addon-storysource',
Expand All @@ -17,14 +31,40 @@ module.exports = {
'@storybook/addon-notes/register',
'storybook-readme/register',

// Phase 2: port over webpack config for Sass
// Phase 3: port over custom panels/add-ons
],

webpack(config) {
// Configure sass
// sass-loader
// css-loader, style-loader, postcss-loader, ...
const sassLoader = {
loader: 'sass-loader',
options: {
prependData() {
return `
$feature-flags: (
ui-shell: true,
enable-css-custom-properties: ${CARBON_REACT_STORYBOOK_USE_CUSTOM_PROPERTIES},
);
`;
},
sassOptions: {
includePaths: [path.resolve(__dirname, '..', 'node_modules')],
},
sourceMap: true,
},
};

const fastSassLoader = {
loader: 'fast-sass-loader',
options: {
data: `
$feature-flags: (
ui-shell: true,
enable-css-custom-properties: ${CARBON_REACT_STORYBOOK_USE_CUSTOM_PROPERTIES},
);
`,
},
};

config.module.rules.push({
test: /-story\.jsx?$/,
loaders: [
Expand All @@ -45,6 +85,48 @@ module.exports = {
enforce: 'pre',
});

config.module.rules.push({
test: /\.scss$/,
sideEffects: true,
use: [
{
loader: useExternalCss ? MiniCssExtractPlugin.loader : 'style-loader',
},
{
loader: 'css-loader',
options: {
importLoaders: 2,
sourceMap: true,
},
},
{
loader: 'postcss-loader',
options: {
plugins: () => {
const autoPrefixer = require('autoprefixer')({
overrideBrowserslist: ['last 1 version', 'ie >= 11'],
});
return [
customProperties(),
autoPrefixer,
...(useRtl ? [rtlcss] : []),
];
},
sourceMap: true,
},
},
NODE_ENV === 'production' ? sassLoader : fastSassLoader,
],
});

if (useExternalCss) {
config.plugins.push(
new MiniCssExtractPlugin({
filename: '[name].[contenthash].css',
})
);
}

return config;
},
};
79 changes: 0 additions & 79 deletions packages/react/.storybook/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,16 @@
*/

const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const rtlcss = require('rtlcss');
const customProperties = require('postcss-custom-properties');

const {
CARBON_REACT_STORYBOOK_USE_CUSTOM_PROPERTIES = 'false',
CARBON_REACT_STORYBOOK_USE_EXTERNAL_CSS,
CARBON_REACT_USE_CONTROLLED_STATE_WITH_EVENT_LISTENER,
CARBON_REACT_STORYBOOK_USE_RTL,
NODE_ENV = 'development',
} = process.env;

const useExternalCss = NODE_ENV === 'production';
const useControlledStateWithEventListener =
CARBON_REACT_USE_CONTROLLED_STATE_WITH_EVENT_LISTENER === 'true';
const useRtl = CARBON_REACT_STORYBOOK_USE_RTL === 'true';

const replaceTable = {
useControlledStateWithEventListener,
Expand Down Expand Up @@ -74,78 +67,6 @@ module.exports = ({ config, mode }) => {
},
});

const sassLoader = {
loader: 'sass-loader',
options: {
prependData() {
return `
$feature-flags: (
ui-shell: true,
enable-css-custom-properties: ${CARBON_REACT_STORYBOOK_USE_CUSTOM_PROPERTIES},
);
`;
},
sassOptions: {
includePaths: [path.resolve(__dirname, '..', 'node_modules')],
},
sourceMap: true,
},
};

const fastSassLoader = {
loader: 'fast-sass-loader',
options: {
data: `
$feature-flags: (
ui-shell: true,
enable-css-custom-properties: ${CARBON_REACT_STORYBOOK_USE_CUSTOM_PROPERTIES},
);
`,
},
};

config.module.rules.push({
test: /\.scss$/,
sideEffects: true,
use: [
{
loader: useExternalCss ? MiniCssExtractPlugin.loader : 'style-loader',
},
{
loader: 'css-loader',
options: {
importLoaders: 2,
sourceMap: true,
},
},
{
loader: 'postcss-loader',
options: {
plugins: () => {
const autoPrefixer = require('autoprefixer')({
overrideBrowserslist: ['last 1 version', 'ie >= 11'],
});
return [
customProperties(),
autoPrefixer,
...(useRtl ? [rtlcss] : []),
];
},
sourceMap: true,
},
},
NODE_ENV === 'production' ? sassLoader : fastSassLoader,
],
});

if (useExternalCss) {
config.plugins.push(
new MiniCssExtractPlugin({
filename: '[name].[contenthash].css',
})
);
}

config.resolve = {
modules: ['node_modules'],
plugins: [new FeatureFlagProxyPlugin()],
Expand Down