babel-plugin-react-css-modules没有输出样式

时间:2018-03-21 23:26:37

标签: javascript reactjs webpack sass babel-plugin-react-css-modules

我正在使用babel-plugin-react-css-modules,它似乎正在创建className / styleName,但由于某种原因它不输出CSS以便模块可以读取它。

这是我的申请:launchpad

我正在使用webpack 4.2.0 / React 16并在从单独的配置文件导入的webpack.config中使用babel配置,因为您无法在.babelrc中设置上下文。

这是babel配置:

    {
      loader: 'babel-loader',
       options: {
        'plugins': [
           'react-hot-loader/babel',
           'transform-runtime',
           'transform-object-rest-spread',
           'transform-class-properties',
           ['react-css-modules',
            {
              context: paths.javascript,
              'generateScopedName': '[name]__[local]___[hash:base64:5]',
              'filetypes': {
                '.scss': {
                  'syntax': 'postcss-scss',
                  'plugins': ['postcss-nested']
                }
               },
              'exclude': 'node_modules',
              'webpackHotModuleReloading': true
            }
          ]
        ],
        'presets': [
          'env',
          'react',
          'flow'
        ],
        'env': {
          'production': {
            'presets': ['react-optimize']
          }
        }
      }
    }

这是我的网站:

const webpack = require('webpack')
const path = require('path')

const paths = require('./webpack/config').paths
const outputFiles = require('./webpack/config').outputFiles
const rules = require('./webpack/config').rules
const plugins = require('./webpack/config').plugins
const resolve = require('./webpack/config').resolve
const IS_PRODUCTION = require('./webpack/config').IS_PRODUCTION
const IS_DEVELOPMENT = require('./webpack/config').IS_DEVELOPMENT

const devServer = require('./webpack/dev-server').devServer

const DashboardPlugin = require('webpack-dashboard/plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')

// Default client app entry file
const entry = [
  'bootstrap-loader/extractStyles',
  path.join(paths.javascript, 'index.js')
]

plugins.push(
  // Creates vendor chunk from modules coming from node_modules folder
  new webpack.optimize.CommonsChunkPlugin({
    name: 'vendor',
    filename: outputFiles.vendor,
    minChunks (module) {
      const context = module.context
      return context && context.indexOf('node_modules') >= 0
    }
  }),
  // Builds index.html from template
  new HtmlWebpackPlugin({
    template: path.join(paths.source, 'index.html'),
    path: paths.build,
    filename: 'index.html',
    minify: {
      collapseWhitespace: true,
      minifyCSS: true,
      minifyJS: true,
      removeComments: true,
      useShortDoctype: true
    }
  })
)

if (IS_DEVELOPMENT) {
  // Development plugins
  plugins.push(
    // Enables HMR
    new webpack.HotModuleReplacementPlugin(),
    // Don't emmit build when there was an error while compiling
    // No assets are emitted that include errors
    new webpack.NoEmitOnErrorsPlugin(),
    // Webpack dashboard plugin
    new DashboardPlugin()
  )

  // In development we add 'react-hot-loader' for .js/.jsx files
  // Check rules in config.js
  rules[0].use.unshift('react-hot-loader/webpack')
  entry.unshift('react-hot-loader/patch')
}

// Webpack config
module.exports = {
  devtool: IS_PRODUCTION ? false : 'eval-source-map',
  context: paths.javascript,
  watch: IS_DEVELOPMENT,
  entry,
  output: {
    path: paths.build,
    publicPath: '/',
    filename: outputFiles.client
  },
  module: {
    rules
  },
  resolve,
  plugins,
  devServer
}

如果您运行npm startyarn start,代码将编译并运行,您可以看到基本的应用程序布局。如果检查反应代码,您将看到样式名称反映在组件中。没有发生的是没有输出样式。我似乎无法弄明白为什么。

感谢任何帮助。

0 个答案:

没有答案