Skip to content
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

TypeScript and <script type="module"></script> #13422

Closed
cyrilletuzi opened this issue Jan 11, 2017 · 71 comments
Closed

TypeScript and <script type="module"></script> #13422

cyrilletuzi opened this issue Jan 11, 2017 · 71 comments
Labels
Domain: ES Modules The issue relates to import/export style module behavior Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript

Comments

@cyrilletuzi
Copy link

cyrilletuzi commented Jan 11, 2017

A first implementation of <script type="module"><script> just landed in Safari Technology Preview 21, so I tried to use it on a TypeScript / Angular project, to finally get rid of system.js.

First issue, resolved but unconvenient : ES6 modules paths must include the '.js' extension. Fortunately, TS 2.0+ knows how to resolve import { Something } from './test.js' into import { Something } from './test.ts' while in dev. But it's a huge change to common practices and a lot of refactoring : official TypeScript docs, Angular, RxJS and so on have all encouraged until know to use the no extension form (import { Something } from './test').

Second issue, unresolved : TS provides many options to tell the compiler how to resolve paths like import { Component } from '@angular/core'. But it never transforms the imported path in the compiled files. '@angular/core' will stay '@angular/core' in the transpiled files. After investigation, I've read in many issues it is by design.

It's OK with loaders like system.js and others, which provide similar path mapping options. But with <script type="module"><script>, as far as I know, there is no configuration, so import { Component } from '@angular/core' will always fail.

I am missing something, or does that really mean that we won't be able to use the native loader with TypeScript projects ?

If I'm right, is it possible to add an option to force path transformation in compiled files ?

@aluanhaddad
Copy link
Contributor

aluanhaddad commented Jan 14, 2017

Just a comment. It seems ironic that you wish to

to finally get rid of system.js

When it is about the only option comes that close to what you seem to be trying to achieve.

But it's a huge change to common practices and a lot of refactoring : official TypeScript docs, Angular, RxJS and so on have all encouraged until know to use the no extension form (import { Something } from './test').

Personally, I think extensionless imports are far better, they make your code far more portable.

I suspect the native loader will eventually support configuration. Of course the specification is in major flux, but there is likely to be a configurable API to perform abstracted name resolution for imports like

import {Component} from '@angular/core';

because it is clearly needed and because the issue has been discussed at least to some extent. I've heard discussion of how to resolve

import $ from 'jquery';

You may find this repository interesting https://github.com/whatwg/loader/

If I'm right, is it possible to add an option to force path transformation in compiled files ?

No this is not possible at present.

@cyrilletuzi
Copy link
Author

I don't see what's ironic in wanting to depend on native and standard features, instead of librairies...

@aluanhaddad
Copy link
Contributor

aluanhaddad commented Jan 14, 2017

Sorry I am a dyed in the wool SystemJS user (actually I'm not sorry 😜). But the whole point of SystemJS is to get out of the way, when native loaders are available, but until then polyfill the loader specification (and provide other features such as CommonJS interop).
In other words it has built in obsolescence, by design, so it does not interfere with the future it seeks to enable today.

Seriously SystemJS is great. Not every framework uses it well, for example Angular 2, made poor use of it and is now removing it. Look at Aurelia as an example framework that, while not requiring SystemJS, uses it by default and plays to its strengths showing how elegant it can be.

@cyrilletuzi
Copy link
Author

It's not a charge against SystemJS. Yes, it is a nice tool. But as you said it very well, it was designed as a temporary tool until the native loader is finally here.

So now the central part of it is there (<script type="module"></script>), but unusable in a real case : I can't see the point if we can just load a few local scripts, and no librairies. So I'm confused.

Hopefully configuration is coming. Or hopefully TypeScript will manage it.

@aluanhaddad
Copy link
Contributor

@Avol-V
Copy link

Avol-V commented Jan 19, 2017

Personally, I think extensionless imports are far better, they make your code far more portable.

SystemJS didn't recommend to use default extension as well.

@mhegazy
Copy link
Contributor

mhegazy commented Jan 26, 2017

i am not sure I see why this is a TS issue. If you put the full file path in the module, it should work. Or putting it diffrentelly, how could you do the same with a plain .js file with the typescript compiler completely out of the picture?

@Avol-V
Copy link

Avol-V commented Jan 26, 2017

Whether TypeScript transforms paths or not, it could be an option to add extension to the names of modules (or it could convert .ts to .js).

@mhegazy
Copy link
Contributor

mhegazy commented Jan 26, 2017

import .. from "./foo.ts" is not allowed so it is either "./foo.js" or just "./foo". I would recommend using consistent extension and always specify .js.

@cyrilletuzi
Copy link
Author

i am not sure I see why this is a TS issue. If you put the full file path in the module, it should work

As far as I know, TypeScript doesn't support absolute paths. So no, it doesn't work.

After a deeper thinking, I think the current behavior is really not normal. As a transpiler, TS should produce standard and ready-to-work JavaScript. TS also choose the very good design option to be "just" enhanced JS, ie. backward compatible with normal JS.

import { Something } from './something' is clearly not standard JS.

@mhegazy
Copy link
Contributor

mhegazy commented Jan 27, 2017

As far as I know, TypeScript doesn't support absolute paths. So no, it doesn't work.

what do you mean absolute path? can you share an example of an app with a module that is absolute? a CDN path should work with a path mapping entry.

import { Something } from './something' is clearly not standard JS.

We do not know what node will do yet. but if node considers this invalid filename, the compiler can flag it as an error. either ways, you should just write:

import { Something } from './something.js'

@cyrilletuzi
Copy link
Author

cyrilletuzi commented Jan 27, 2017

We do not know what node will do yet. but if node considers this invalid filename

It's already an invalid filename for browsers, which is the main use case of TypeScript.

you should just write: import { Something } from './something.js'

I agree, that's what I do now to be future proof. Problem is, like I said on first message, that it's a huge change to common practices and a lot of refactoring : official TypeScript docs, Angular, RxJS and so on have all encouraged until know to use the no extension form (import { Something } from './test').

For absolute paths, I mean a code like this :

import { Component } from '/node_modules/@angular/core/index.js';

Otherwise, I need to do things like this :

import { Component } from '../../../node_modules/@angular/core/index.js';

First, it's a mess, second, it doesn't work if outDir is not at the same path level.

@cyrilletuzi
Copy link
Author

you should just write: import { Something } from './something.js'

And most importantly, I can refactor my code, but I can't refactor librairies I use...

@mhegazy
Copy link
Contributor

mhegazy commented Jan 27, 2017

It's already an invalid filename for browsers, which is the main use case of TypeScript.

No it is not if you are using requireJS, Browserify, Webpack, SystemJs, etc.. i.e. any thing other than a browser that supports native ES6 modules, and as i far as i know, there are not many of these around today.

@mhegazy
Copy link
Contributor

mhegazy commented Jan 27, 2017

For absolute paths, I mean a code like this :

The loader spec does not really talk about how these absolute paths are allowed. When we have a clear description of that, allowing such should not be hard.

@cyrilletuzi
Copy link
Author

there are not many of these around today

Implementations have started, it is now in Safari Technology Preview, in Edge Preview and it's coming soon in Chrome Canary. That was the starting point of this issue. TS is supposed to produce standard JS, not JS that need specific tools. Maybe it's too soon to decide, but for now common usage of TypeScript is incompatible with native JS.

@cyrilletuzi
Copy link
Author

cyrilletuzi commented Jan 28, 2017

I don't understand why it's so difficult for this issue to be acknowledged.

TypeScript is supposed to be JS compatible, and it is also a transpiler. The point of a transpiler is to produce native JS. And it's not right anymore with <script type="module"></script>, so it's a serious issue.

OK, maybe there are still some points to be clarified (loader spec and Node choices are not final yet), but if navigators have started implementations (even Safari ^^), it's because they think the spec is now stable enough.

So now that it's possible in some beta versions to use <script type="module"></script>, it's impossible to make it work with a TypeScript project, like an Angular app, because :

  • absolute paths are required (I can use them in my code, but then dev tools like Intellisense don't work anymore as TS doesn't support them) ;
  • .js extension are required (I can use them in my code, but I can't rewrite Angular files where there is no .js extension anywhere, like in every current TypeScript projects as it was not the common usage, so all imports inside Angular fail).

And even if native loaders just started to appear, ES6 import is here for some time now, and it was always clear that the .js extension is required. Maybe I've missed something, but I've seen no sign that it could become optional.

That's precisely why tools like Systemjs offer a defaultJSExtension option from the beginning : because the .js extension has always been required. In the last Systemjs version (0.20) which aligns to the last loader spec, the global defaultJSExtension option has even been removed. Now you can just use it by package, meaning that you shouldn't rely on it, but it's just here to support compatibility with some packages that don't follow the standard, like TypeScript projects.

@mhegazy
Copy link
Contributor

mhegazy commented Feb 1, 2017

absolute paths are required (I can use them in my code, but then dev tools like Intellisense don't work anymore as TS doesn't support them) ;

this we can solve. and probably should. possibly with a flag to consider the / as baseUrl

.js extension are required (I can use them in my code, but I can't rewrite Angular files where there is no .js extension anywhere, like in every current TypeScript projects as it was not the common usage, so all imports inside Angular fail).

I would say this is an issue with the Angular code base, and not TS. there are going to be dependencies that do not use full URI, the TS compiler can not go touch all your node_modules to make them ES6 ready.

@Avol-V
Copy link

Avol-V commented Feb 2, 2017

I would say this is an issue with the Angular code base, and not TS.

But you should fix TypeScript documentation — currently there is no .js extension in examples of using modules.

And, for strict code convention, it can be an option of TypeScript to require .js extension in modules.

@cyrilletuzi
Copy link
Author

I would say this is an issue with the Angular code base, and not TS.

It's not an Angular issue. It's a possibility, and the current common usage in all TypeScript projects, not just Angular. It's even the indicated way in official TypeScript documentation. And as valid TypeScript which is here to stay for compatibility reasons (next point), it should be managed and transpiled to valid JavaScript.

Of course TypeScript won't rewrite existing projects and files in my node_modules. But when I transpile my project, all my final transpiled files (my own and the ones imported from librairies) must be ES6 compliant.

And for current librairies to become ES6 compliant and ES6 ready-to-use, TypeScript must start to transpile to valid JS, which is not currently possible.

it can be an option of TypeScript to require .js extension in modules.

It won't happen as it would break compatibility. '.js' extension works only since TypeScript 2. As we can't revert things now, transpilation to valid JS must be managed.

@Avol-V
Copy link

Avol-V commented Feb 3, 2017

It won't happen as it would break compatibility. '.js' extension works only since TypeScript 2. As we can't revert things now, transpilation to valid JS must be managed.

It's true that we have a problem with compatibility and I think it can be an option to choose backward-compatible mode (with adding .js) and strict mode (with requiring .js).

Why we need that strict mode? The main idea of TypeScript is to be more strict than JavaScript, but currently in modules JavaScript is more strict than TypeScript. TypeScript should be as strict as JavaScript but with additional restrictions.

@cyrilletuzi
Copy link
Author

Now in Firefox Nightly https://twitter.com/evilpies/status/829604616216125442

Meaning all major browsers are now implementing this. I think it's time for TypeScript to be ready.

@cyrilletuzi
Copy link
Author

cyrilletuzi commented Feb 10, 2017

Another point : current TypeScript loaders (ts-loader, awesome-ts-loader and @ngtools/webpack) for webpack only work with extension-less imports. So if I need webpack for production, I won't be able to use <script type="module"> in development.

@mjackson
Copy link

mjackson commented Apr 19, 2017

@cyrilletuzi Where did you read that support for <script type="module"> is coming to Chrome Canary?

Edit: Nevermind, found it https://www.chromestatus.com/feature/5365692190687232

@aluanhaddad
Copy link
Contributor

aluanhaddad commented Apr 19, 2017

@Avol-V

@aluanhaddad wrote:
Personally, I think extensionless imports are far better, they make your code far more portable.

SystemJS didn't recommend to use default extension as well.

That's true, but only for JavaScript code. SystemJS TypeScript transpilation doesn't work with a .js extension because there's no file on disk with that name.

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label May 24, 2017
@quantuminformation
Copy link

Related to #16577

@ghost
Copy link

ghost commented Apr 3, 2021

Just ran into this issue and it surprises me that it hasn't been fixed. Workarounds are called workarounds for a reason - even if they work, they have their own problems.

@nuts-n-bits
Copy link

nuts-n-bits commented Apr 3, 2021

Just ran into this issue and it surprises me that it hasn't been fixed. Workarounds are called workarounds for a reason - even if they work, they have their own problems.

Unfortunately I've found out that this bug is now considered "working as intended" and affirmatively closed by @RyanCavanaugh in a recent issue in January 2021. According to him there won't be a fix even behind a flag, so.

I'm not picking on Ryan though as I imagine this is not his decision alone, but so far I haven't seen a satisfying rationale, across 3 issues and hundreds of comments, as to why this fix is not worth doing. From Ryan it seems that complexity is a problem, and that you should write import specifiers how they're supposed to be during runtime. But I think his reasons pale in front of this argument. I would very much appreciate a response to the linked comment from the TS team.

@tjcrowder
Copy link

Please consider reopening this and implementing it.

@antongolub
Copy link

antongolub commented May 11, 2021

It seems that proper way fix will not be available soon, so I wrote a small js-script as an alternative to sed-based patcher mentioned above.
To save someone else's time:

@orta
Copy link
Contributor

orta commented May 11, 2021

Given how the ecosystem has changed since 2017. To try summarize the TS position here in mid 2021:

  • TypeScript won't be changing your import specifiers for you (e.g. import {x} from "./x/y/z.ts" to import {x} from "./x/y/z.js" ) it breaks one of the founding guidelines of only erasing types (and TS would not be where it is today if it broke its own design rules) - you can write this in ESM code today and it will work. I do it.

  • An import like import { Component } from '@angular/core' only works today with node-style resolution strategies, but the import map spec for JS will solve that problem, and once it's settled and standardized TypeScript will support that too.

Re: original point about resolving TS in modules in html files- I've been doing some work on that in microsoft/vscode#121517

@cdalexndr
Copy link

cdalexndr commented Jul 14, 2021

Instead of waiting X years for caniuse import maps to be implemented by all browsers, typescript could implement this itself.

For example, typescript already has something similar for lookup (paths), but it doesn't transform output js import paths:

    "paths": {
      "jquery": ["node_modules/jquery/dist/jquery"]
    }

Adding an import map feature, for example:

    "importMap": {
      "jquery": ["/node_modules/jquery/dist/jquery.js"]
    }

would allow to transform import $ from "jquery" to import $ from "/node_modules/jquery/dist/jquery.js".

@NigeNigeNige
Copy link

  • TypeScript won't be changing your import specifiers for you (e.g. import {x} from "./x/y/z.ts" to import {x} from "./x/y/z.js" ) it breaks one of the founding guidelines of only erasing types (and TS would not be where it is today if it broke its own design rules) - you can write this in ESM code today and it will work. I do it.

The TypeScript compiler already rewrites import statements depending on the value of config.compilerOptions.module. The issue here is that when omitting a file extension from an import statement in a source file (as has been standard practice in front-end development for a long time and is prevalent through many third-party packages in that domain), the ES2015, ES6, ES2020 and ESNext settings cause the compiler to produce code that does not work on these target environments, despite tsc stating that there are no errors.

The problem isn't that our TypeScript code is wrong - it passes all compile and linting steps perfectly - it's that the compiler is doing the wrong thing when converting to JavaScript for these target module systems.

@srcspider
Copy link

srcspider commented Nov 18, 2021

With regard to this issue, not only does Typescript use non-extension imports in it's own documentation on modules https://www.typescriptlang.org/docs/handbook/module-resolution.html the VS Code editor, which is also made by microsoft needs special settings to even work https://2ality.com/2021/06/typescript-esm-nodejs.html#visual-studio-code

I can understand sticking to a design principle. But I have to ask, WHO is this for? Who is actually benefiting from that design principle? Since it feels like, it's literally for nobody. Especially when nobody is asking to change default behavior, just a switch to append ".js" or ".mjs" when outputting (out of which there are crazier output settings already in the language). To be honest just the confusion aspect of this issue is reason enough to add a simple output


@ anyone stumbling here...

If you're trying to get your node program to work, just replace whenever you would write node with node --es-module-specifier-resolution=node (node v16.13.0). Think of it as the right of passage for the language; can't be mature language with out pointless mandatory flags (it's just like how you see everyone write perl -w, welcome to the club)

If you need it as a shell header, don't forget the -S this is the correct version:
#!/usr/bin/env -S node --es-module-specifier-resolution=node

@AjayChambers
Copy link

After tediously reading 4+ years worth of comments on this thread I must say SrcSpider ended the thread on a good note for me. The thing that confuses is me, is I don't understand why no one wants to fix this issue. I don't contribute to TS, so I don't know the project all that well, but usually path resolution issues (from what I have seen) are solvable issues. At the worst case a flag could be added to change TS behavior for ES6 Module path resolution right? IDK, maybe I am out of my league, but the way it is as it stands to be extremely annoying. So annoying it makes me wonder if I am just wasting my time working to make sure I write everything in TS. The whole point of compiling to JavaScript (transpiling) is backwards compatibility, portability, and valid code (or valid ES6 JS in this case), right? Its 2022, and no one working on the project seems to even care.

@quantuminformation
Copy link

@W3Dojo don't miss out part 2

#16577

12 hrs of reading

@jogibear9988
Copy link

I've created a pull request wich wich solve this for raltive imports, see:

#47436

I've added a compiler switch:

   appendModuleExtension

@one-github
Copy link

I've created a pull request wich wich solve this for raltive imports, see:

#47436

I've added a compiler switch:

   appendModuleExtension

They said it wasn't possible. Along came some guy who didn't know this and just did it. 🥳

@mitsukuri
Copy link

mitsukuri commented Mar 22, 2022

According to my trusted sources, the rationale behind this stubborn refusal to actually make tsc emit javascript imports with, well, .js extension is as follows:

If implemented, Typescript newcomers will cease writhing in pangs, thus disrupting the steady supply of bad vibes that keep Ctulhu asleep, and it will make Him emerge from R'lyeh, scramble the letters in all occurrences of Microsoft in existence into Coots Firm and scuttle back into the Ancient City growling unspeakable curses; And the management clearly doesn't want that!

Let us all support the stoicism of those behind this unpopular decision and applaud their efforts to redeem the world!

@paul-uz
Copy link

paul-uz commented Aug 12, 2022

I've created a pull request wich wich solve this for raltive imports, see:

#47436

I've added a compiler switch:

   appendModuleExtension

How exactly do I use this? I can't find anything about this option in the documentation :/

@jogibear9988
Copy link

it was not accepted

@pylgrym
Copy link

pylgrym commented Nov 14, 2023

I can understand sticking to a design principle. But I have to ask, WHO is this for? Who is actually benefiting from that design principle?

I can answer this for you :-). Because I am in the same boat as you, but I actually don't want this solution..
The problem with this proposed solution, is that having it available would cause a whole new host of different problems, even larger than the problem you/we are currently having.

The principle that typescript/tsc will preserve import paths, and never try to magically adjust them,
is necessary for interoperability of released library code.
Because import paths are never adjusted, you have a guarantee that the shape of the present import paths does not depend on specific build settings.
IF typescript would adjust the paths differently based on settings, you would suddenly

  • need to know WHICH variant of settings they were based on.
  • even worse, maybe need multiple variants of the present files, a separate subset for each possible combination of settings.

Also, one of the invariant rules of typescript is, that if you feed it a plain javascript file, that file will be re-output unmolested/without needing any adjustments. If you had this import-path adjusting possibility, it would need to be able to adjust import paths in a plain javascript file, violating the guarantee that it won't adjust plain-javascript.

I/we are already living variants of this nightmare.
I am currently trying to use an npm package that contains javascript files that use weird import syntax.
Because of this, I cannot use that npm package with
browser-ESM imports, because the foreign-import-syntax in that npm package is not legal browser-ESM.
Neither can I use that npm package directly in nodeJS, because the foreign-import-syntax is not legal nodeJS either.
Instead, I am forced to go on a detective-mystery-hunt, to guess-figure-out which magic combination of bundler tools the authors of that npm package are using, to allow their fancy non-standard import syntax to work.

This is the hell these 'defenders of typescript' are fighting against/protecting you from :-) :-(.

I will now continue on my odyssey to unravel, how their (my 'enemies'.., not the ts-people) non-standard import-syntax can be made to work when using ESBUILD :-/.

@trusktr
Copy link
Contributor

trusktr commented Nov 14, 2023

Random addition: one solution to this for browser native ESM is to add a service worker to your app, and append the .js extensions in the service worker. I've had good success with that (although I much prefer just adding .js in the import statements and simply moving along while keeping complexity lower).

I also use service workers for things like importing `.css` or other types of files.

Basically service workers let you implement a module loader.

What you do is you intercept the URL (f.e. for a URL ending in .css) and you have the service worker make the response be JavaScript code that can actually fetch the original file and do anything with it (f.e. the returned JS fetches the .css file and instantiates a style sheet with it and appends it to the document.head, etc). And because ES Modules are async (they can even have top-level await), fetching resources as part of a module works just fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Domain: ES Modules The issue relates to import/export style module behavior Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests