【问题标题】:Why am I not being able to compile SASS with Webpack?为什么我无法使用 Webpack 编译 SASS?
【发布时间】:2016-08-30 02:12:15
【问题描述】:

我的 Webpack 配置中有以下模块:

module: {
    preLoaders: [
      {
        test: /\.vue$/,
        loader: 'eslint',
        include: projectRoot,
        exclude: /node_modules/
      },
      {
        test: /\.js$/,
        loader: 'eslint',
        include: projectRoot,
        exclude: /node_modules/
      }
    ],
    loaders: [
      {
        test: /\.vue$/,
        loader: 'vue'
      },
      {
        test: /\.js$/,
        loader: 'babel',
        include: projectRoot,
        exclude: /node_modules/
      },
      {
        test: /\.json$/,
        loader: 'json'
      },
      {
        test: /\.html$/,
        loader: 'vue-html'
      },
      {
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
        loader: 'url',
        query: {
          limit: 10000,
          name: utils.assetsPath('img/[name].[hash:7].[ext]')
        }
      },
      {
        test: /\.scss$/,
        loaders: ['style', 'css', 'sass']
      },
      {
        test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
        loader: 'url',
        query: {
          limit: 10000,
          name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
        }
      }
    ]
  },

您可以看到我正在使用 sass-loader,对于 *.scss 文件,我正在定义这个管道:['style', 'css', 'sass']

然后我有我的 scss 文件:

html {
  height: 100%;
}

body {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
}

(...)

最后,我的 HTML 页面(在来自 vue.js 的 VUE 文件中)导入了该 scss 文件:

<script>

require('./styles/main.scss')

(...)

</script>

但不知何故,我在启动项目时遇到了这个错误:

ERROR in ./~/css-loader!./~/sass-loader!./~/style-loader!./~/css-loader!./~/sass-loader!./src/styles/main.scss
Module build failed:
html {
^
      Invalid CSS after "...load the styles": expected 1 selector or at-rule, was "var content = requi"
      in /Users/td/uprank/src/styles/main.scss (line 1, column 1)
 @ ./src/styles/main.scss 4:14-247 13:2-17:4 14:20-253

为什么管道出错了? (看起来 webpack 正在尝试处理 ['css', 'sass', 'style', 'css', 'sass'] 而不仅仅是模块中配置的 ['style', 'css', 'sass']

我做错了什么?

谢谢!

编辑: 完整示例项目的链接:https://dl.dropboxusercontent.com/u/1066659/dummy.zip

【问题讨论】:

  • require('./styles/main.scss') 不应进入&lt;script&gt; 标签内。相反,它应该使用 sass 导入语法在 &lt;style lang="sass"&gt;&lt;/style&gt; 内。
  • 我现在编译没有显示错误。但是 CSS 不会呈现到结果页面中(即没有包含在 html 中)。
  • 我添加了一个完整项目的链接。

标签: webpack vue.js webpack-dev-server webpack-style-loader sass-loader


【解决方案1】:

之所以会这样,是因为 Vue 会自动为 CSS、SASS/SCSS、Stylus 生成加载器配置。

(参见projectRoot/build/utils.js 第~10 到~50 行)

所以你看到错误是因为你有一个 webpack 加载器正在尝试导入文件,然后 Vue 正在尝试导入(已经加载的)文件。所以你可以把你的加载器链想象成sass -&gt; css -&gt; style -&gt; sass -&gt; css -&gt; vue-style

为了解决这个问题,你必须摆脱你添加的加载器:

{
  test: /\.scss$/,
  loaders: ['style', 'css', 'sass']
},

并且只依赖提供的加载器。

您可以通过以下两种方式之一加载样式表:

注意:您必须使用scss 而不是sass,因为sass 将尝试使用缩进语法而不是括号语法来解析您的样式表。

1:

<style lang="scss">
  @import './styles/main.scss
</style>

2:

<style src="./styles/main.scss"></style>

这两个都将导入样式表,后者只是阻止您向组件添加任何其他样式。

【讨论】:

  • 最后一段的快速补充:你可以在一个 .vue 组件中使用多个 &lt;style&gt; 标签,所以虽然你不能在带有 src 属性的标签中添加任何 CSS 规则,但你可以添加第二个标签并将它们放在那里。
  • 你拯救了我的一天!
  • 如果要在JS文件中导入scss文件怎么办?
  • @TaylorGlaeser - 你是说 Vue 正在尝试确定加载程序,还是说 Vue-cli?
猜你喜欢
  • 2017-11-25
  • 2017-04-30
  • 2018-12-27
  • 1970-01-01
  • 2017-07-05
  • 2018-07-07
  • 2020-05-24
  • 2013-04-03
  • 1970-01-01
相关资源
最近更新 更多