Webpack 3无法从SASS文件解析资产

时间:2018-05-18 10:59:27

标签: javascript webpack sass

从SASS文件加载时,我只面临图像和字体的问题。 其他所有内容都在加载,并在 ./ public 文件夹中填充,但图片和字体除外。

错误

Module build failed: ModuleNotFoundError: Module not found: Error: Can't resolve '../../assets/fonts/ProximaNovaA-Bold.eot' in '/Users/ajinkyaborade/projects/product/src/sass'

package.json ...

"webpack": "^3.12.0",
"webpack-dev-server": "^2.11.2"

...

webpack.config.js

const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const SRC = path.resolve(__dirname, 'src');
const extractPlugin = new ExtractTextPlugin({
    filename: './style.css'
});

module.exports = {
    cache: true,
    entry: "./index.js",
    output: {
        filename: '[name].bundle.js',
        path: path.resolve(__dirname, 'public')
    },
    context: SRC,
    devServer: {
        contentBase: path.resolve(__dirname, 'public/assets'),
        stats: 'errors-only',
        open: true,
        port: 8080,
        compress: true
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: 'index.html'
        }),
        new CleanWebpackPlugin(['public']),
        extractPlugin
    ],
    module: {
        rules: [
            {
                test: /\.(jpg|png|gif|svg|woff|woff2|ttf|eot|ico)$/,
                // include: SRC,
                exclude: /node_modules/,
                use: [
                {
                    loader: 'url-loader',
                    options: {
                        name: '[name].[ext]',
                        outputPath: './assets/',
                        limit: 8192
                    }
                }]
            },
            {
                test: /(\.css|\.scss)$/,
                // include: path.resolve(__dirname, 'src'),
                use: extractPlugin.extract({
                    use: [
                        {
                            loader: 'css-loader',
                            options: {
                                sourceMap: true,
                                importLoaders: 1
                            }
                        },
                        {
                            loader: 'postcss-loader',
                            options: {
                                sourceMap: true,
                                importLoaders: 1
                            }
                        },
                        {
                            loader: 'sass-loader',
                            options: {
                                sourceMap: true,
                                importLoaders: 1
                            }
                        }
                    ],
                    fallback: 'style-loader'
                })
            },
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['env', 'stage-0', 'react']
                    }
                }
            }]
    },
    resolve: {
        extensions: ['.js', '.scss']
    }
};

文件夹结构:

enter image description here

1 个答案:

答案 0 :(得分:1)

我认为你有一个main.scss文件,你导入所有的scss文件?然后你应该使用resolve-url-loader。 resolve-url-loader位于sass-loader和css-loader之间。请确保所有加载器 resolve-url-loader之前将propource设置为true。即使是生产!

 {
                test: /(\.css|\.scss)$/,
                // include: path.resolve(__dirname, 'src'),
                use: extractPlugin.extract({
                    use: [
                        {
                            loader: 'css-loader',
                            options: {
                                sourceMap: true,
                                importLoaders: 1
                            }
                        },
                        {
                            loader: 'postcss-loader',
                            options: {
                                sourceMap: true,
                                importLoaders: 1
                            }
                        },
                        {
                            loader: 'resolve-url-loader'
                        }
                        {
                            loader: 'sass-loader',
                            options: {
                                sourceMap: true,
                                importLoaders: 1
                            }
                        }
                    ],
                    fallback: 'style-loader'
                })