css - 使用 Webpack 的提取文本插件捆绑时,LESS 导入不起作用

标签 css less webpack

我在开发中一直在玩 less,但由于某种原因,当我使用提取文本插件将其捆绑用于生产时,导入功能不起作用。

对于下面的 index.less 文件:

@import 'global-styles/variables.less';

h1, h2, h3, h4, h5, h6,
.h1, .h2, .h3, .h4, .h5, .h6 {
  font-family: @font-family;
  font-weight: @headings-font-weight;
}

我收到以下错误:

Module build failed: variable @font-family is undefined
 @ /Users/mattdalton/WebStorm Projects/react-comments/client/src/index.less        (line 5, column 15)
 near lines:
   .h1, .h2, .h3, .h4, .h5, .h6 {
     font-family: @font-family;
     font-weight: @headings-font-weight;

我已经使用以下 Webpack 配置捆绑了它:

module.exports = {
  bail: true,
  devtool: 'source-map',
  entry: [
    require.resolve('./polyfills'),
    path.join(paths.appSrc, 'index')
  ],
  output: {
    path: paths.appBuild,
    filename: 'static/js/[name].[chunkhash:8].js',
    chunkFilename: 'static/js/[name].[chunkhash:8].chunk.js',
    publicPath: publicPath
  },
  resolve: {
    extensions: ['.js', '.json', ''],
    alias: {
       'react-native': 'react-native-web'
    }
  },
  loaders: [
  {
    test: /\.js$/,
    include: paths.appSrc,
    loader: 'babel',
    query: require('./babel.prod')
  },
  {
    test: /\.css$/,
    loader: ExtractTextPlugin.extract('style', 'css?-autoprefixer!postcss')
  },
  {
    test: /\.less$/,
    loader: ExtractTextPlugin.extract('style', 'css?-autoprefixer!postcss!less')
  },
 //.....
  postcss: function() {
    return [
      autoprefixer({
        browsers: [
          '>1%',
          'last 4 versions',
          'Firefox ESR',
          'not ie < 9', 
        ]
      }),
    ];
  },
  plugins: [
    // Generates an `index.html` file with the <script> injected.
    new HtmlWebpackPlugin({
      inject: true,
      template: paths.appHtml,
      minify: {
        removeComments: true,
        collapseWhitespace: true,
        removeRedundantAttributes: true,
        useShortDoctype: true,
        removeEmptyAttributes: true,
        removeStyleLinkTypeAttributes: true,
        keepClosingSlash: true,
        minifyJS: true,
        minifyCSS: true,
        minifyURLs: true
      }
     }),
    new webpack.DefinePlugin(env),
    new webpack.optimize.OccurrenceOrderPlugin(),
    new webpack.optimize.DedupePlugin(),
    new webpack.optimize.UglifyJsPlugin({
      compress: {
        screw_ie8: true, 
        warnings: false
     },
      mangle: {
        screw_ie8: true
      },
      output: {
        comments: false,
        screw_ie8: true
      }
    }),
    new ExtractTextPlugin('static/css/[name].[contenthash:8].css')
  ]
};

我用几个不同的无组件文件试过这个,我遇到了同样的问题。

有什么想法吗?

最佳答案

对于阅读本文的任何人,我没有直接回答这个问题,但我确实通过切换到 sass 解决了这个问题。

我安装了 sass-loadernode-sass,将文件名更改为 .scss,然后简单地将我的变量更改为 sass 语法(即 $ 而不是 @)。一旦我在 webpack 中将 less 更改为 sass 一切正常。我只能假设 less-loader 或者它与另一个加载器的兼容性有问题。

可能对遇到同样问题的人有用 - SASS 似乎在 Webpack/React-land 中得到更好的支持,所以如果可能的话,进行切换可能是个好主意。

关于css - 使用 Webpack 的提取文本插件捆绑时,LESS 导入不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39546191/

相关文章:

CSS 左对齐 float 但受右边距限制

html - Bootstrap 行超过 Container-Fluid

css - 样板 LESS 框架应该如何定制?

twitter-bootstrap - meteor 编译自定义 Bootstrap 少的例子?

javascript - Webpack 在包输出中导致语法错误

html - 我在外部 div 内水平放置了 2 个 div。当第 2 个内部 div 的内容变长时,它应该包裹在自己的 div 区域

html - 如何正确叠加透明背景的图像?

html - Bootstrap col-md-3 类不工作

javascript - 如果文件导入命名导出,webpack-dev-server 不会监视/重新编译文件

javascript - 如何让 Webpack Dev Server 显示本地存储的图片?