javascript - 意外字符 '@' 您可能需要适当的加载程序来处理此文件类型

标签 javascript webpack webpack-2 es6-module-loader css-modules

这是我尝试运行 webpack 时得到的结果:我得到的错误是:

"ERROR in ./v3/app/styles/main.scss Module parse failed: /Users/vovina/widget-login-react/v3/app/styles/main.scss Unexpected character '@' (1:0) You may need an appropriate loader to handle this file type."

它无法解析@import,有什么想法吗?

我的 webpack 配置如下:

    const childProcess = require('child_process')
    const CopyWebpackPlugin = require('copy-webpack-plugin')
    const ExtractTextPlugin = require('extract-text-webpack-plugin')
    const trimEnd = require('lodash/trimEnd')
    const webpack = require('webpack')
    const path = require('path')

    const ENV = {
      NODE_ENV: process.env.NODE_ENV,
      API: 'https://accounts' + (process.env.NODE_ENV === 'prd' ? '' : '-' 
    + process.env.NODE_ENV),
      BUILD_VERSION: trimEnd(childProcess.execSync('git rev-list HEAD --
    count').toString(), '\n'),
      BUILD_DATE: trimEnd(childProcess.execSync('git log --format="%cd" -n 
    1').toString(), '\n'),
      BUILD_COMMIT_ID: trimEnd(childProcess.execSync('git log --format="%h" 
    -n 1').toString(), '\n')
    }
    const prodLikeEnvironment = process.env.NODE_ENV === 'stg' || 
    process.env.NODE_ENV === 'prd'
    const CSS_MAPS = !prodLikeEnvironment
    module.exports = {

      entry: {
        init: [
          './app/init.js'
        ],
        login: [
          './app/login.js'
        ],
        authentication: [
          './v3/app/authenticator.js'
        ],
        common: [
          './app/common.js'
        ]
      },
      target: 'web',
      output: {
        path: path.join(__dirname, 'dist', process.env.NODE_ENV),
        pathinfo: true,
        publicPath: '/assets/widgets/login/v2/',
        filename: '[name].bundle.js',
        chunkFilename: '[id].bundle.js',
        libraryTarget: 'umd'
      },
      resolve: {
        alias: {
          'react': 'preact-compat',
          'react-dom': 'preact-compat'
        },
        modules: [
          path.resolve('./app'),
          path.resolve('./node_modules')
        ]
      },
      module: {
        loaders: [
          {
            test: /\.jsx?$/,
            loaders: ['babel-loader'],
            exclude: [/bower_components/, /node_modules/]
          },
          {
    // Transform our own .(scss|css) files with PostCSS and CSS-modules
            test: /\.(scss|css)$/,
            include: [path.resolve(__dirname, 'v3/app')],
            options: {
              sourceMap: true
            },
            loader: [
              `style-loader?singleton`,
              `css-loader?modules&importLoaders=1&localIdentName=
    [local]${process.env.CSS_MODULES_IDENT || 
    '_[hash:base64:5]'}&sourceMap=${CSS_MAPS}`,
              'postcss-loader',
              `sass-loader?sourceMap=${CSS_MAPS}`
            ].join('!')
          },
          {
            test: /\.(scss|css)$/,
            exclude: [path.resolve(__dirname, 'v3/app')],
            options: {
            sourceMap: true
            },
            loader: [
              `style-loader?singleton`,
              `css-loader?sourceMap=${CSS_MAPS}`,
              `postcss-loader`,
              `sass-loader?sourceMap=${CSS_MAPS}`
            ].join('!')
          },
          {
            test: /\.(svg|eot|woff|woff2?|ttf|otf)$/,
            use: 'base64-font-loader'
          },
          {
            test: /.json$/,
            loader: 'json'
          },
          {
            test: /\.jpe?g$|\.gif$|\.png$/,
            use: 'base64-image-loader'
          },
          {
            test: /\.html?$/,
            loader: 'html'
          },
          {
            test: /\.js$/,
            loader: 'strip-loader?strip[]=debug,strip[]=console.log,strip[]=console.debug,strip[]=console.info'
          }
        ]
      },
      plugins: [
        new webpack.LoaderOptionsPlugin({
          minimize: true
          // postcss: [
          //   autoprefixer({ browsers: 'last 2 versions' })
          // ]
        }),
        new CopyWebpackPlugin([
    { from: 'public' }
        ]),
        new ExtractTextPlugin('[name].bundle.css'),
        new webpack.DefinePlugin({
          ENV: JSON.stringify(ENV),
    // Only used for react prod bundle. Refer to ENV.NODE_ENV for business 
    logic
          'process.env': {
            'NODE_ENV': JSON.stringify('production')
          }
        }),
        new webpack.NoEmitOnErrorsPlugin(),
        new webpack.optimize.UglifyJsPlugin({
          output: {
            comments: false
          },
          compress: {
            unsafe_comps: true,
            properties: true,
            keep_fargs: false,
            pure_getters: true,
            collapse_vars: true,
            unsafe: true,
            warnings: false,
            screw_ie8: true,
            sequences: true,
            dead_code: true,
            drop_debugger: true,
            comparisons: true,
            conditionals: true,
            evaluate: true,
            booleans: true,
            loops: true,
            unused: true,
            hoist_funs: true,
            if_return: true,
            join_vars: true,
            cascade: true,
            drop_console: true
          }
        })
      ]
    }

最佳答案

您在配置文件中使用了 (scss|css) 两次。 删除它并使用发布的代码: 使用前,必须先npm install raw-loader。 我认为您已经安装了 sass-loader

{
  test: /\.css$/,
  include: helpers.root('src', 'app'),
  loader: 'raw-loader'
},
// // css global which not include in components
{
  test: /\.css$/,
  exclude: helpers.root('src', 'app'),
  use: ExtractTextPlugin.extract({
    use: 'raw-loader'
  })
},

{
  test: /\.scss$/,
  include: helpers.root('src', 'app'),
  use: ['raw-loader', 'sass-loader']
},
// // SASS global which not include in components
{
  test: /\.scss$/,
  exclude: helpers.root('src', 'app'),
  use: ExtractTextPlugin.extract({
    use: ['raw-loader', 'sass-loader']
  })
}

添加我的root()函数。

var path = require('path'); 
var _root = path.resolve(__dirname, '..'); 
function root(args) {   
    args = Array.prototype.slice.call(arguments, 0);   
    return path.join.apply(path, [_root].concat(args)); 
} 
exports.root = root;

希望这会奏效。

关于javascript - 意外字符 '@' 您可能需要适当的加载程序来处理此文件类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47300984/

相关文章:

linux - Angular-CLI 在构建时卡住

javascript - 是否有一种纯粹的基于 Promise 的映射/连接集合的方法?

javascript - 不同 .js 文档中的 ANGULAR 1.5 Controller

javascript - Vue2 : view-router exception when trying to use <router-view>

javascript - webpack 可以在没有 Nodejs 的情况下运行吗?

angular - 无法找到模块 './sections/lazy/lazy.module.ngfactory' Angular 2 Webpack + AOT 设置中出现错误

angularjs - angular2 中的 package.json 是否需要以任何方式启动应用程序?

javascript - 调整浏览器大小时进行图像缩放

javascript - 开 Jest 配置错误 : "Preset @shelf/jest-mongodb not found."

javascript - Webpack:未捕获的 ReferenceError:未定义 vendor_bd98b4ed288c6b156cc9