Electron / Angular2 / Typings App:组件中的导入模块

时间:2016-02-12 15:49:37

标签: node.js typescript angular webpack definitelytyped

我是打字稿和Angular2的新手。有一天,我尝试使用Angular2,Webpack和Typings节点模块在电子上运行应用程序。问题在于,每当我在互联网上找到示例时,电子被电子预制作为节点模块加载。我想要的是能够将电子导入任何组件,以便能够从该组件更新我的应用程序。

我尝试使用typings节点模块从DefinitelyTyped添加github-electron.d.ts,将其导入我的组件,如下所示:

 import electron = require('electron')

但是当我启动应用程序(而不是在构建期间)时,我遇到了问题

   Module not found: Error: Cannot resolve module 'electron' in /home/stephane/workspace/hacker-keyboard/app
resolve module electron in /home/xxx/app
   looking for modules in /home/xxx/node_modules

有人有这种架构的例子或者知道错误是什么吗?

我的配置:

树:

├── app
│   ├── app.ts
│   ├── index.html
│   ├── main.js
│   └── package.json
├── build
│   ├── angular2.js
│   ├── angular2.js.map
│   ├── app.js
│   ├── app.js.map
│   ├── common.js
│   ├── common.js.map
│   ├── electron.js
│   └── electron.js.map
├── node_modules
│   ├── angular2
│   ├── bootstrap
│   ├── electron-prebuilt
│   ├── es6-promise
│   ├── es6-shim
│   ├── gulp
│   ├── reflect-metadata
│   ├── rxjs
│   ├── ts-loader
│   ├── typescript
│   ├── typings
│   ├── webpack
│   ├── webpack-dev-server
│   └── zone.js
├── typings
│   ├── browser
│   │   └── ambient
│   │       └── github-electron
│   │           └── github-electron.d.ts
│   ├── browser.d.ts
│   ├── main
│   │   └── ambient
│   │       └── github-electron
│   │           └── github-electron.d.ts
│   └── main.d.ts
├── package.json
├── readme.md
├── tsconfig.json
└── webpack.config.js

配置文件:

tsconfig.json

{
  "compilerOptions": {
    "target": "ES5",
    "module": "commonjs",
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "sourceMap": true
  },
  "files": [
    "app/app.ts",
    "typings/main.d.ts"
  ]
}

webpack.config.js

var path = require('path');
var webpack = require('webpack');
var CommonsChunkPlugin = webpack.optimize.CommonsChunkPlugin;

module.exports = {
  devtool: 'source-map',
  debug: true,

  entry: {
    'angular2': [
      'rxjs',
      'reflect-metadata',
      'angular2/core',
      'angular2/router',
      'angular2/http'
    ],
    'app': './app/app'
  },

  output: {
    path: __dirname + '/build/',
    publicPath: 'build/',
    filename: '[name].js',
    sourceMapFilename: '[name].js.map',
    chunkFilename: '[id].chunk.js'
  },

  resolve: {
    extensions: ['','.ts','.js','.json', '.css', '.html']
  },

  module: {
    loaders: [
      {
        test: /\.ts$/,
        loader: 'ts',
        exclude: [ /node_modules/ ]
      }
    ]
  },

  plugins: [
    new CommonsChunkPlugin({ name: 'angular2', filename: 'angular2.js', minChunks: Infinity }),
    new CommonsChunkPlugin({ name: 'common',   filename: 'common.js' })
  ],
  target:'node-webkit'
};

的package.json

{
  "name": "image-size-calculator",
  "version": "0.0.1",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "webpack --progress --profile --colors --display-error-details --display-cached",
    "watch": "webpack --watch --progress --profile --colors --display-error-details --display-cached",
    "electron": "electron app"
  },
  "author": "Auth0",
  "license": "MIT",
  "devDependencies": {
    "electron-prebuilt": "^0.35.4",
    "es6-shim": "^0.34.0",
    "ts-loader": "^0.7.2",
    "typescript": "^1.7.3",
    "webpack": "^1.12.9",
    "webpack-dev-server": "^1.14.0"
  },
  "dependencies": {
    "angular2": "2.0.0-beta.2",
    "bootstrap": "^3.3.6",
    "es6-shim": "^0.33.3",
    "gulp": "^3.9.0",
    "reflect-metadata": "0.1.2",
    "rxjs": "5.0.0-beta.0",
    "typings": "^0.6.8",
    "zone.js": "^0.5.10"
  }
}

1 个答案:

答案 0 :(得分:0)

你需要改变 target: 'node-webkit'来 你的webpack.config.js中的target: 'electron-renderer',之后webpack能够在我的应用中找到电子