css - 如何使用 webpack 从 MDL 获取 svg

标签 css svg npm sass webpack

使用 Material Design Lite 的复选框组件,即 https://getmdl.io/components/index.html#toggles-section/checkbox

当我使用时,tickmark.svg 显示不正确

import '../../node_modules/material-design-lite/src/material-design-lite.scss';

用于与 npm 和 webpack 捆绑。而不是:

enter image description here

我得到:

enter image description here

在浏览器中,我找到了 tickmark.svg 的以下路径,这是“缺失”的 svg:

enter image description here

当使用这两种选择中的任何一种时,它都可以正常工作: 1) 导入 '../../node_modules/material-design-lite/.tmp/material-design-lite.css'; 2) 导入 '../../node_modules/material-design-lite/material.min.css';

为了比较,当使用 2 时,我在浏览器中得到以下信息:

enter image description here

我的 webpack.config.js:

var path = require('path');
var webpack = require('webpack');
var BundleTracker = require('webpack-bundle-tracker');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var Clean = require('clean-webpack-plugin');
var bootstrapPath = path.join(__dirname, 'node_modules/bootstrap/dist/css');
var sourcePath = path.join(__dirname, 'assets');

module.exports = {
    devtool: 'eval-source-map',
    context: __dirname,
    entry: [
        './assets/js/index' // entry point of our app. .assets/js/index.js should require other js modules and dependencies it needs
    ],
    output: {
        path: path.resolve('./assets/bundles/'),
        filename: '[name]-[hash].js',
    }
    ,
    node: {
        console: true,
        fs: 'empty'
    }
    ,
    plugins: [
        new webpack.ProvidePlugin({
            $: 'jquery',
            jQuery: 'jquery'
        }),
        new BundleTracker({filename: './webpack-stats.json'}),
        new webpack.BannerPlugin('Pqbq Banner!!!! todo'),
        new HtmlWebpackPlugin({
            template: __dirname + '/assets/index.tmpl.html'
        }),
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoErrorsPlugin(),
        new Clean(['assets/bundles'])
    ],
    module: {
        preloaders: [
            {
                test: /\.js/,
                loader: 'eslint'
            }
        ],
        loaders: [
            {
                test: /\.js[x]?$/,
                loader: 'babel',
                exclude: /(node_modules|bower-components)/,
                query: {
                    presets: ['es2015', 'stage-0']
                }
            },
            {
                test: /\.json$/,
                loader: 'json-loader'
            },
            {
                test: /\.js$/,
                include: path.resolve(__dirname, 'node_modules/mapbox-gl/js/render/shaders.js'),
                loader: 'transform/cacheable?brfs'
            },
            {
                test: /\.js$/,
                include: path.resolve(__dirname, 'node_modules/webworkify/index.js'),
                loader: 'worker'
            },
            // {
            //     test: /\.css$/,
            //     loader: 'style!css?modules!postcss'
            // },
            {
                test: /\.scss$/,
                loaders: ['style', 'css?sourceMap', 'sass?sourceMap']
            },
            // {test: /\.(woff2?|svg)$/, loader: 'url?limit=10000'},
            // {test: /\.(ttf|eot)$/, loader: 'file'},
            {test: /\.css$/, loader: 'style-loader!css-loader'},
            {test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: 'file'},
            {test: /\.(woff|woff2)$/, loader: 'url?prefix=font/&limit=5000'},
            {test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=application/octet-stream'},
            //   {test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=image/svg+xml'},
            {test: /\.svg$/, loader: 'url?limit=8192!svgo'},
        ]
    }
    ,
    postcss: [
        require('autoprefixer')
    ],
    resolve: {
        modulesDirectories: ['node_modules', 'bower_components', bootstrapPath, sourcePath],
        extensions: ['', '.js', '.jsx', '.css'],
        alias: {
            webworkify: 'webworkify-webpack',
            '$': 'jquery',
            'jQuery': 'jquery'
        }
    }
    ,
    devServer: {
        contentBase: './assets',
        colors: true,
        historyApiFallback: true,
        inline: true,
        hot: true
    }
};

最佳答案

为了解决这个问题,我必须通过创建自己的 material.scss 文件来覆盖 $image_path 变量。我抓取了这个文件:

https://github.com/google/material-design-lite/blob/master/src/material-design-lite.scss

然后添加:

@import "./variables";

在顶部。

在该文件中我添加了这一行:

$image_path: '~material-design-lite/dist/images/' !default;

关于css - 如何使用 webpack 从 MDL 获取 svg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37323199/

相关文章:

html - 如何在 svg 圆的中心设置图像?

javascript - 使 SVG 变换矩阵围绕其中心旋转

typescript - 警告,缺少 FIREBASE_CONFIG 和 GCLOUD_PROJECT 环境变量。初始化 firebase-admin 将失败

html - 在 Scss 中使用 & 运算符

css - 标题选择器不响应 Rmarkdown html 报告的 css 中的字体大小规则

html - 我如何显示溢出单元格的全部内容 : hidden on mouse hover?

html - 禁用内联 css 样式

y=mx+c 格式的 SVG 行

node.js - TypeError : require(. ..) 不是一个函数 (Node.js) [server.js]

node.js - package.json 中应用程序的不同分支中的不同 Node 包版本?