繁体   English   中英

模块解析失败:woff woff2 和 ttf 文件 webpack 的意外字符

[英]Module parse failed: Unexpected character for woff woff2 and ttf file webpack

我有webpack.config.js文件并为单个模块块中的每种字体类型添加了加载器但是当我运行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]  

并在浏览器中打开页面并在其上应用了引导程序 css 但在控制台中,它为woff woff2ttf文件提供了多个错误(见图)

模块解析失败:意外字符 '' (1:4)
您可能需要一个合适的加载器来处理这种文件类型。
(此二进制文件省略了源代码)

控制台错误

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
    }
}

索引.jsx

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

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

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

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

试验 2(使用 url 装载机)

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

试验 3(使用具有下限的 url 装载机)

        {
            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'
}

例如使用文件加载器。

有我关于字体的配置

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

也许有用

--- 添加 ---

resolve: {
    extensions: ['.js', '.jsx', '.css', '.less', '.json'],
    modules: ['node_modules', 'path/to/your/static_resource']
}

我在我正在工作的项目中安装了一个 vue 插件,但我收到了这个错误。

我试图按照说明进行操作,但我不知道它们已经过时了。 我正在分享我的解决方案。 在 webpack.config.js 中,我添加了以下设置:

module: {
  rules: [
... --> other existing rules 
  {
    test: /\.(woff|woff2|ttf|eot)$/,
    loader: 'file-loader',
    options: {
      name: 'fonts/[name].[ext]!static'
    },
    include: /node_modules/
  }
 ]
}

我正在做 React/Redux,也遇到了这个错误。 我在 webpack.config.dev.js 中添加了这一行来解决这个问题:

module: {
  loaders: [
    ...,
    {test: /\.png$/, loader: 'file'} // Line added
  ]
}

然后在我的 R/R 代码中,我使用 import (ES6) 来要求图像。 然后我这样做:

<img src={myPNGImage} .../>

我正在使用 vue cli webpack 和下面的代码修复了错误

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

在 Magento2 的 PWA 工作室中,

您可以在 webPackConfig.js 中添加一行,如下所示

config.module.rules.push({ test: /\.(woff|woff2|ttf|eot)$/, use: 'file-loader' });

这应该可以完成这项工作。

接受的答案对我有用,但是由于 webpack 4 答案需要稍微更新以使用file-loader而不仅仅是file ,例如:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM