【问题标题】:add material-components-web.css via webpack通过 webpack 添加 material-components-web.css
【发布时间】:2017-12-08 18:23:11
【问题描述】:

我将 mterial-components-web.css 添加到我的 index.html 文件的标题中。

 <script src="node_modules/material-components-web/dist/material-components-web.js"></script>
 <script  src="dist/bundle.js"></script>

css 组件运行良好。现在我通过 webpack 添加了一些 javscript 组件。现在我想我可以将所有 vai webpack 添加到我的 bundle.js 中。

import 'material-components-web/dist/material-components-web.js';

没有错误,但没有加载样式!问题出在哪里?

问候

我的 webpack 配置。

const path = require('path');
const ExtractTextPlugin = require("extract-text-webpack-plugin");

module.exports = {
  entry: './src/app.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js'
  },
  module: {
    rules: [
       {
        test: /\.scss$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: ['css-loader', 'sass-loader']
        })
      }
    ]
  },
  plugins: [
    new ExtractTextPlugin('style.css')
  ],
  devServer: {
    contentBase: "./build"
  }
};

【问题讨论】:

    标签: node.js webpack material-components-web


    【解决方案1】:

    如果不查看 webpack 配置,很难确定确切原因。

    尽管如此,CSS 需要两个加载器,正如 webpack docs 中所建议的那样。我只能猜测装载机没有到位。

    也可以按照here的说明使用内联导入

    虽然不接近您尝试的内容,但请随意扫描this 具有的简单代码。它基本上通过 webpack 捆绑了 bootstrap css。但是请注意,我使用的是require() 而不是import。在 webpack 配置中添加适当的 js 预编译器应该不难切换。

    【讨论】:

    • 好的,你说得对,我切换到 require ('./../node_modules/material-components-web/dist/material-components-web.js');现在它正确加载了,但是 window.mdc.autoInit() 不知道 mdc!如何正确加载脚本?我将我的 webpack.config 粘贴到原始帖子中。
    • 查看配置,捆绑的代码放在dist文件夹中。 devServer 正在从build 服务,这是预期的吗? index.html 不被 webpack 处理。这意味着它可以包含脚本(即由 webpack 捆绑的脚本:bundle.js。但是,我建议你使用html-webpack-pugin。因为 webpack 会将 index.html 复制到 @987654334,Thaat 会让你的生活更轻松@ 并且也插入脚本标签。参考:webpack.js.org/plugins/html-webpack-plugingithub.com/rakcheru/webpack-bootstrap/blob/master/…
    • 如您所见,github.com/rakcheru/webpack-bootstrap/blob/master/src/… 中没有脚本标签。那是因为 webpack 为你完成了工作。
    猜你喜欢
    • 2017-08-28
    • 2018-10-23
    • 2016-05-29
    • 1970-01-01
    • 2016-06-16
    • 2018-11-13
    • 2018-04-10
    • 2019-02-09
    • 2020-10-03
    相关资源
    最近更新 更多