4
votes

I'm using webpack with my asp.net mvc core project. I'm able to bundle js files. But I can't resolve my font paths.

I have this I'm scss file:

$font-path: '../fonts' !global; 

/*fonts*/
/* roboto-300 - latin */
@font-face {
    font-family: 'Roboto';
    font-style: normal;
    font-weight: 300;
    src: url('#{$font-path}/roboto-v15-latin-300.eot'); /* IE9 Compat Modes */
    src: local('Roboto Light'), local('Roboto-Light'), url('#{$font-path}/roboto-v15-latin-300.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
    url('#{$font-path}/roboto-v15-latin-300.woff2') format('woff2'), /* Super Modern Browsers */
    url('#{$font-path}/roboto-v15-latin-300.woff') format('woff'), /* Modern Browsers */
    url('#{$font-path}/roboto-v15-latin-300.ttf') format('truetype'), /* Safari, Android, iOS */
    url('#{$font-path}/roboto-v15-latin-300.svg#Roboto') format('svg'); /* Legacy iOS */
}

File structure:

└── Project
    ├── assets
    │ └── scripts
    │   └── main.ts
    │ └── scss
    │   └── fonts
    │       └── roboto-condensed-v13-latin-700.eot
    │   └── main.scss
    │
    └── webpack.config.js

var path = require('path');
var extractTextPlugin = require("extract-text-webpack-plugin");
var cleanWebpackPlugin = require('clean-webpack-plugin');
var webpack = require('webpack');

module.exports = {
    entry: {
        'role/role': './assets/scripts/role/roleVM.ts',
        main: './assets/scripts/main.ts',
        vendor: ["bootstrap", "popper.js", "jquery"]
    },
    output: {
        filename: 'js/[name].js',
        path: path.resolve(__dirname, 'wwwroot/dist/')
    },
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                loader: 'ts-loader',
                exclude: /node_modules/,
            },
            {
                test: /\.scss$/,
                use: extractTextPlugin.extract({
                    use: [{
                        loader: "css-loader", options: {
                            sourceMap: true
                        }
                    }, {
                        loader: "resolve-url-loader"
                    },
                    {
                        loader: "sass-loader", options: {
                            sourceMap: true
                        },

                    }],
                    fallback: 'style-loader'
                }),
                exclude: /node_modules/
            },
            {
                test: /.(ttf|otf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/,
                use: [{
                    loader: 'file-loader',
                    options: {
                        name: '[name].[ext]',
                        outputPath: 'fonts/'
                    }
                }]
            }
        ]
    },
    plugins: [
        new cleanWebpackPlugin(['dist'], {
            root: path.resolve(__dirname, 'wwwroot'),
            verbose: true,
            dry: false
        }),
        new webpack.optimize.CommonsChunkPlugin({ name: 'vendor', filename: 'js/vendor.js' }),
        new extractTextPlugin("./css/main.css"),
        new webpack.ProvidePlugin({
            $: 'jquery',
            jQuery: 'jquery',
            'window.jQuery': 'jquery',
            'Popper': 'popper.js'
        })
    ],
    resolve: {
        extensions: [".tsx", ".ts", ".js", '.scss'],
        modules: [
            path.resolve('./'),
            path.resolve('./node_modules'),
        ]
    }
};

When I build webpack, I get an error:

ERROR in ./node_modules/css-loader?{"sourceMap":true}!./node_modules/resolve-url-loader!./node_modules/sass-loader/lib/loader.js?{"sourceMap":true}!./assets/scss/main.scss Module not found: Error: Can't resolve '../fonts/roboto-condensed-v13-latin-700.eot' in 'C:\project\src\project\assets\scss' @ ./node_modules/css-loader?{"sourceMap":true}!./node_modules/resolve-url-loader!./node_modules/sass-loader/lib/loader.js?{"sourceMap":true}!./assets/scss/main.scss 6:215433-215491

1
It looks like you should be using 1 dot instead of 2 for the path to fonts: $font-path: './fonts' !global; If it's in an .scss file in the scss folder, then fonts is a subfolder relative to this, not relative to its parent.GregHNZ
No, I tried that already, didn't work.capiono
can you display the error that occurs with only 1 dot.thomasmeadows

1 Answers

2
votes

Can you try adding the two options as below.

  1. Add entry.devtool option to your webpack config.
  2. ExtractTextPlugin has a publicPath option that can resolve this issue.

    module.exports = {
    entry: {
        'role/role': './assets/scripts/role/roleVM.ts',
        main: './assets/scripts/main.ts',
        vendor: ["bootstrap", "popper.js", "jquery"]
    },
    output: {
        filename: 'js/[name].js',
        path: path.resolve(__dirname, 'wwwroot/dist/')
    },
    devtool:'source-map', //..1. add devtool option to source-map
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                loader: 'ts-loader',
                exclude: /node_modules/,
            },
            {
                test: /\.scss$/,
                use: extractTextPlugin.extract({
                    publicPath:'',
                    //2. add public path to your font folder in the build directory
                    use: [{
                        loader: "css-loader", options: {
                            sourceMap: true
                        }
                    }, {
                        loader: "resolve-url-loader"
                    },
                        {
                            loader: "sass-loader", options: {
                            sourceMap: true
                        },
    
                        }],
                    fallback: 'style-loader'
                }),
                exclude: /node_modules/
            },
            {
                test: /.(ttf|otf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/,
                use: [{
                    loader: 'file-loader',
                    options: {
                        name: '[name].[ext]',
                        outputPath: 'fonts/'
                    }
                }]
            }
        ]
    },
    plugins: [
        new cleanWebpackPlugin(['dist'], {
            root: path.resolve(__dirname, 'wwwroot'),
            verbose: true,
            dry: false
        }),
        new webpack.optimize.CommonsChunkPlugin({ name: 'vendor', filename: 'js/vendor.js' }),
        new extractTextPlugin("./css/main.css"),
        new webpack.ProvidePlugin({
            $: 'jquery',
            jQuery: 'jquery',
            'window.jQuery': 'jquery',
            'Popper': 'popper.js'
        })
    ],
    resolve: {
        extensions: [".tsx", ".ts", ".js", '.scss'],
        modules: [
            path.resolve('./'),
            path.resolve('./node_modules'),
        ]
    }
    

    };