import { StylableWebpackPlugin, applyWebpackConfigStylableExcludes, } from '@stylable/webpack-plugin';
import { StylableOptimizer } from '@stylable/optimizer';
const stylableOptimizer = new StylableOptimizer();
const packages = [
    '@wix/design-system',
    '@wix/patterns',
    '@wix/wix-style-react-incubator',
];
export const decorateNextJsWebpackConfig = (config, { isServer }) => {
    // causes provided packages to be bundled (not external)
    if (isServer && Array.isArray(config.externals)) {
        const nextExternals = config.externals[0];
        config.externals = [
            async (ctx) => {
                for (const pack of packages) {
                    if (ctx.request?.startsWith(pack)) {
                        return false;
                    }
                }
                return nextExternals(ctx);
            },
        ];
    }
    // excludes other configs from attempting to handle stylable files
    applyWebpackConfigStylableExcludes(config);
    config.plugins = config.plugins ?? [];
    config.plugins.push(new StylableWebpackPlugin({
        assetFilter: () => false,
        filename: 'static/css/stylable.[contenthash].css',
        optimizer: stylableOptimizer,
    }));
    return config;
};
//# sourceMappingURL=styleableDecorator.js.map