【问题标题】:css-loader : Unexpected token of css coming from node modulecss-loader :来自节点模块的意外的 css 令牌
【发布时间】:2017-10-06 15:14:21
【问题描述】:

我将用 Github 问题风格解释我的问题:

目前的行为是什么?

无法加载 css。 完整的错误信息:

如果当前行为是错误,请提供重现步骤。
我的 package.json :https://gist.github.com/jy95/99377c113252e6baaa23087abe859814
我的 webpack.config.js :https://gist.github.com/jy95/16b0f03d46b087c6f2640c8e9178914f

请注明浏览器版本、Node.js版本、webpack版本、操作系统等其他相关信息。
节点:6.10.3
操作系统:Windows 10

PS:在您告诉我查看 ExtractTextPlugin 之前,我想解释一下为什么会出现此错误(我还尝试在我的条目中要求 css:app.js,结果相同)

【问题讨论】:

    标签: css node.js webpack css-loader


    【解决方案1】:

    您已在 webpack.config.js 中为 css-loader 排除了 node_modules。可能您必须包含需要处理的来自node_modules 的路径。像这样的:

    {
      test: /\.css$/,
      use: 'css-loader',
      exclude: /node_modules/,
      include: [
        'node_modules/admin-lte/dist/css/skins/skin-blue.min.css',
        <place here others you need>
      ],
    }
    

    可能会出现有关根文件夹的一些问题,请自行检查系统。

    【讨论】:

      【解决方案2】:

      在你的 webpack.config.js 中试试这个

      const postcssPlugins = [
        require('postcss-cssnext')(),
        require('postcss-modules-values')
      ];
      
      const scssLoader = [
        { loader: 'style-loader' },
        { loader: 'css-loader' },
        { loader: 'sass-loader' }
      ];
      
      const postcssLoader = [
        { loader: 'style-loader' },
        { loader: 'css-loader', options: { modules: true } },
        { loader: 'postcss-loader', options: { plugins: () => [...postcssPlugins] } }
      ];
      
       module: {
            loaders: [
               {
                  test: /\.jsx?$/,
                  exclude: /node_modules/,
                  loader: 'babel-loader',
      
                  query: {
                     presets: ['es2015', 'react']
                  }
               },
               {
                   test: /\.(scss|sass)$/,
                   loader: scssLoader,
                   include: [__dirname]
                 },
                 { test: /\.css$/,
                   loader: postcssLoader,
                   include: [__dirname]
                 }
            ]
      
         }
      

      【讨论】:

      • 我已经更新了代码,我不知道为什么会发生这个错误,但可能是它没有得到适当的加载器来编译依赖项
      • 这是一个 css 文件,所以我不明白为什么会这样。这不是我试图将 .less 传递给 css-loader
      • 我认为依赖项本身需要 sass 或 scss 或更少的加载器,因为单独的 css 加载器不适用于这种情况
      • 谢谢 .. 我回家后会试试这个,但你的回答是有道理的,而且似乎是正确的。
      • mmm ... 字体加载出现问题(font-awesone 等)。有什么想法吗?
      【解决方案3】:

      我终于找到了解决办法,希望对你有帮助:

      罪魁祸首是 v2.0.1 中的 postcss-loader,将其更新为 2.0.5 测试 (https://github.com/webpack-contrib/extract-text-webpack-plugin/issues/498)

      const webpack = require('webpack');
      const path = require('path');
      const ExtractTextPlugin = require('extract-text-webpack-plugin');
      
      const postcssPlugins = [
        require('postcss-cssnext')(),
        require('postcss-modules-values')
      ];
      
      const scssLoader = [
        { loader: 'style-loader' },
        { loader: 'css-loader' },
        { loader: 'sass-loader' }
      ];
      
      const postcssLoader = [
        { loader: 'style-loader' },
        { loader: 'css-loader', options: { modules: true } },
        { loader: 'postcss-loader', options: { plugins: [...postcssPlugins] } }
      ];
      
      // you'll need to npm install the following: [raw-loader, html-minifier-loader, json-loader, css-loader,style-loader, url-loader, file-loader ]
      // in your index.html you should have two script tags, one for app.js(or bundle.js) and vendor.js
      // no need for babelify with webpack. just, npm install --save-dev babel-cli babel-preset-es2016
      // in .babelrc file change the preset of 2015 to ["es2016"]
      module.exports = {
        entry: {
          app: './app.js',
          // if any on these are just for css remove them from here and require(with absolute path) them from app.js
          vendor: [
            'babel-polyfill',
            'admin-lte',
            'admin-lte/bootstrap/js/bootstrap.min.js',
            'lobibox/dist/js/notifications.min.js',
            'admin-lte/plugins/fastclick/fastclick.js',
            'moment',
            'moment/locale/fr.js',
            'moment-timezone',
            'eonasdan-bootstrap-datetimepicker',
            'bootstrap-table',
            'bootstrap-table/dist/locale/bootstrap-table-fr-BE.min.js',
            'parsleyjs',
            'parsleyjs/dist/i18n/fr.js',
            'bootstrap-daterangepicker',
            'socket.io-client',
            'jquery-confirm',
            'push.js',
            'lodash',
            'bootstrap-table/dist/extensions/export/bootstrap-table-export.min.js',
            'tableexport.jquery.plugin'
          ]
        },
        //devtool: 'eval', // for test in the browser
        output: {
          filename: '[name].js',
          path: path.resolve(__dirname, 'dist')//,
          //pathinfo: true
        },
        module: {
          rules: [{
            test: /\.js$/,
            use: 'babel-loader',
            exclude: /node_modules/
          }, {
            test: /\.html$/,
            use: ['raw-loader', 'html-minifier-loader'],
            exclude: /node_modules/
          }, {
            test: /\.json$/,
            use: 'json-loader',
            exclude: /node_modules/
          }, {
            test: /\.(scss|sass)$/,
            use: ExtractTextPlugin.extract({
                  fallback: scssLoader[0], // style-loader
                  use: scssLoader.slice(1) // [ 'css-loader', 'sass-loader' ]
              })
          },{ 
            test: /\.css$/,
            use: ExtractTextPlugin.extract({
                fallback: "style-loader",
                use: "css-loader"
            })
          }, {
            test: /\.(jpg|png|gif)$/,
            use: 'file-loader'
          }, {
            test: /\.(ttf|otf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/,
            loader: 'file-loader?name=fonts/[name].[ext]'
          }],
        },
        plugins: [
          new ExtractTextPlugin({
              filename: 'app.bundle.css',
              allChunks: true
          }),
          new webpack.ProvidePlugin({
                  $: "jquery",
                  jQuery: "jquery"
          })
        ],
      };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-16
        • 2018-05-31
        • 2019-12-19
        相关资源
        最近更新 更多