Skip to content
Open
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
14 changes: 7 additions & 7 deletions src/index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { logger } from '@storybook/node-logger';
import { webpack } from './index.ts';
import { webpackFinal } from './index.ts';

jest.mock('@storybook/node-logger');

Expand All @@ -24,22 +24,22 @@ describe('webpack hook', () => {
rules: ['dummy-loader'],
},
};
const config = webpack(configFixture);
const config = webpackFinal(configFixture);
expect(config.module.rules[0]).toEqual('dummy-loader');
expect(config.module.rules[1]).toMatchObject(loaderMatcher);
});

test('applying to an empty webpack config', () => {
const configFixture = {};
const config = webpack(configFixture);
const config = webpackFinal(configFixture);
expect(config.module.rules[0]).toMatchObject(loaderMatcher);
});

test('accepts a custom postcss through postcssLoaderOptions & logs the version being used', () => {
const version = '99.99.99';
const configFixture = {};
const fakePostcss = () => ({ version });
const config = webpack(configFixture, {
const config = webpackFinal(configFixture, {
postcssLoaderOptions: {
implementation: fakePostcss,
},
Expand All @@ -61,7 +61,7 @@ describe('webpack hook', () => {

test('always ensures importLoaders: 1 on css-loader', () => {
const configFixture = {};
const config = webpack(configFixture, {
const config = webpackFinal(configFixture, {
cssLoaderOptions: {
importLoaders: 2,
},
Expand All @@ -80,7 +80,7 @@ describe('webpack hook', () => {

test('disables all loaders if their options are set to false', () => {
const configFixture = {};
const config = webpack(configFixture, {
const config = webpackFinal(configFixture, {
styleLoaderOptions: false,
cssLoaderOptions: false,
postcssLoaderOptions: false,
Expand All @@ -92,7 +92,7 @@ describe('webpack hook', () => {

test('overrides rule properties with option', () => {
const configFixture = {};
const config = webpack(configFixture, {
const config = webpackFinal(configFixture, {
rule: {
sideEffects: false,
},
Expand Down
55 changes: 38 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function wrapLoader(
return [{ loader, options }];
}

export const webpack = (
export const webpackFinal = (
webpackConfig: Configuration = {},
options: Options = {},
): Configuration => {
Expand All @@ -57,26 +57,47 @@ export const webpack = (

logger.info(`=> Using PostCSS preset with postcss@${version}`);

const { rules = [] } = webpackConfig.module || {};

const prevCssRuleIdx = rules.findIndex(
({ test }) => test?.toString() === '/\\.css$/',
);
const cssRuleIdx = prevCssRuleIdx === -1 ? rules.length : prevCssRuleIdx;
const rulesBefore = rules.slice(0, cssRuleIdx);
const rulesAfter = rules.slice(cssRuleIdx + 1);

const cssRule = {
test: /\.css$/,
sideEffects: true,
exclude: /\.module\.css$/,
...rule,
use: [
...wrapLoader(require.resolve('style-loader'), styleLoaderOptions),
...wrapLoader(require.resolve('css-loader'), cssLoaderOptions),
...wrapLoader(require.resolve('postcss-loader'), postcssLoaderOptions),
],
};

const cssModulesRule = {
test: /\.css$/,
include: /\.module\.css$/,
sideEffects: true,
...rule,
use: [
...wrapLoader(require.resolve('style-loader'), styleLoaderOptions),
...wrapLoader(require.resolve('css-loader'), {
...cssLoaderOptions,
modules: true,

@unpunnyfuns unpunnyfuns Nov 16, 2021

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice if there was a way to configure the localIdentName, like so

[
  {
    name: "@storybook/addon-postcss",
    options: {
      postcssLoaderOptions: {
        implementation: require("postcss"),
      },
      cssLoaderOptions: {
        modules: {
          localIdentName: "[path][name]__[local]",
        },
      },
    },
  },
]

}),
...wrapLoader(require.resolve('postcss-loader'), postcssLoaderOptions),
],
};

return {
...webpackConfig,
module: {
...webpackConfig.module,
rules: [
...(webpackConfig.module?.rules ?? []),
{
test: /\.css$/,
sideEffects: true,
...rule,
use: [
...wrapLoader(require.resolve('style-loader'), styleLoaderOptions),
...wrapLoader(require.resolve('css-loader'), cssLoaderOptions),
...wrapLoader(
require.resolve('postcss-loader'),
postcssLoaderOptions,
),
],
},
],
rules: [...rulesBefore, cssRule, cssModulesRule, ...rulesAfter],
},
};
};