TypeScript generating wrong JS in Release Mode

2019-09-06 08:28发布

问题:

I do not know if this is a known issue. I am working with VS 2012 Web Express with Typescript 0.8.1.1. I am using the --module AMD clause to generate AMD modules.

Iin Debug mode the compiler generates a define clause like this:

define(["require", "exports", "app/Config", "app/ModelLocator", "app/Presenter", "app/Messenger", "app/LogOnWindow", "app/vm/VmAppHeader", "app/framework/PageLocator", "app/framework/ViewStacks"], function(require, exports, __cfg__, __ml__, __pr__, __ms__, __rc__, __lw__, __ah__, __pl__, __vs__) ...

When I compile in Release mode the generated code is this:

var cfg = require("./app/Config")
var ml = require("./app/ModelLocator")
var pr = require("./app/Presenter")
var ms = require("./app/Messenger")
var rc = require("./app/RouteConfig")
var lw = require("./app/LogOnWindow")
var ah = require("./app/vm/VmAppHeader")
var pl = require("./app/framework/PageLocator")
var vs = require("./app/framework/ViewStacks")

That is in Release mode it is generating code for CommonJS modules and not AMD modules.

Is there a way to make it work ???

Thanks in advance

回答1:

I'm guessing you did this:

  <PropertyGroup Condition="'$(Configuration)' == 'Debug'">
    <TypeScriptSourceMap> --sourcemap --module amd</TypeScriptSourceMap>
  </PropertyGroup>

Note that this block only gets included in the Debug configuration. What you want to do instead is move the --module amd thing lower in the file where the compiler is invoked.

  <Target Name="BeforeBuild">
    <Message Text="Compiling TypeScript files" />
    <Message Text="Executing tsc$(TypeScriptSourceMap) @(TypeScriptCompile ->'&quot;%(fullpath)&quot;', ' ')" />
    <Exec Command="tsc$(TypeScriptSourceMap) --module amd @(TypeScriptCompile ->'&quot;%(fullpath)&quot;', ' ')" />
  </Target>