繁体   English   中英

在 ASP.NET Core 中捆绑 Typescript

[英]Bundling Typescript in ASP.NET Core

我最近更新了一个 ASP.NET Core/React 项目以使用 TypeScript。 但是我注意到为了让 TypeScript 编译,我需要将导入放在文件的顶部。 当捆绑器合并这些文件时,导入不会被删除,这导致import declarations may only appear at top level of a module错误的import declarations may only appear at top level of a module 另一端出来的 jsx 除了导入之外还可以。 有没有办法让默认打包器处理这些导入?

捆绑配置文件

[
  {
    "outputFileName": "wwwroot/js/vehicles.jsx",
    "inputFiles": 
     [
       "built/Components/General/VehiclePanel.jsx",
       "built/Components/Vehicles/VehiclePage.jsx"
     ],
     "minify": 
     {
       "enabled": false,
       "renameLocals": false
     },
  },
  {
    "outputFileName": "wwwroot/js/editVehicle.jsx",
    "inputFiles": 
     [
       "built/Components/General/Job.jsx",
       "built/Components/General/History.jsx",
       "built/Components/General/EditVehiclePanel.jsx",
       "built/Components/Vehicles/EditVehiclePage.jsx"
     ],
     "minify": 
     {
       "enabled": false,
       "renameLocals": false
     },
  },
  {
    "outputFileName": "wwwroot/js/editService.jsx",
    "inputFiles": 
     [
       "built/Components/General/Job.jsx",
       "built/Components/General/History.jsx",
       "built/Components/General/ServicePanel.jsx",
       "built/Components/Services/ServicePage.jsx"
     ],
     "minify": 
     {
       "enabled": false,
       "renameLocals": false
     },
  },
]

带有导入语句的组件示例

// Name History.tsx
// Author: Redacted
// CreatedDate: 24/02/2020
// ModifiedDate: 29/02/2020
// Description: History panel
import { Component } from 'react';
import { IHistory } from "../../Interfaces/Interfaces";

interface HistoryProps {
    history: IHistory;
}

export class History extends Component<HistoryProps> {

    constructor(props) {
        super(props);
    }

    render() {
        return (
            <div>
                <div className="box">
                    <div className="box-header with-border">
                        <h3 className="box-title">{this.props.history.title}</h3><div className="box-tools pull-right">{this.props.history.createdDateTime}</div>
                    </div>
                    <div className="box-body">
                        {this.props.history.description.split("\n").map(function (history, i) { return <p key={"h" + i}>{history}</p> })}
                    </div>
                </div>
            </div>
        );
    }

}

编辑:我刚刚注意到编译的组件前面仍然有export语句。 这让我觉得这可能与我的打字稿的编译方式有关。 我已经添加了我的 tsconfig.json 以防万一。

配置文件

{
  "compileOnSave": true,
  "compilerOptions": {
    "outDir": "./built",
    "allowJs": true,
    "target": "es2015",
    "jsx": "preserve",
    "lib": [ "es2015", "es2016", "dom" ],
    "moduleResolution": "Node",
    "noUnusedLocals": true,
    "noUnusedParameters": true
  },
  "include": [
    "./Scripts/Components/**/*"
  ],
  "exclude": [
    "./Scripts/Interfaces/*"
  ]
}

第二次编辑:在 tsconfig.json 中将"module": "none"compilerOptions后,导入和导出语句被删除。 不幸的是,这消除了我之前所做的更改的影响。 我在compilerOptions设置了"moduleResolution": "Node"来解决这个被添加到所有文件顶部的问题。

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

我解决了将其添加到每个文件顶部的问题。

Object.defineProperty(exports, "__esModule", { value: true });

我不得不将导出移动到文件的顶部。

我从这里改变了它

import * as React from 'react';
import { IVehicle } from "../../Interfaces/Interfaces";

export class VehiclePanel extends React.Component<IVehicle> {

    constructor(props) {
        super(props);
    }

对此

export = VehiclePanel;
import * as React from 'react';
import { IVehicle } from "../../Interfaces/Interfaces";

class VehiclePanel extends React.Component<IVehicle> {

    constructor(props) {
        super(props);
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM