-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tsc emits __esModule
even for ModuleKind.None
#14351
tsc emits __esModule
even for ModuleKind.None
#14351
Comments
TranspileModule treats the input as a module regardless of the module option; hence the name of the function. |
|
@mhegazy Is there any way to not compile as modules using compiler API? @HerringtonDarkholme |
I have similar problem. Today I made an attempt to update our typescript version from 2.1.4 to 2.2.1. We have a stack with electron/react/redux/typescript. This forced me to roll back to previous version, where I do not have this additional line after compilation. |
I have this problem too on Electron app with typescript 2.2 .
It looks too hacky... TypeScript compiler inserts
|
Just for clarification, the code resulting from traspileModule is not a module? you do not use WebPack/browserify? nor do you wrap the result in AMD/UMD functions? so how did you handle imports in the past? |
My tsconfig may be wrong. Below is mine: {
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"removeComments": true,
"preserveConstEnums": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noEmitOnError": true,
"strictNullChecks": true,
"target": "es2015",
"sourceMap": true
},
"include": [
"main/**/*.ts",
"renderer/**/*.ts",
"webview/**/*.ts",
"typings/**/*.d.ts"
]
} from here. I'm not using browserify, webpack and AMD/UMD functions. Just compiled with |
If you are on node, then |
yeah, |
I found that I need to use |
I have a similar issue with PhantomJS when running JS transpired from TS. There should be an option to totally disable generation of this line: Object.defineProperty(exports, "__esModule", { value: true }); |
Workaround is to add for example |
This is a bug. We create a tiny bootstrap .ts file to be included inside a page itself, such a generated .js file should not rely on any modules. Yet, we're stuck with 2.1 until this is fixed. |
Do your files have any |
Whether or not you call this a bug it is still a breaking change in a minor version. We have to pin back at version 2.1.x until we figure out what to do. |
The behavior was there for modules with I would like to get some clarity on the issue and why was it working before, and breaking now. |
Apologies, I missed important context. This ticket is explicitly about |
The compiler did this before, but only for c:\test>type a.ts
export default 0;
c:\test>tsc --v
Version 1.8.10
c:\test>tsc a.ts --m commonjs --t es5
c:\test>type a.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = 0; We sure can add this to the breaking change documentation for the TS 2.2 release. |
I had no idea that that was a thing. Yeah, it should be listed in the docs as a breaking change. |
Okay in reading this I found out that we do exports. Removing this now gets rid of this generated code. Some documentation around this would be helpful. |
I just noticed that passing
Despite the error diagnostic, a CommonJS module (which is not what I wanted) is emitted anyway. |
Is the |
Not sure i see why this is relevant. the error is accurate. |
You're right, of course, Passing anything other than |
Sure. feel free to send a PR with your proposed error message enhancements. |
So in my case, the library I am using (npm: type-check) provides its own require function when used inside the browser,
It does that to enable importing the library via require. Thereby typescript is also happily finding the @types declaration file in node_modules. In index.ts one can now...
With
So to make this work, one has to add the exports object:
PS: Just read that it will be fixed in 2.3. Maybe this workaround helps someone. |
@hansrwindhoff Your issue was fixed in #14493 I believe; I assume the fix will be in the next TS release. |
Hi Bruce, thanks for the note, I read just now read that it will be
fixed in 2.3. Thanks again
…On 4/13/17, Bruce Pascoe ***@***.***> wrote:
@hansrwindhoff Your issue was fixed in #14493 I believe; I assume the fix
will be in the next TS release.
--
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub:
#14351 (comment)
|
I've upgraded my project with AMD/ES3 targets from TS 2.1.6 onto 2.3.2 and now TS starts emiting Resulting files are AMD: define(["require", "exports", ... ], function (require, exports, ... ) {
"use strict";
exports.__esModule = true;
}); I can't say the new behavior breaks anything, but I can't understand why this field My tsconfig.json: {
"compileOnSave": true,
"compilerOptions": {
"module": "amd",
"target": "es3",
"sourceMap": true,
"noEmitHelpers": true,
"experimentalDecorators": true,
"baseUrl": ".", // This must be specified if "paths" is.
"paths": {
// map runtime paths to compile-time paths
"lib/*": [ "src/lib/*" ],
"modules/*": [ "src/modules/*" ],
"vendor/*": [ "src/vendor/*" ]
},
"typeRoots" : ["src/typings"]
},
"include": [
"src/**/*.ts",
"src/**/.*.ts", // TS ignores file names starting with dot by default
"tests/**/*.ts",
"tests/**/.*.ts"
]
} // cc @mhegazy |
I've noticed that this |
@zaggino The |
@aluanhaddad But in one of my browser use-cases (quite edge one), this is a breaking change, could this be changed to:
otherwise this breaks in browsers with
|
But it was never valid in the first place. If you are not writing a module then you can't use module syntax and if you are writing a module then you need a loader or a packager. Type declarations are available globally if the module that they describe can be loaded globally. If not the type declaration needs to be revised. |
@aluanhaddad I'm not writing a module but I'm using module syntax to reference my interfaces which are defined in another file. So the resulting file doesn't have any imports itself although the source has, as all interface imports get removed from the result. It'd be great if Or is there another way of using interfaces defined in a different file that I could use for this? |
Yes, there are several options. You can not export them, effectively making them global. Then they will be available in the global type namespace. // global
interface Named {
name: string;
}
interface Identity {
id: number;
} You can place them in a namespace, export them from that namespaces, but not export the namespace itself. // global
declare namespace interfaces {
export interface Named {
name: string;
}
export interface Identity {
id: number;
}
} You can also follow the UMD pattern which is used by most @types packages to export the namespace for use by modules and make it available globally at the same time for non module consumers. // UMD globally available from scripts but requiring import from modules
export = interfaces;
export as namespace interfaces;
declare namespace interfaces {
export interface Named {
name: string;
}
export interface Identity {
id: number;
}
} |
Are we not supposed to use import in the client side? I've tried everything I've found and nothing works, I just get different problems. I'm using Typescript 2.4.1 with Webstorm. I've experimented with different config files and got different results like:
All of these failed to work. |
@goktugyil using Since you are using TypeScript, you are already transpiling your code in order to execute it on JavaScript runtimes. However, that step is necessary but not sufficient. In addition to using a transpiler, you also need to incorporate a module tool, such as a loader or a bundler, into your application. Then you can configure TypeScript to transpile your code into a format recognized by that tool or, in some cases, use a plugin that integrates TypeScript into that tool. Some options include RequireJS, Browserify, SystemJS/JSPM, Webpack, and Rollup. |
I think |
TypeScript Version: 2.3.0-dev.20170227
Code
Expected behavior:
Actual behavior:
I believe it's caused by #13709 that fixed #13709, but
ModuleKind.None
also uses CommonJS emit routine (module.ts#L14) so it's also affected.The text was updated successfully, but these errors were encountered: