css - 用于 css 和 scss 的 Webpack 加载器

标签 css reactjs sass webpack webpack-style-loader

我正在为我的 React 应用程序使用 .scss 文件,并且需要使用需要 .css 文件的第三方组件。我将我的 webpack.config 文件更新为:

import path from 'path';
import webpack from 'webpack';
import extend from 'extend';
import AssetsPlugin from 'assets-webpack-plugin';

const DEBUG = !process.argv.includes('--release');
const VERBOSE = process.argv.includes('--verbose');
const AUTOPREFIXER_BROWSERS = [
  'Android 2.3',
  'Android >= 4',
  'Chrome >= 35',
  'Firefox >= 31',
  'Explorer >= 9',
  'iOS >= 7',
  'Opera >= 12',
  'Safari >= 7.1',
];
const GLOBALS = {
  'process.env.NODE_ENV': DEBUG ? '"development"' : '"production"',
  __DEV__: DEBUG,
};

//
// Common configuration chunk to be used for both
// client-side (client.js) and server-side (server.js) bundles
// -----------------------------------------------------------------------------

const config = {
  output: {
    publicPath: '/',
    sourcePrefix: '  ',
  },

  cache: DEBUG,
  debug: DEBUG,

  stats: {
    colors: true,
    reasons: DEBUG,
    hash: VERBOSE,
    version: VERBOSE,
    timings: true,
    chunks: VERBOSE,
    chunkModules: VERBOSE,
    cached: VERBOSE,
    cachedAssets: VERBOSE,
  },

  plugins: [
    new webpack.optimize.OccurenceOrderPlugin(),
  ],

  resolve: {
    extensions: ['', '.webpack.js', '.web.js', '.js', '.jsx', '.json'],
  },

  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        include: [
          path.resolve(__dirname, '../node_modules/react-routing/src'),
          path.resolve(__dirname, '../src'),
        ],
        loader: 'babel-loader',
    }, {
        test: /\.css$/,
        loaders: [ 'style-loader', 'css-loader?module&localIdentName=[local]'],
      },
    {
        test: /\.scss$/,
        loaders: [
          'isomorphic-style-loader',
          `css-loader?${DEBUG ? 'sourceMap&' : 'minimize&'}modules&localIdentName=` +
          `${DEBUG ? '[name]_[local]_[hash:base64:3]' : '[hash:base64:4]'}`,
          'postcss-loader?parser=postcss-scss',
        ],
      }, {
        test: /\.json$/,
        loader: 'json-loader',
      }, {
        test: /\.txt$/,
        loader: 'raw-loader',
      }, {
        test: /\.(png|jpg|jpeg|gif|svg|woff|woff2)$/,
        loader: 'url-loader?limit=10000',
      }, {
        test: /\.(eot|ttf|wav|mp3)$/,
        loader: 'file-loader',
      },
    ],
  },

  postcss: function plugins(bundler) {
    return [
      require('postcss-import')({ addDependencyTo: bundler }),
      require('precss')(),
      require('autoprefixer')({ browsers: AUTOPREFIXER_BROWSERS }),
    ];
  },
};

//
// Configuration for the client-side bundle (client.js)
// -----------------------------------------------------------------------------

const clientConfig = extend(true, {}, config, {
  entry: './src/client.js',
  output: {
    path: path.join(__dirname, '../build/public'),
    filename: DEBUG ? '[name].js?[hash]' : '[name].[hash].js',
  },

  // Choose a developer tool to enhance debugging
  // http://webpack.github.io/docs/configuration.html#devtool
  devtool: DEBUG ? 'cheap-module-eval-source-map' : false,
  plugins: [
    new webpack.DefinePlugin(GLOBALS),
    new AssetsPlugin({
      path: path.join(__dirname, '../build'),
      filename: 'assets.js',
      processOutput: x => `module.exports = ${JSON.stringify(x)};`,
    }),
    ...(!DEBUG ? [
      new webpack.optimize.DedupePlugin(),
      new webpack.optimize.UglifyJsPlugin({
        compress: {
          // jscs:disable requireCamelCaseOrUpperCaseIdentifiers
          screw_ie8: true,

          // jscs:enable requireCamelCaseOrUpperCaseIdentifiers
          warnings: VERBOSE,
        },
      }),
      new webpack.optimize.AggressiveMergingPlugin(),
    ] : []),
  ],
});

//
// Configuration for the server-side bundle (server.js)
// -----------------------------------------------------------------------------

const serverConfig = extend(true, {}, config, {
  entry: './src/server.js',
  output: {
    path: './build',
    filename: 'server.js',
    libraryTarget: 'commonjs2',
  },
  target: 'node',
  externals: [
    /^\.\/assets$/,
    function filter(context, request, cb) {
      const isExternal =
        request.match(/^[@a-z][a-z\/\.\-0-9]*$/i) &&
        !request.match(/^react-routing/) &&
        !context.match(/[\\/]react-routing/);
      cb(null, Boolean(isExternal));
    },
  ],
  node: {
    console: false,
    global: false,
    process: false,
    Buffer: false,
    __filename: false,
    __dirname: false,
  },
  devtool: 'source-map',
  plugins: [
    new webpack.DefinePlugin(GLOBALS),
    new webpack.BannerPlugin('require("source-map-support").install();',
      { raw: true, entryOnly: false }),
  ],
});

export default [clientConfig, serverConfig];

但是,当我尝试运行我的应用程序时,我得到了 SyntaxError: Unexpected Token . 我是 webpack 的新手,所以我不确定我在这里遗漏了什么。我需要更改什么才能在我的应用程序中使用 .css 文件和 .scss 文件?

完整的错误信息:SyntaxError: Unexpected token . at exports.runInThisContext (vm.js:53:16) at Module._compile (module.js:387:25) at Object.Module._extensions..js (module.js:422:10) at Module.load (module.js:357:32) at Function.Module._load (module.js:314:12) at Module.require (module.js:367:17) at require (internal/module.js:16:19) at Object.<anonymous> (/Users/erichardson/Projects/interview_react/build/webpack:/external "react-flexr/styles.css":1:1) at __webpack_require__ (/Users/erichardson/Projects/interview_react/build/webpack:/webpack/bootstrap 1793a21c8f893ac9d09a:19:1) at Object.<anonymous> (/Users/erichardson/Projects/interview_react/build/server.js:3659:3)

最佳答案

样式文件 react-flexr/styles.css 解析为 external .因此,您需要:

1) 将 'style-loader' 改回 'isomorphic-style-loader',因为您在服务器端包中使用它。

2) 更新 externals 部分中的 filter 函数,以便 react-flexr/styles.css 不被解析为外部:

function filter(context, request, cb) {
  const isExternal =
    request.match(/^[@a-z][a-z\/\.\-0-9]*$/i) &&
    !request.match(/^react-flexr\/styles\.css/) &&
    !request.match(/^react-routing/) &&
    !context.match(/[\\/]react-routing/);
  cb(null, Boolean(isExternal));
}

关于css - 用于 css 和 scss 的 Webpack 加载器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36020274/

相关文章:

CSS 文本方向 : vertically oriented to the right

html - Canvas 响应式设计

html - 一次针对多个类

css - 使 div 与图标卡一样宽

css - Rails 5,Sass::SyntaxError 无法预编译 Assets

css - 未显示 Glyphicon 图标

html - 如何分配css?

reactjs - Material UI 自动完成芯片 onDelete 不起作用

javascript - 为什么它给我 'Cannot read property ' deleteProduct' of undefined' 错误 react Js

reactjs - 如何修复必须在 "Failed prop type: The property ` 上使用的 `grid` 的 `container` 间距?