0

extract-text-webpack-plugin到之后,mini-css-extract-plugin我在 SCSS / CSS 文件中遇到了一些问题,这些问题不再解决。这些 URL 是svg-sprites使用svg-sprite-loader. 在一切正常之前。

Webpack 只是添加了原始 svg 文件所在的完整路径(“/User/...”)。我希望我从 sprite 中获取 URL,并在末尾带有正确的哈希值,例如my-svg-sprite.svg#someid.

const path = require('path');
const webpack = require('webpack');
const SpritePlugin = require('svg-sprite-loader/plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const { WebpackManifestPlugin } = require('webpack-manifest-plugin');
const StylelintPlugin = require('stylelint-webpack-plugin');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const TerserPlugin = require('terser-webpack-plugin');

module.exports = function({manifestConfig, target, commonPlugins, mode}) {

    const plugins = [
        ...
        new SpritePlugin(),
        new MiniCssExtractPlugin({filename: `[name]${manifestConfig ? '-[contenthash]' : ''}.css`}),
        new StylelintPlugin({   "files": ["**/*.scss","!/shops/"],
            "context": path.resolve(__dirname, '../scss/'),
            "fix": true,
            "configFile": path.resolve(__dirname, '../scss/_stylelintrc')
        }),
        ...commonPlugins
    ];

    if (manifestConfig) {
        plugins.push(
                new WebpackManifestPlugin({
                    fileName: manifestConfig.output,
                    seed: manifestConfig.assets,
                    basePath: '/somepage/',
                    publicPath: '/somepage/',
                    map: (file) => {
                        file.name = file.name.replace(/(-[a-f0-9]{32})(\..*)$/, '$2');
                        return file;
                    },
                })
        )
    }

    return {
        stats: 'minimal',
        devtool: 'source-map',
        entry: {
            myapp: [
                "babel-polyfill",
                path.resolve(__dirname, '../js/es6/myapp.es6.js')
            ]
        },
        output: {
            filename: `[name]${manifestConfig ? '-[chunkhash]' : ''}.js`, // use the properties of entry as name
            path: path.join(target, 'somepage'),
        },
        optimization: {
            minimizer: [
                new TerserPlugin({
                    parallel: true,
                    sourceMap: true,
                }),
                new CssMinimizerPlugin(),
            ]
        },
        module: {
            rules: [
                ...
                {
                    test: /\.svg$/,
                    include: [
                        path.resolve(__dirname, "../img/flags"),
                        path.resolve(__dirname, "../img/myapp-icons")
                    ],
                    use: [
                        { loader: 'svg-sprite-loader',
                            options: {
                                extract: true,
                                spriteFilename: pathname => pathname.includes('myapp-icons') ?
                                        `images/other-sprite${manifestConfig ? '.[hash]' : ''}.svg` :
                                        `images/sprite${manifestConfig ? '.[hash]' : ''}.svg` },
                        }
                    ]
                },
                {
                    test: /\.(png|jp(e*)g|svg|gif)$/,
                    exclude: [
                        path.resolve(__dirname, "../img/flags"),
                        path.resolve(__dirname, "../img/myapp-icons")
                    ],
                    use: [{
                        loader: 'url-loader',
                        options: {
                            limit: 300,
                            name: `images/[name]${manifestConfig ? '-[hash]' : ''}[ext]`
                        }
                    }]
                },
                ...
                {
                    test: /\.scss$/,
                    use: [
                        MiniCssExtractPlugin.loader,
                        'css-loader',
                        'postcss-loader',
                        {
                            loader: 'resolve-url-loader',
                            options: {
                                debug: true
                            }
                        },
                        {
                            loader: 'sass-loader',
                            options: {
                                sourceMap: true,
                                sassOptions: {
                                    sourceMapContents: true
                                }
                            }
                        }
                    ],
                }
            ]
        },

        ...
    };
};

4

0 回答 0