2

我正在使用 React clicreate-react-app并尝试从 UI 库http://react-conventions.herokuapp.com/加载 .scss 文件。我已经运行npm run eject并将以下内容添加到loadersconfig.dev还下载了 sass-loader 中。

var path = require('path');
var autoprefixer = require('autoprefixer');
var webpack = require('webpack');
var findCacheDir = require('find-cache-dir');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
var InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
var WatchMissingNodeModulesPlugin = require('react-dev-   utils/WatchMissingNodeModulesPlugin');
var getClientEnvironment = require('./env');
var paths = require('./paths');

var publicPath = '/';

var env = getClientEnvironment(publicUrl);

module.exports = {

  entry: [

    require.resolve('react-dev-utils/webpackHotDevClient'),

    require.resolve('./polyfills'),

    paths.appIndexJs

  ],
  output: {

    path: paths.appBuild,

    pathinfo: true,

    filename: 'static/js/bundle.js',

    publicPath: publicPath
  },
  resolve: {

    fallback: paths.nodePaths,

    extensions: ['.js', '.json', '.jsx', ''],
    alias: {

      'react-native': 'react-native-web'
    }
  },

  module: {
    // First, run the linter.
    // It's important to do this before Babel processes the JS.
    preLoaders: [
      {
        test: /\.(js|jsx)$/,
        loader: 'eslint',
        include: paths.appSrc,
      }
    ],
    loaders: [
      // Process JS with Babel.
      {
        test: /\.(js|jsx)$/,
        include: paths.appSrc,
        loader: 'babel',
        query: {

          // This is a feature of `babel-loader` for webpack (not Babel itself).
          // It enables caching results in ./node_modules/.cache/react-scripts/
          // directory for faster rebuilds. We use findCacheDir() because of:
          // https://github.com/facebookincubator/create-react-app/issues/483
          cacheDirectory: findCacheDir({
            name: 'react-scripts'
          })
        }
      },
      // "postcss" loader applies autoprefixer to our CSS.
      // "css" loader resolves paths in CSS and adds assets as dependencies.
      // "style" loader turns CSS into JS modules that inject <style> tags.
      // In production, we use a plugin to extract that CSS to a file, but
      // in development "style" loader enables hot editing of CSS.
      {
        test: /\.css$/,
        loader: 'style!css?importLoaders=1!postcss'
      },
      {
        test: /\.scss$/,
        loaders: ["style", "css", "sass"],
        exclude: /node_modules(?!\/react-conventions)/
      },
      {
        test: /\.sass$/,
        loaders: ["style", "css", "sass"]
      },
      // JSON is not enabled by default in Webpack but both Node and Browserify
      // allow it implicitly so we also enable it.
      {
        test: /\.json$/,
        loader: 'json'
      },
      // "file" loader makes sure those assets get served by WebpackDevServer.
      // When you `import` an asset, you get its (virtual) filename.
      // In production, they would get copied to the `build` folder.
      {
        test: /\.(ico|jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2)(\?.*)?$/,
        loader: 'file',
        query: {
          name: 'static/media/[name].[hash:8].[ext]'
        }
      },
      // "url" loader works just like "file" loader but it also embeds
      // assets smaller than specified size as data URLs to avoid requests.
      {
        test: /\.(mp4|webm|wav|mp3|m4a|aac|oga)(\?.*)?$/,
        loader: 'url',
        query: {
          limit: 10000,
          name: 'static/media/[name].[hash:8].[ext]'
        }
      }
    ]
  },

  // We use PostCSS for autoprefixing only.
  postcss: function() {
    return [
      autoprefixer({
        browsers: [
          '>1%',
          'last 4 versions',
          'Firefox ESR',
          'not ie < 9', // React doesn't support IE8 anyway
        ]
      }),
    ];
  },
  plugins: [

    new InterpolateHtmlPlugin({
      PUBLIC_URL: publicUrl
    }),
    // Generates an `index.html` file with the <script> injected.
    new HtmlWebpackPlugin({
      inject: true,
      template: paths.appHtml,
    }),

    new webpack.DefinePlugin(env),
    // This is necessary to emit hot updates (currently CSS only):
    new webpack.HotModuleReplacementPlugin(),

    new CaseSensitivePathsPlugin(),

    new WatchMissingNodeModulesPlugin(paths.appNodeModules)
  ],

  node: {
    fs: 'empty',
    net: 'empty',
    tls: 'empty'
  }
};

我尝试使用导入我的组件import Input from 'react-conventions/lib/Input';但我仍然收到错误

编译失败。

./~/react-conventions/lib/components/Input/style.scss 模块解析失败:/Users/user/Desktop/App/node_modules/react-conventions/lib/components/Input/style.scss Unexpected character ' @' (1:0) 你可能需要一个合适的加载器来处理这个文件类型。SyntaxError: 意外字符 '@' (1:0) @ ./~/react-conventions/lib/components/Input/Input.js 19:13-36

这可能是图书馆的问题吗?值得怀疑,但我只是没有看到我缺少的东西。

4

2 回答 2

2

试试这个(排除负前瞻):

 {
    test: /\.scss$/,
    loaders: ["style", "css", "sass"],
    exclude: /node_modules(?!\/react-conventions)/
 }
于 2016-11-26T16:16:22.400 回答
1

尝试将此添加到您的 package.json 脚本

"scripts": {
"build-css": "node-sass src/ -o src/",
"watch-css": "npm run build-css && node-sass src/ -o src/ --watch --recursive"

并安装 node-sass,因为脚本依赖于它。假设您尚未弹出项目,这将起作用。

于 2017-03-09T22:53:12.763 回答