Webpack 2迁移打破ES6模块导入

时间:2016-12-02 19:20:29

标签: reactjs webpack babeljs

所以我最近在React应用上升级到webpack 2。它开箱即用,没有我改变任何东西,但版本号!大。

然而,现在我试图让它为我捆绑我的ES6模块(导入,导出等),而babel和webpack似乎并不能很好地协同工作。

我为实现这一目标而做出的唯一改变就是改变我的.babelrc

{
  "presets": ["es2015", "stage-1", "react"],
  "plugins": ["transform-object-rest-spread"],
}

到此:

{
  "presets": [["es2015", {"modules": false}], "stage-1", "react"],
  "plugins": ["transform-object-rest-spread"],
}

Webpack仍然捆绑这个而不会抛出错误,但是当我打开应用程序时,我得到了可怕的"Warning: React.createElement: type should not be null, undefined, boolean, or number. It should be a string (for DOM elements) or a ReactClass (for composite components)."

Image of error Image of second error

堆栈跟踪将我带回我的应用程序的第一行(ReactDOM.render(...

从我可以看到,当您尝试使用import { Component } from './component.js'而不是import Component from './component.js'导入另一个组件的默认导出时,通常会出现此错误。我不明白为什么会出现这种情况,因为我知道我的代码可以与babel进行转换。

任何提示?

1 个答案:

答案 0 :(得分:1)

似乎没有必要import pygame import os import time img_path = os.path.join('test.jpg') class redEnemy(object): def __init__(self): self.image = pygame.image.load(img_path) self.x = 0 self.y = 208 def moveEnemy(self): dist = 3 if True: self.x += dist def draw(self, surface): surface.blit(self.image, (self.x, self.y)) lifes(self) def lifeText(life): font = pygame.font.SysFont("monospace", 20) text = font.render("Lifes %s" % life, True, black) textpos = text.get_rect() textpos.center = (50,20) screen.blit(text, textpos) def lifes(self): global life life = 50 if self.x > 950: life = life - 1 enemies.append(redEnemy()) enemies.remove(self) lifeText(life) pygame.init() screen = pygame.display.set_mode((960, 720)) black = (0,0,0) enemies = [redEnemy()] redenemy = enemies[0] clock = pygame.time.Clock() mapImg = pygame.image.load("mapimage.jpg") running = True while running: screen.fill((0,0,0)) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() running = False for enemy in enemies: enemy.moveEnemy() screen.blit(mapImg, (0,0)) for enemy in enemies: enemy.draw(screen) pygame.display.update() clock.tick(60) 并导致错误。

通过离开我的babelrc来管理以获得所需的行为,并将以下插件添加到我的webpack配置:["es2015", {"modules": false}]new webpack.optimize.CommonsChunkPlugin({...})

还根据this great tutorial进行了一些其他配置更改,这也可以帮助您单独捆绑供应商模块,并向您展示如何根据new HtmlWebpackPlugin({...})路由将代码拆分为块。