[英]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 编译失败。
正如@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>
生成
vue init webpack .
包管理器:npm
然后,安装 sass-loader
npm i -D sass-loader node-sass
vue.config.js
并用以下内容填充它:module.exports = {
css: {
loaderOptions: {
sass: {
includePaths: [
'./node_modules', //here I include node_modules
]
},
}
}
}
之后,安装一个包含 scss/sass 的模块(例如material-components-web
, npm i material-components-web
)
然后,创建一个导入到位于node_modules
中的样式表,如下所示:
@import '@material/button/mdc-button'; //mdc-button comes with material-components-web
npm run dev
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>
命令,或者如果你想修改现有项目:
打开build/utils.js
文件。
在exports.cssLoaders
函数中找到return...
:
return {... sass: generateLoaders('sass', { indentedSyntax: true }), scss: generateLoaders('sass'), ... }
像这样修改它:
const includePaths = [path.resolve(__dirname, '..', 'node_modules')]; return {... sass: generateLoaders('sass', { indentedSyntax: true, includePaths }), scss: generateLoaders('sass', { includePaths }), ... }
删除未使用vue.config.js
文件。
你可以给我演示吗? 我不想使用任何框架集成或包装器! 如何使用material-components-web?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.