通过Webpack要求Material-UI的正确方法

lazywei

我正在尝试通过webpack使用material-ui但是,我在chrome dev-tool中收到了错误消息

Uncaught ReferenceError: require is not defined

我的webpack.config.js:

var bower_dir = __dirname + '/bower_components';
var node_modules_dir = __dirname + '/node_modules';

var config = {
  addVendor: function (name, path) {
    this.resolve.alias[name] = path;
    this.module.noParse.push(new RegExp(path));
  },

  entry: {
    Messenger: './app/Messenger.jsx',
    AppComponent: './app/AppComponent.jsx'
  },

  // The resolve.alias object takes require expressions
  // (require('react')) as keys and filepath to actual
  // module as values
  resolve: {
    alias: {},
    extensions: ['', '.jsx']
  },

  output: {
    path: './www',
    filename: '[name].bundle.js'
  },

  module: {
    noParse: [],
    loaders: [
      { test: /\.css$/, loader: 'style-loader!css-loader' }, // use ! to chain loaders
      { test: /\.png$/, loader: "url-loader?limit=100000&mimetype=image/png" },
      { test: /\.jsx$/, loader: 'jsx-loader' }
    ]
  }
};

config.addVendor('react', bower_dir + '/react/react.min.js');
config.addVendor('material-ui', bower_dir + '/material-ui/src/index.js');
config.addVendor('react-tap-event-plugin', node_modules_dir + '/react-tap-event-plugin/src/injectTapEventPlugin.js');

module.exports = config;

我需要一些有关如何通过webpack要求material-ui的建议。谢谢。

lazywei

最后,我明白了。

答案很简单:

  1. material-ui通过npm安装npm install material-ui --save
  2. 添加resolve.moduleDirectoriesresolve.extensions
  3. 打开和声模式中jsx-loader通过jsx-loader?harmony

我最终得到了webpack.config.js:

var config = {
  entry: {
    Messenger: './app/Messenger.jsx',
    AppComponent: './app/AppComponent.jsx'
  },

  resolve: {
    modulesDirectories: ['node_modules'],
    alias: {},
    extensions: ['', '.jsx', '.js']
  },

  output: {
    path: './www',
    filename: '[name].bundle.js'
  },

  module: {
    noParse: [],
    loaders: [
      { test: /\.css$/, loader: 'style-loader!css-loader' }, // use ! to chain loaders
      { test: /\.png$/, loader: "url-loader?limit=100000&mimetype=image/png" },
      { test: /\.jsx$/, loader: 'jsx-loader?harmony' }
    ]
  }
};

module.exports = config;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何通过Webpack的外部使用CDN导入ReactJS Material UI?

使用material-ui CSS的正确方法是什么?

使用material-ui应用程序栏和material-ui-next左右浮动的正确方法是什么?

要求root通过nmcli对NetworkManager进行更改的“正确”方法

如何通过反射按要求/正确的顺序调用方法

在Material-UI自动完成中使用react-hook-form控制器的正确方法

在Material-ui包装器组件中定位和设置特定子元素样式的正确方法?

显示SelectField空白菜单项的正确方法是什么(material-ui,react)

Jest + Material-UI:正确模拟useMediaQuery

material ui:如何正确对齐输入标签

无法正确渲染Material UI React Grid

使用带有 typescript 和 material-ui 的 react-hook-form 来显示错误消息的正确方法

将 createMuiTheme 中定义的样式与 Material-UI 的 useStyles 一起使用的正确方法是什么?

通过回滚NUnit,SQL Server和UI测试中的事务来维护数据库已知状态的正确方法

如何根据Material UI(React JS)的要求制作“选择”组件

如何根据Material UI(React JS)的要求制作“选择”组件

Material UI IconButton 工具提示未正确显示

如何使用Material UI表分页显示每页正确的行

material-ui中正确的日期格式是什么?

我的Material UI AppBar是否在正确的轨道上?

我无法让 Material UI DatePicker 正确解析英国日期

Next JS 在刷新之前无法正确加载 Material UI 样式

Webpack通过变量动态要求npm模块

如何使用webpack正确管理jquery-ui

要求将导出的方法导入类的正确方法(原型?)

React + Material UI +打字稿+ Webpack + SSR不起作用

如何在Material UI的webpack构建中包括Roboto字体?

找不到模块 - webpack ts-loader @material-ui

React + Webpack + Material UI样式的生产中断