如何使用 Electrode.io 拥有多个 css 文件

How to have multiple css files with Electrode.io

我想让我的应用程序使用 2 个独立的 css 文件(同时),这些文件是从不同的文件生成的(1 个来自 scss 个文件,一个来自 less 个文件);两个文件名都应该进行哈希处理。

我目前将 webpack 配置为生成 2 个 css 文件;但是,在开发模式下,Electrode 不会消耗第二个,而在 prod 模式下,Electrode 不会消耗第一个。生成的同构加载器文件列出了两者(在 "main" 块内)。

index.html 的输出似乎是在 electrode-react-webapp/lib/default-handlers.js:65-68 中生成的,这导致了单个 <link> 标记。所以我想我做错了。

我在 electrode-react-webapp: ^2.0.0webpack: ^3.0.0

const extractLess = new ExtractTextPlugin({
    allChunks: true,
    disable: isDev,
    filename: `semantic-ui${isDev ?  '' : '.[hash]'}.css`,
});

const extractSass = new ExtractTextPlugin({
    allChunks: true,
    disable: isDev,
    filename: `gemini${isBundling ? '[name]' : ''}${isDev ?  '' : '.[hash]'}.css`,
});

const lessRule = {
    test: /\.less$/,
    use: extractLess.extract({
        // …
    })
};

const sassRule = {
    test: /\.scss$/,
    use: extractSass.extract({
        // …
    })
};

// …

composer.addPartials([ // webpack-config-composer
    {
        'app-style': {
            config: {
                module: { rules: [ sassRule ] },
            },
        },
    },
    {
        'semantic-style': {
            config: {
                module: { rules: [ lessRule ] },
            },
        },
    },
]);

// register custom styles
const { _base } = composer.profiles;
const _extractStyle = _base.partials['_extract-style'];

_base.partials['app-style'] = { order: _extractStyle.order };
_base.partials['semantic-style'] = { order: _extractStyle.order };

delete _base.partials['_extract-style']; // `.enable = false` doesn't work

const config = compose();

在 v2.2.0 中发布的 Electrode React Webapp #715 之前,这是不可能的。

PR 中提供的示例代码的几个小改进:

不要在 webpack 配置中添加 entry 中的第二项(而是在您的应用中使用 import

// /config/utils/chunk-selector.js
module.exports = () => {
    const css = process.env.NODE_ENV === 'production'
        ? [
            'app',
            'third-party-something',
        ]
        : [];

    return: {
        css,
        js: 'app',
    };
};

这允许第二个包在开发模式下使用 style-loader 回退。