【发布时间】:2017-10-26 20:15:00
【问题描述】:
当我尝试通过 SASS 导入我 npm 安装的 material-components-web/material-components-web 时,我收到一个错误,告诉我该文件未找到或无法读取。
我在 App.vue 主文件中进行了导入:
<style lang="sass">
@import "material-components-web/material-components-web"
</style>
目录如下:
node_modules/
(lots of packages)
@material/
(lots of material components)
material-components-web/
material-components-web.scss
src/
App.vue
main.js
index.html
package.json
webpack.config.js
我的第一次尝试解决这个问题:
显然我首先想到的是路径不正确并尝试导入../node_modules/material-components-web/material-components,而不是哪种工作。然而material-components-web 也在导入东西(来自@material 的每个单独的材料组件)。因此,即使 material-components-web 已加载,它终究还是会中断。
我还在material-components-web的vue示例的源代码中看到他们只导入material-components-web/material-components而不是../node_modules/material-components-web/material-components,所以问题肯定出在其他地方。
然后我偶然发现了一些有类似问题的人。他们都被告知在他们的 webpack 配置中实现类似的东西:
{
loader: 'sass-loader',
options: {
sourceMap: true,
includePaths: glob.sync('packages/*/node_modules').map((d) => path.join(__dirname, d)),
},
}
我也试过了。没用。 我克隆了 material-components-web 的vue-example,这令人震惊地也没有用。 (虽然它应该可以正常工作?我的意思是这是官方示例)
所以我的问题是:这是一个错误吗?如果不是,我做错了什么?
这里有更多的代码来获得概览:
完全错误
./node_modules/css-loader!./node_modules/vue-loader/lib/style-compiler?{"vue":true,"id":"data-v-0219ec88","scoped":false,"hasInlineConfig":false}!./node_modules/sass-loader/lib/loader.js?indentedSyntax!./node_modules/vue-loader/lib/selector.js?type=styles&index=0&bustCache!./src/App.vue
Module build failed:
undefined
^
File to import not found or unreadable: material-components-web/material-components-web.
Parent style sheet: stdin
in somedirectory\src\App.vue (line 30, column 1)
@ ./node_modules/vue-style-loader!./node_modules/css-loader!./node_modules/vue-loader/lib/style-compiler?{"vue":true,"id":"data-v-0219ec88","scoped":false,"hasInlineConfig":false}!./node_modules/sass-loader/lib/loader.js?indentedSyntax!./node_modules/vue-loader/lib/selector.js?type=styles&index=0&bustCache!./src/App.vue 4:14-342 13:3-17:5 14:22-350
@ ./src/App.vue
@ ./src/main.js
@ multi (webpack)-dev-server/client?http://localhost:8080 webpack/hot/dev-server ./src/main.js
webpack.config.js 的相关部分
var path = require('path')
var webpack = require('webpack')
module.exports = {
entry: './src/main.js',
output: {
path: path.resolve(__dirname, './dist'),
publicPath: '/dist/',
filename: 'build.js'
},
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
loaders: {
// Since sass-loader (weirdly) has SCSS as its default parse mode, we map
// the "scss" and "sass" values for the lang attribute to the right configs here.
// other preprocessors should work out of the box, no loader config like this necessary.
'scss': 'vue-style-loader!css-loader!sass-loader',
'sass': 'vue-style-loader!css-loader!sass-loader?indentedSyntax'
}
// other vue-loader options go here
}
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.(png|jpg|gif|svg)$/,
loader: 'file-loader',
options: {
name: '[name].[ext]?[hash]'
}
}
]
},
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js'
}
},
devServer: {
historyApiFallback: true,
noInfo: true,
overlay: true
},
performance: {
hints: false
},
devtool: '#eval-source-map'
}
App.vue
<template>
<div id="app">
<!-- Html Stuff -->
</div>
</template>
<style lang="sass">
@import "material-components-web/material-components-web"
</style>
<script>
export default {
name: 'app',
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
}
}
</script>
main.js
import Vue from 'vue'
import App from './App.vue'
new Vue({
el: '#app',
render: h => h(App)
})
【问题讨论】: