Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 使用lerna子包为节点TS配置网页包_Node.js_Typescript_Webpack - Fatal编程技术网

Node.js 使用lerna子包为节点TS配置网页包

Node.js 使用lerna子包为节点TS配置网页包,node.js,typescript,webpack,Node.js,Typescript,Webpack,我有以下结构(基于勒纳): index.ts import { Server } from './src/server'; new Server() console.log('test'); import * as express from 'express'; export class Server { private app: express.Application constructor() { this.init(); this.bootstrap()

我有以下结构(基于勒纳):

index.ts

import { Server } from './src/server';

new Server()

console.log('test');
import * as express from 'express';

export class Server {

  private app: express.Application

  constructor() {
    this.init();
    this.bootstrap();
  }

  init(): void {
    this.app = express();
    this.app.get('/', (req, res, next) => {
      res.send("Hello world");
      next();
    });
  }

  bootstrap(): void {
    const PORT = 3000;
    this.app.listen(PORT, () => {
      console.log(`Express server listening on port ${PORT}.\nEnvironment: ${process.env.NODE_ENV}`)
    });
  }
}
const path = require('path');
const CleanWebpackPlugin = require('clean-webpack-plugin');

module.exports = {
  target: 'node',
  entry: {
    app: './packages/core/index.ts'
  },
  devtool: 'inline-source-map',
  plugins: [
    new CleanWebpackPlugin(['dist']),
  ],
  resolve: {
    extensions: ['.ts', '.tsx', '.js'],
    modules: ['node_modules']
  },
  module: {
    rules: [{
      test: /\.ts|\.tsx$/,
      include: ['packages/**/src'],
      use: [{
        loader: 'awesome-typescript-loader',
        options: {
            configFileName: 'tsconfig.json',
            sourceMap: true
        },
      }],
    }],
  },
};
src/server.ts

import { Server } from './src/server';

new Server()

console.log('test');
import * as express from 'express';

export class Server {

  private app: express.Application

  constructor() {
    this.init();
    this.bootstrap();
  }

  init(): void {
    this.app = express();
    this.app.get('/', (req, res, next) => {
      res.send("Hello world");
      next();
    });
  }

  bootstrap(): void {
    const PORT = 3000;
    this.app.listen(PORT, () => {
      console.log(`Express server listening on port ${PORT}.\nEnvironment: ${process.env.NODE_ENV}`)
    });
  }
}
const path = require('path');
const CleanWebpackPlugin = require('clean-webpack-plugin');

module.exports = {
  target: 'node',
  entry: {
    app: './packages/core/index.ts'
  },
  devtool: 'inline-source-map',
  plugins: [
    new CleanWebpackPlugin(['dist']),
  ],
  resolve: {
    extensions: ['.ts', '.tsx', '.js'],
    modules: ['node_modules']
  },
  module: {
    rules: [{
      test: /\.ts|\.tsx$/,
      include: ['packages/**/src'],
      use: [{
        loader: 'awesome-typescript-loader',
        options: {
            configFileName: 'tsconfig.json',
            sourceMap: true
        },
      }],
    }],
  },
};
我正在尝试使用webpack编译typescript代码,以下是我的配置文件:

webpack.config.js

import { Server } from './src/server';

new Server()

console.log('test');
import * as express from 'express';

export class Server {

  private app: express.Application

  constructor() {
    this.init();
    this.bootstrap();
  }

  init(): void {
    this.app = express();
    this.app.get('/', (req, res, next) => {
      res.send("Hello world");
      next();
    });
  }

  bootstrap(): void {
    const PORT = 3000;
    this.app.listen(PORT, () => {
      console.log(`Express server listening on port ${PORT}.\nEnvironment: ${process.env.NODE_ENV}`)
    });
  }
}
const path = require('path');
const CleanWebpackPlugin = require('clean-webpack-plugin');

module.exports = {
  target: 'node',
  entry: {
    app: './packages/core/index.ts'
  },
  devtool: 'inline-source-map',
  plugins: [
    new CleanWebpackPlugin(['dist']),
  ],
  resolve: {
    extensions: ['.ts', '.tsx', '.js'],
    modules: ['node_modules']
  },
  module: {
    rules: [{
      test: /\.ts|\.tsx$/,
      include: ['packages/**/src'],
      use: [{
        loader: 'awesome-typescript-loader',
        options: {
            configFileName: 'tsconfig.json',
            sourceMap: true
        },
      }],
    }],
  },
};
tsconfig.json

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "declaration": false,
    "emitDecoratorMetadata": true,
    "strictNullChecks": false,
    "noImplicitAny": true,
    "removeComments": true,
    "experimentalDecorators": true,
    "lib": [
      "dom",
      "es2015",
      "esnext.asynciterable"
    ],
    "module": "es2015",
    "moduleResolution": "node",
    "sourceMap": true,
    "target": "es5"
  },
  "include": [
    "packages/**/*.ts"
  ],
  "exclude": [
    "node_modules",
    "./packages/**/node_modules",
    "./packages/**/*.spec.ts",
    "e2e"
  ],
  "compileOnSave": false,
  "atom": {
    "rewriteTsconfig": false
  }
}
但是当运行
webpack
时,我得到以下信息:

➜  app git:(master) ✗ npm run build

> app-core@0.0.1 build /home/dev/app
> webpack --mode development

clean-webpack-plugin: /home/dev/app/dist has been removed.
Hash: a2cdfcfd39d5692648d6
Version: webpack 4.1.0
Time: 80ms
Built at: 2018-3-7 10:50:04
 Asset      Size  Chunks             Chunk Names
app.js  7.83 KiB     app  [emitted]  app
Entrypoint app = app.js
[./packages/core/index.ts] 80 bytes {app} [built]
[./packages/core/src/server.ts] 218 bytes {app} [built] [failed] [1 error]

ERROR in ./packages/core/src/server.ts
Module parse failed: Unexpected token (5:10)
You may need an appropriate loader to handle this file type.
| export class Server {
| 
|   private app: express.Application
| 
|   constructor() {
 @ ./packages/core/index.ts 1:0-38 3:4-10
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! app@0.0.1 build: `webpack --mode development`
npm ERR! Exit status 2
npm ERR! 
npm ERR! Failed at the app-core@0.0.1 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
  • 使用
    路径
    而不是
    /
  • 看起来您构建了一个节点js项目。你不需要tsx。它是网站react文件的扩展
  • awesome typescript loader
    的GitHub看起来不再受支持了。尝试移动到ts加载器或babel加载器
  • 我有一个样本和最小的项目就是这样做的(巴贝尔装载机):

    您还应该使用my.babelrc,这样所有的es6语法也将被传输