javascript - 模块解析失败 : Unexpected character for woff woff2 and ttf file webpack

标签 javascript webpack-dev-server webpack-2

我有 webpack.config.js 文件并为单个模块 block 中的每种字体类型添加了加载器,但是当我运行 yarn start

webback 在终端中成功地遵守了这些细节

  f4769f9bdb7466be65088239c12046d1.eot    20.1 kB          [emitted]         
448c34a56d699c29117adc64c43affeb.woff2      18 kB          [emitted]         
 fa2772327f55d8198301fdb8bcfc8158.woff    23.4 kB          [emitted]         
  e18bbf611f2a2e43afc071aa2f4e1512.ttf    45.4 kB          [emitted]         
  89889688147bd7575d6327160d64e760.svg     109 kB          [emitted]         
                             bundle.js    1.56 MB       0  [emitted]  [big]  main
                           favicon.ico    1.15 kB          [emitted]         
                            index.html  605 bytes          [emitted]  

并且页面在浏览器中打开并应用了 bootstrap css 但在控制台中,它为 woff woff2ttf 提供了多个错误文件(见图片)

Module parse failed: Unexpected character '' (1:4)
You may need an appropriate loader to handle this file type.
(Source code omitted for this binary file)

console error

package.json

"dependencies": {
    "bootstrap": "^3.3.7",
    "css-loader": "^0.28.7",
    "extract-text-webpack-plugin": "^3.0.2",
    "file-loader": "^1.1.5",
    "history": "^4.7.2",
    "html-webpack-plugin": "^2.30.1",
    "less": "^2.7.3",
    "less-loader": "^4.0.5",
    "path": "^0.12.7",
    "postcss-loader": "^2.0.8",
    "react": "^16.0.0",
    "react-dom": "^16.0.0",
    "react-redux": "^5.0.6",
    "react-redux-form": "^1.16.0",
    "react-router-dom": "^4.2.2",
    "redux": "^3.7.2",
    "redux-logger": "^3.0.6",
    "redux-thunk": "^2.2.0",
    "style-loader": "^0.19.0",
    "svg-inline-loader": "^0.8.0",
    "uglifyjs-webpack-plugin": "^1.0.1",
    "url-loader": "^0.6.2",
    "webpack-combine-loaders": "^2.0.3"
  },
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-eslint": "^8.0.2",
    "babel-loader": "^7.1.2",
    "babel-plugin-transform-es2015-destructuring": "^6.23.0",
    "babel-plugin-transform-object-rest-spread": "^6.26.0",
    "babel-preset-env": "^1.6.1",
    "babel-preset-react": "^6.24.1",
    "eslint": "^4.10.0",
    "eslint-config-airbnb": "^16.1.0",
    "eslint-plugin-flowtype": "^2.39.1",
    "eslint-plugin-html": "^3.2.2",
    "eslint-plugin-import": "^2.8.0",
    "eslint-plugin-jsx-a11y": "^6.0.2",
    "eslint-plugin-react": "^7.4.0",
    "webpack": "^3.8.1",
    "webpack-dev-server": "^2.9.4"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "webpack-dev-server"
  }

webpack.config.js

const webpack = require('webpack');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin'); // eslint-disable-line
const path = require('path');
const combineLoaders = require('webpack-combine-loaders');

const BUILD_DIR = path.resolve(__dirname, 'build');
const APP_DIR = path.resolve(__dirname, 'src');

const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
    template: './src/index.html',
    filename: 'index.html',
    favicon: './src/assets/favicon.ico',
    inject: 'body'
});
// const extractPluginConfig = new ExtractTextPlugin({filename:'style.css', disable: false, allChunks: true});
module.exports = {
    context: __dirname,
    entry: [
        'webpack-dev-server/client?http://localhost:8080',
        'webpack/hot/only-dev-server',
        APP_DIR + '/index.jsx',
    ],
    output: {
        publicPath: '/',
        path: BUILD_DIR,
        filename: 'bundle.js'
    },
    module: {
        loaders: [{
                test: /\.jsx?/,
                loader: 'babel-loader',
                include: path.join(__dirname, 'src'),
                exclude: /(node_modules|bower_components)/,
                query: { presets: ["env", "react"] }
            },
            {
                test: /\.css$/,
                // exclude: /node_modules/,
                loader: 'style-loader!css-loader?importLoaders=1'
            },
            {
                test: /\.scss$/,
                loader: ExtractTextPlugin.extract('style', 'css?sourceMap!sass?sourceMap')
            },
            {
                test: /\.less$/,
                loader: 'style-loader!css-loader!postcss-loader!less-loader'
            },
            {
                test: /\.json$/,
                loader: 'json'
            },
            {
                test: /\.png$/,
                loader: "url-loader",
                query: {
                    limit: 100000
                }
            },
            {
                test: /\.jpg$/,
                loader: "url-loader"
            },
            {
                test: /\.svg(\?.*)?$/,
                loader: "url-loader",
                query: {
                    limit: 10000,
                    mimetype: 'image/svg+xml'
                }
            },
            {
                test: /\.(woff2?)(\?.*)?$/,
                loader: "url-loader",
                query: {
                    limit: 10000,
                    mimetype: 'application/font-woff'
                }
            },
            {
                test: /\.(ttf|otf)(\?.*)?$/,
                loader: 'url-loader',
                query: {
                    limit: 10000,
                    mimetype: 'application/octet-stream'
                }
            },
            {
                test: /\.eot(\?.*)?$/,
                loader: 'file-loader'
            }
        ]
    },
    resolve: {
        extensions: ['.js', '.jsx', '.css', '.less', '.json']
    },
    plugins: [
        new webpack.HotModuleReplacementPlugin(),
        new webpack.DefinePlugin({
            'process.env': {
                'NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development')
            }
        }),
        // new UglifyJsPlugin(), for production server only
        HtmlWebpackPluginConfig
    ],
    devServer: {
        historyApiFallback: true,
        hot: true
    }
}

index.jsx

 import 'bootstrap/dist/css/bootstrap.css'

我尝试了各种组合来代替来自 github solution 的加载程序的上述模块但它们都不起作用,请参阅试验

试用 1(使用文件加载器)

    { 
      test: /\.(jpg|png|woff|woff2|eot|ttf|svg)$/, 
      loader: 'file-loader?name=[path][name].[ext]?[hash]' 
    }

试验 2(使用 url loader)

    { 
       test: /\.(woff(2)?|eot|ttf|otf)(\?[a-z0-9]+)?$/, 
       loader: 'url-loader?limit=100000' 
    }

试验三(使用带下限的url loader)

        {
            test: /\.(woff2?|ttf|eot|svg|png|jpe?g|gif)(\?v=\d+\.\d+\.\d+)?$/,
            loader: 'url-loader?limit=8192'
        },

最佳答案

在 webpack 4 中,你需要:

{
 test: /\.(woff|woff2|ttf|eot)$/,
 use: 'file-loader?name=fonts/[name].[ext]!static'
}

例如使用文件加载器。

关于javascript - 模块解析失败 : Unexpected character for woff woff2 and ttf file webpack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47216927/

相关文章:

jquery-plugins - Webpack 需要一个 jQuery 插件

javascript - jQuery 动画数字计数器从零到值 - 当值达到数十万时计数器显示错误值

javascript - 如何在 React 中传递事件处理函数?

javascript - Firebase 实时数据库快照不完整/缺少多个层

fonts - 导出 react-bootstrap 时如何使用 Glyphicons

node.js - React 没有定义 ReferenceError?我正在使用 webpack 和 webpack-dev-server

webpack - React-share util.inherits 不是一个函数 webpack 2

webpack - 如何编写基于其他模块动态添加模块到包中的 Webpack 插件?

javascript - Webpack 2 使用 System.import 进行代码分割 : Dependencies of dependencies

javascript - 检测 Firefox 扩展的安装