gpt4 book ai didi

javascript - 使用 webpack 从 MDL 加载特定组件

转载 作者:行者123 更新时间:2023-11-30 15:45:47 25 4
gpt4 key购买 nike

我只想在我的项目中使用 MDL 的布局组件。

因此,在我的 app.css 文件中,我导入了这个:

@import url('~material-design-lite/src/layout/_layout.scss');

在构建过程中出现以下错误:

ERROR in ./~/css-loader!./~/material-design-lite/src/layout/_layout.scss
Module not found: Error: Cannot resolve 'file' or 'directory' ../variables in /Users/Mitch/Documents/project/web/node_modules/material-design-lite/src/layout
@ ./~/css-loader!./~/material-design-lite/src/layout/_layout.scss 3:10-66

ERROR in ./~/css-loader!./~/material-design-lite/src/layout/_layout.scss
Module not found: Error: Cannot resolve 'file' or 'directory' ../mixins in /Users/Mitch/Documents/project/web/node_modules/material-design-lite/src/layout
@ ./~/css-loader!./~/material-design-lite/src/layout/_layout.scss 4:10-63

_layout.sccs 以这些行开头

@import "../variables";
@import "../mixins";

知道我做错了什么吗?

我的 webpack.config.js 的相关部分:

const PATHS = {
node_modules: path.resolve(__dirname, 'node_modules'),
build: path.join(__dirname, 'public', 'build'),
js: path.resolve(__dirname, 'public', 'js'),
css: path.resolve(__dirname, 'public', 'css'),
};

...
resolve: {
root: [
path.resolve(__dirname),
PATHS.node_modules,
PATHS.js,
PATHS.css
],
...
extensions: ['', '.js', '.jsx', '.json', '.css', '.scss']
},
module: {
loaders: [
{
test: /\.css$/,
loader: ExtractTextPlugin.extract('style-loader', 'css-loader')
},
{
test: /\.scss$/,
loader: ExtractTextPlugin.extract('style', 'css?modules!sass')
},
{
test: /\.png$/,
loader: 'url-loader?limit=100000'
},
{
test: /\.jpg$/,
loader: 'file-loader'
},
{
test: /\.gif$/,
loader: 'file-loader'
},
{
test: /\.(woff|woff2)(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url?limit=10000&mimetype=application/font-woff'
},
{
test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url?limit=10000&mimetype=application/octet-stream'
},
{
test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
loader: 'file'
},
{
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url?limit=10000&mimetype=image/svg+xml'
},
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: ['es2015']
}
}
]
}
...

相关依赖:

...
"devDependencies": {
"babel-core": "^6.14.0",
"babel-loader": "^6.2.5",
"babel-preset-es2015": "^6.14.0",
"css-loader": "^0.25.0",
"eslint": "^3.6.1",
"extract-text-webpack-plugin": "^1.0.1",
"file-loader": "^0.9.0",
"node-sass": "^3.10.1",
"sass-loader": "^4.0.2",
"style-loader": "^0.13.1",
"url-loader": "^0.5.7",
"webpack": "^1.13.2",
"webpack-validator": "^2.2.7"
}
...

更新

我试过@import url('~material-design-lite/src/material-design-lite.scss');,因为这不是partial,在我修改为只导入布局组件。

现在我收到以下错误:

ERROR in ./~/css-loader!./~/material-design-lite/src/material-design-lite.scss
Module build failed: Unknown word (19:1)

17 | /* Material Design Lite */
18 |
> 19 | // Variables and mixins
| ^
20 | @import "variables";
21 | @import "mixins";
22 |

@ ./~/css-loader!./public/css/app.css 3:10-114

评论失败。我不知道如何解决这个问题,我尝试使用 postcsspostcss-scss 加载器和 postcss-strip-inline-comments 插件来徒劳无功;旧错误只是被新错误所取代。

最佳答案

您的示例中的实际问题是您像这样导入 MDL

@import url('~material-design-lite/src/layout/_layout.scss');

代替

@import '~material-design-lite/src/layout/_layout.scss';

第一个被 sass-loader 忽略(因为 SASS 没有 url() 导入)并且将被 css-loader 处理。然而,css-loader 不会自动将 .scss 扩展为 variables,例如。这就是您收到这些“找不到模块”错误的原因。

关于javascript - 使用 webpack 从 MDL 加载特定组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40095558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com