angular - webpack css-loader 背景图片加载错误

标签 angular webpack babeljs css-loader babel-register

我正在做一个 Angular2+ES2015+Babel+Webpack 项目,当我在 CSS 文件中包含图像并使用 css-loader 时遇到了这个问题,已经花了几个小时来寻找这个问题但是到目前为止没有运气:

ERROR in ./src/app/home/home.component.css
Module build failed: SyntaxError: /path/to/project/src/app/home/images/penguin.png: Unexpected character '�' (1:0)
> 1 | �PNG
    | ^
  2 | 
  3 | 
  4 | IHDR��>a�gAMA��7��!�IDATx��]xTEמ�B�"�)"��� ��T?@?�iRiD�tPA��)"���$��K���K���wτ���f��M6a���OHؽ3s�3gΜ93#���>���x��}����>���x�Ƅ&���f&�o>
                                                ��ɉI�O����#S�a-a
                                                                  �9�8!�W1<�)O�G�j��B�
    at Parser.pp.raise (/path/to/project/node_modules/babylon/lib/parser/location.js:22:13)
    at Parser.getTokenFromCode (/path/to/project/node_modules/babylon/lib/tokenizer/index.js:561:12)
    at Parser.readToken (/path/to/project/node_modules/babylon/lib/tokenizer/index.js:180:21)
    at Parser.readToken (/path/to/project/node_modules/babylon/lib/plugins/flow.js:164:22)
    at Parser.nextToken (/path/to/project/node_modules/babylon/lib/tokenizer/index.js:169:21)
    at Parser.parse (/path/to/project/node_modules/babylon/lib/parser/index.js:128:12)
    at parse (/path/to/project/node_modules/babylon/lib/index.js:47:47)
    at File.parse (/path/to/project/node_modules/babel-core/lib/transformation/file/index.js:517:34)
    at File.parseCode (/path/to/project/node_modules/babel-core/lib/transformation/file/index.js:603:20)
    at /path/to/project/node_modules/babel-core/lib/transformation/pipeline.js:49:12
    at File.wrap (/path/to/project/node_modules/babel-core/lib/transformation/file/index.js:563:16)
    at Pipeline.transform (/path/to/project/node_modules/babel-core/lib/transformation/pipeline.js:47:17)
    at Object.transformFileSync (/path/to/project/node_modules/babel-core/lib/api/node.js:144:10)
    at compile (/path/to/project/node_modules/babel-register/lib/node.js:129:20)
    at loader (/path/to/project/node_modules/babel-register/lib/node.js:158:14)
    at Object.require.extensions.(anonymous function) [as .js] (/path/to/project/node_modules/babel-register/lib/node.js:168:7)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (/path/to/project/src/app/home/home.component.css:6:2430)
    at Module._compile (module.js:409:26)
    at Object.loaderContext.exec (/path/to/project/node_modules/webpack-core/lib/NormalModuleMixin.js:88:7)
    at Object.module.exports (/path/to/project/node_modules/to-string-loader/src/to-string.js:6:54)

如果您搜索错误,您会在 github 问题和 stackoverflow 上找到类似的情况,但不完全相同。在所有这些文件中都指出缺少图像加载器(此处为 png 文件),但就我而言,我有一个完美运行的图像加载器。 如果 require('../image/penguin.png') 直接在 JS 文件上,它只会被加载并导出到输出路径!

我的流程和 Webpack 配置非常复杂,很大并且分成不同的文件,但这是有趣的部分

import config from './config';

{
  [...]
  module: {
    preLoaders: [
      {
        test: /\.js$/,
        exclude: [/node_modules/],
        loader: 'eslint'
      },
      {
        test: /\.css$/,
        exclude: [/node_modules/],
        loader: 'postcss?pack=lint'
      }
    ],
    loaders: [
      {
        test: /\.js$/,
        exclude: [/node_modules/, /\.(spec|e2e)\.js$/],
        loaders: [
          'babel',
          'angular2-template'
        ]
      },
      {
        test: /\.css$/,
        exclude: [/node_modules/],
        loaders: [
          'to-string',
          'css?-import',
          'postcss?pack=process'
        ]
      },
      {
        test: /\.(png|jpe?g|gif)$/,
        loaders: [
          'url?limit=10000&name=assets/images/[name].[ext]?[hash:20]',
          'image-webpack?bypassOnDebug&optimizationLevel=6'
        ]
      },
      [...]
    ]
  },
  output: {
    path: resolve(config.paths.dist),
    publicPath: `http://${config.dev.host}:${config.dev.port}/`,
    filename: '[name].js',
    sourceMapFilename: '[name].map',
    chunkFilename: '[id].chunk.js'
  },
  [...]
}

这里是触发错误的 css 片段

[...]
.post-image {
  height: 150px;
  background-image: url(./images/penguin.png);
  background-position: center center;
  background-repeat: no-repeat;
}

如果对 css 使用 to-string-loader 看起来很奇怪,那是因为 angular2-template-loader 允许将 CSS 直接内联到 Angular2 Componet 的 stylesUrl

由于错误堆栈,似乎一旦 css-loader 完成工作,png 文件就不会使用定义的加载程序(url!image-webpack)加载,而是使用 babel-register 加载?所以我 console.log 来自 css-loader 的结果,并且有预期的图像 require:

[...]
exports.push([module.id, ".post-image{height:150px;background-image:url(" + require("./images/penguin.png") + ");background-position:50%;background-repeat:no-repeat}", ""]);

所以我让babel忽略了png文件

{
  "presets": [
    "es2015",
    "angular2",
    "stage-3"
  ],
  "ignore": ["*.png"]
}

这会引发一个稍微不同的错误,因为 babel 忽略了图像,但最终似乎是一样的:

ERROR in ./src/app/home/home.component.css
Module build failed: SyntaxError: Unexpected token ILLEGAL
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:373:25)
    at loader (/path/to/project/node_modules/babel-register/lib/node.js:158:5)
    at Object.require.extensions.(anonymous function) [as .js] (/path/to/project/node_modules/babel-register/lib/node.js:168:7)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (/path/to/project/src/app/home/home.component.css:6:2430)
    at Module._compile (module.js:409:26)
    at Object.loaderContext.exec (/path/to/project/node_modules/webpack-core/lib/NormalModuleMixin.js:88:7)
    at Object.module.exports (/path/to/project/node_modules/to-string-loader/src/to-string.js:6:54)

我一头雾水,我要 WTF 只是在 CSS 文件上设置图像背景应该很容易

有什么想法吗??

最佳答案

终于成功了。

问题的根源在链的末端:to-string-loader

加载器使用 Loader.exec 来处理来自 css-loader 的响应,但是这段代码知道 issue

除了停止使用加载程序之外,还可以在该问题中找到一些解决方案。但到目前为止,最好的方法是创建一个由 Mathias Raacke (@ooc) 建议的临时加载程序 in this other issue

我希望这可以帮助其他人不要浪费这么多时间!

关于angular - webpack css-loader 背景图片加载错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38424488/

相关文章:

node.js - 如何使 SQLite 与 Angular 4+、Electron 和 NodeJS 一起工作

angular - 如何获得 Angular 版本?

javascript - 带 webpack 的 ACE 编辑器

javascript - Next.js:你可能需要一个合适的加载器来处理这个文件类型,目前没有配置加载器来处理这个文件

javascript - 如果同名的 'let' 被使用两次(在 for 循环中),Babel 会重新声明函数参数

javascript - <sup> 标签不适用于 Android 设备上的电子邮件

angular - 在 Angular 6 中,在脚本标签中使用 defer

vue.js - 如何在构建时替换图像和文件而不在应用程序中引用它们?

javascript - 在 nuxt 中使用 babel-polyfill (而不是 polyfill.io)

javascript - webpack 4 react 意外 token ...(扩展运算符)