繁体   English   中英

没有波浪号 (~) 无法从 node_modules 导入样式表

[英]Cannot import stylesheets from node_modules without tilde (~)

我正在尝试使用material-components-vue和 vue-cli 使用 webpack 创建一个简单的网络应用程序,但是,我发现如果没有前面的~ ,我无法从node_modules导入样式表。

我尝试了几个 webpack/vue-cli 配置,最后在vue.config.js中配置了一个传递加载器选项的配置。

我的vue.config.js看起来像这样:

module.exports = {
    css: {
      loaderOptions: {
        sass: {
          includePaths: [
              './node_modules', //here I include node_modules
            ]
        },
      }
    }
  }

所以我希望能够导入这样的东西:

@import 'normalize/normalize'

(假设我的node_modules中有一个名为normalize的目录,其中包含一个文件normalize.scss

然而,webpack 抛出一个错误,说它找不到模块。
但是,这确实有效:

@import '~normalize/normalize'

如果所有的@import都是我写的,这不会有问题,但是因为我使用了一个第三方模块,里面有@import ,所以 webpack 编译失败。

编辑 1:

正如@Styx要求的那样

请分享更多配置

显示 vue inspect --rule scss 的输出,以及这个有问题的导入的整个文件

这里是:

我有问题的文件很空:

<template>
  <div id="app">
    <m-button>Hello</m-button>
  </div>
</template>

<script>
import Vue from 'vue'
import Button from 'material-components-vue/dist/button'
Vue.use(Button)

export default {
  name: 'App',
  components: {
}
</script>

<style lang="scss">
@import "~material-components-vue/dist/button/styles"; //this works
@import "material-components-vue/dist/button/styles"; //but this does not
</style>

我的vue inspect --rule scss输出位于此处

所有其他配置由vue init webpack <name>生成

编辑 2:重现此问题的具体步骤:

  1. 初始化一个 vue-webpack 应用:
vue init webpack .
  1. Vue 构建:运行时 + 编译器(默认)
  2. Vue-路由器:没有
  3. 包管理器:npm

  4. 然后,安装 sass-loader

npm i -D sass-loader node-sass
  1. 创建一个文件vue.config.js并用以下内容填充它:
module.exports = {
    css: {
      loaderOptions: {
        sass: {
          includePaths: [
              './node_modules', //here I include node_modules
            ]
        },
      }
    }
  }
  1. 之后,安装一个包含 scss/sass 的模块(例如material-components-webnpm i material-components-web

  2. 然后,创建一个导入到位于node_modules中的样式表,如下所示:

@import '@material/button/mdc-button'; //mdc-button comes with material-components-web
  1. 最后,启动开发服务器:
npm run dev
  1. 它将抛出以下错误:
 ERROR  Failed to compile with 1 errors                                                                        11:36:35 AM

 error  in ./src/App.vue

Module build failed: 
@import '@material/button/mdc-button';
^
      File to import not found or unreadable: @material/button/mdc-button.
      in /home/maxim/projects/holiday.js/stackoverflow/src/App.vue (line 18, column 1)

 @ ./node_modules/vue-style-loader!./node_modules/css-loader?{"sourceMap":true}!./node_modules/vue-loader/lib/style-compil
er?{"vue":true,"id":"data-v-7ba5bd90","scoped":false,"hasInlineConfig":false}!./node_modules/sass-loader/lib/loader.js?{"s
ourceMap":true}!./node_modules/vue-loader/lib/selector.js?type=styles&index=0!./src/App.vue 4:14-359 13:3-17:5 14:22-367
 @ ./src/App.vue
 @ ./src/main.js
 @ multi (webpack)-dev-server/client?http://localhost:8080 webpack/hot/dev-server ./src/main.js

顺便说一句,在第一个示例中我想导入material-components-vue/dist/foo/styles ,但在这里我导入@material/foo

在此配置中,您的vue.config.js忽略 此文件@vue/cli-service使用,但您使用的是webpack-dev-server 因此,您的sass-loader不会收到此includePaths选项。

你可以使用现代vue create <app-name>命令,或者如果你想修改现有项目:

  1. 打开build/utils.js文件。

  2. exports.cssLoaders函数中找到return...

     return {... sass: generateLoaders('sass', { indentedSyntax: true }), scss: generateLoaders('sass'), ... }
  3. 像这样修改它:

     const includePaths = [path.resolve(__dirname, '..', 'node_modules')]; return {... sass: generateLoaders('sass', { indentedSyntax: true, includePaths }), scss: generateLoaders('sass', { includePaths }), ... }
  4. 删除未使用vue.config.js文件。

你可以给我演示吗? 我不想使用任何框架集成或包装器! 如何使用material-components-web?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM