如何从MDL使用webpack获取svg

时间:2016-05-19 12:26:24

标签: 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)import '../../node_modules/material-design-lite/.tmp/material-design-lite.css'; 2)import '../../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
    }
};

1 个答案:

答案 0 :(得分:0)

要解决此问题,我必须通过创建自己的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;