Skip to content

Treat new URL('relativePathHere', import.meta.url) as assets. #45955

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

Closed
4 of 5 tasks
Jack-Works opened this issue Sep 19, 2021 · 8 comments
Closed
4 of 5 tasks

Treat new URL('relativePathHere', import.meta.url) as assets. #45955

Jack-Works opened this issue Sep 19, 2021 · 8 comments
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript

Comments

@Jack-Works
Copy link
Contributor

Suggestion

When using a file in TypeScript, new URL("./file.png", import.meta.url), TypeScript should copy this file to the output folder, like what it did for importing JSON files.

🔍 Search Terms

new URL import meta url

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

📃 Motivating Example

This is useful when writing a library with assets referenced otherwise we need extra steps to copy those files.

@nmain
Copy link

nmain commented Sep 20, 2021

https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals

Non-goals
Provide an end-to-end build pipeline. Instead, make the system extensible so that external tools can use the compiler for more complex build workflows.

@Jack-Works
Copy link
Contributor Author

https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals

Non-goals
Provide an end-to-end build pipeline. Instead, make the system extensible so that external tools can use the compiler for more complex build workflows.

But why it can copy JSON files with resolveJsonModule? And I belive they'll also do this for wasm files (#39784)

@andrewbranch
Copy link
Member

How are we supposed to know that URL is special? It’s just a constructor defined in a lib file.

@Jack-Works
Copy link
Contributor Author

How are we supposed to know that URL is special? It’s just a constructor defined in a lib file.

It's hard coded in all of the toolchains like how they treat require.

@andrewbranch andrewbranch added Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript labels Sep 24, 2021
@andrewbranch
Copy link
Member

Therein lies the answer to

But why it can copy JSON files with resolveJsonModule?

@Jack-Works
Copy link
Contributor Author

Therein lies the answer to

But why it can copy JSON files with resolveJsonModule?

Because in resolveJsonModule, TypeScript actually parse and generate type information for JSON module right

@andrewbranch
Copy link
Member

Yes, and more generally, we have to process imports and exports in order for modules to work, and there is unambiguous syntax, not dependent on type definitions, that triggers an import.

@Jack-Works
Copy link
Contributor Author

What if we have this import reflection proposal in the ECMAScript? Can TypeScript copy the asset for the following code? (tc39/proposal-source-phase-imports#16 (comment))

import u from './x.png' as 'asset-reference'
await readFile(u)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants