This comes out of my answer at https://stackoverflow.com/a/48436147/1019307.
I need to change the module kind that the transpiler VS uses to be AMD
. It defaults to commonjs
even when the <TypescriptModuleKind>
is changed in the project.csproj
's from commonjs
to be AMD
.
To see the problem:
project.csproj
contains <TypescriptModuleKind>commonjs</TypescriptModuleKind>
and leave it like thatOpen the app.js
file and observe that it starts with:
"use strict";
var Greeter = /** @class */ (function () {
function Greeter(element) {
Open app.ts
and prefix with export
to make it export class Greeter {
exports is undefined
in the developer consoleOpen the app.js
file and observe it now says the following:
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Greeter = /** @class */ (function () {
Create a tsconfig.json
by running tsc --init
"module": "commonjs"
tsc
to transpile the ts filesapp.js
file is still as above (and would cause the exports is not defined
error)tsconfig.json
file so that it contains "module": "amd"
tsc
to transpile the ts filesOpen the app.js
file and observe it now says the following:
define(["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Greeter = /** @class */ (function () {
Follow https://stackoverflow.com/a/48436147/1019307 to setup to use requirejs
:-)
tsconfig.json
out of the way by renaming eg to xxxxtsconfigxx.xxjson
app.ts
file to make it dirty; restart the serverexports is not defined
error has returnedtsconfig.json
and repeat the last step and observe the error still exists. This confirms that VS does not use the local tsconfig.json
for configuration.project.csproj
to contain <TypescriptModuleKind>amd</TypescriptModuleKind>
app.ts
file to make it dirty; restart the serverexports is not defined
error has returnedThus proving that VS does not honor the tsconfig.json
file and seems to ignore its TypescriptModuleKind
configuration value.
What are the TypeScript project build configuration options? seems to say how VS uses tsc. I ran using the --module
switch and it worked correctly (eg. tsc --module commonjs [app.ts]
and tsc --module amd [app.ts]
). With [app.ts]
being required if the tsconfig.json
file doesn't exist. Thus it seems that VS is ignoring the TypescriptModuleKind
setting or not applying it in the way that it should.
So what's the deal here. Is there any way to make it either use the tsconfig.json
file or honor its TypescriptModuleKind
setting?
I found the answer.
In the Project's Properties there is a Typescript Build
(side) tab and there is a dropdown labeled Module System
which I set to 'AMD'. And this worked! Awesome.
In a comment @Chris Halcrow suggested that if you take out the Typescript configurations from the project file then VS may use the configuration from the tsconfig.json
. I didn't try it but definitely worth a try for the case where you need more options than what VS supplies.
I just found https://developercommunity.visualstudio.com/content/problem/24584/cant-compile-the-ts-files-on-vs2017-node-modules-n.html, which has some useful information on how VS deals with typescript.
I also realised that @ChrisHalcrow was correct when he said "if there's a tsconfig.json inside a project folder then the VS TS Config is ignored". I hadn't forced VS to recognise the tsconfig.json
file. Once I added it again, it was seen. I forgot that VS needs to have things 'add'ed (source files, config files, ..). I'm used to Eclipe and IDEA seeing the changes and asking or just doing.
Even more awesome!