首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >webpack css-加载后台-图像错误加载

webpack css-加载后台-图像错误加载
EN

Stack Overflow用户
提问于 2016-07-17 18:20:39
回答 1查看 2.1K关注 0票数 4

我正在从事一个Angular2+ES2015+Babel+Webpack项目,当我在CSS文件中包含图像并使用css-loader时遇到了这个问题,它已经花了几个小时来寻找这个问题,但到目前为止还没有得到任何结果:

代码语言:javascript
复制
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问题和堆栈溢出问题上找到类似的情况,但并不完全相同。在所有这些文件中,都指出缺乏图像加载程序(这里的png文件),但在我的例子中,我有一个工作得很好。如果require('../image/penguin.png') 直接放在JS文件上,它将被加载并导出到输出路径!

我的过程和Webpack的配置是相当复杂的,很大,并且分成不同的文件,但是下面是有趣的部分。

代码语言:javascript
复制
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片段

代码语言:javascript
复制
[...]
.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-加载程序完成了工作,png文件就不是用定义的加载器(url!image-webpack)加载的,而是用babel-寄存器加载的?因此,我console.log来自css-加载程序的结果,并且有预期的图像require

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

所以我让babel忽略了png文件

代码语言:javascript
复制
{
  "presets": [
    "es2015",
    "angular2",
    "stage-3"
  ],
  "ignore": ["*.png"]
}

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

代码语言:javascript
复制
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文件上设置一个图像背景就像派一样简单

有什么想法吗?

EN

回答 1

Stack Overflow用户

发布于 2016-07-17 19:36:23

终于成功了。

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

加载程序使用Loader.exec来处理来自css-loader的响应,但是这段代码有一个熟悉的问题

除了停止使用加载程序之外,还可以在这个问题上找到一些解决方案。但是到目前为止,最好的方法是按照Mathias (@ooc) 在另一个问题上的提议创建一个临时加载程序。

我希望这能帮助别人不要浪费这么多时间!

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38424488

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档