使用电子和网络包需要vm2

时间:2016-06-10 12:12:49

标签: javascript node.js webpack electron

当我尝试使用Webpack在Electron渲染器进程中导入vm2模块时遇到错误。

import fs from 'fs'

console.log('ok')

const { NodeVM } = require('vm2')

console.log('ko')

这是Webpack显示的错误:

WARNING in ./~/vm2/lib/main.js
Critical dependencies:
180:26-33 require function is used in a way in which dependencies cannot be statically extracted
335:15-22 require function is used in a way in which dependencies cannot be statically extracted
367:26-33 require function is used in a way in which dependencies cannot be statically extracted
 @ ./~/vm2/lib/main.js 180:26-33 335:15-22 367:26-33

WARNING in ./~/vm2/lib/sandbox.js
Module parse failed: /Users/guillaume/Voyager/Voyager/node_modules/vm2/lib/sandbox.js 'return' outside of function (20:0)
You may need an appropriate loader to handle this file type.
SyntaxError: 'return' outside of function (20:0)
    at Parser.pp.raise (/Users/guillaume/Voyager/Voyager/node_modules/acorn/dist/acorn.js:923:13)
    at Parser.pp.parseReturnStatement (/Users/guillaume/Voyager/Voyager/node_modules/acorn/dist/acorn.js:1864:74)

我尝试从我的webpack配置中排除node_modules文件夹:

module: {

    loaders: [{
      test: /\.js$/,
      include: PATHS.app,
      loader: 'babel',
      exclude: /node_modules/,
      query: {
        presets: [ 'es2015', 'react', 'stage-1' ],
        plugins: [ 'add-module-exports', 'transform-decorators-legacy' ],
      },
    }, {
      test: /\.scss$/,
      loaders: [ 'style', 'css', 'postcss-loader?parser=postcss-scss' ],
      include: PATHS.app,
    }],
  }

但它似乎不起作用。出现第一个console.log,而不是第二个webpack-target-electron-renderer。 我也使用externals: [/vm2/]添加了目标。相同..

最后,当我添加Uncaught ReferenceError: vm2 is not defined时,Webpack不会抛出任何错误,但渲染器控制台会显示vm2

我猜这与require动态使用fs并且webpack不喜欢的事实有关(因为导入/** * Course * * @ORM\Table(name="course") * @ORM\Entity(repositoryClass="CoreBundle\Repository\CourseRepository") * @ORM\HasLifecycleCallbacks */ class Course { /** * @var int * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string * * @ORM\Column(name="name", type="string", length=255, unique=false) */ private $name; /** * @var ParentCategory * * @ORM\ManyToOne(targetEntity="ParentCategory") * @ORM\JoinColumn(name="parentCategory_id", referencedColumnName="id") */ private $parentCategory; /** * @var ChildCategory * * @ORM\ManyToOne(targetEntity="ChildCategory", inversedBy="courses") * @ORM\JoinColumn(name="childCategory_id", referencedColumnName="id") */ private $childCategory; /** * @var City * * @ORM\ManyToOne(targetEntity="City") * @ORM\JoinColumn(name="city_id", referencedColumnName="id") */ private $city; /** * @var Users * * @ORM\ManyToOne(targetEntity="UserBundle\Entity\Users", inversedBy="course") * @ORM\JoinColumn(name="owner_id", referencedColumnName="id") */ private $owner; /** * @var text * * @ORM\Column(name="description", type="text") */ private $description; /** * @var Picture * * @ORM\OneToMany(targetEntity="CoreBundle\Entity\Picture", mappedBy="course", cascade={"persist", "remove"}) */ private $pictures; /** * @var Ratings * * @ORM\OneToMany(targetEntity="CoreBundle\Entity\Ratings", mappedBy="courseid") */ private $ratings; public $avgRating; } 工作正常)

问题出在哪里?如何解决?

0 个答案:

没有答案