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

Raw import of .ts files as text with build-angular:application #26575

Open
santam85 opened this issue Dec 4, 2023 · 4 comments
Open

Raw import of .ts files as text with build-angular:application #26575

santam85 opened this issue Dec 4, 2023 · 4 comments
Labels
angular/build:application area: @angular-devkit/build-angular feature: under consideration Feature request for which voting has completed and the request is now under consideration feature Issue that requests a new feature state: blocked on upstream

Comments

@santam85
Copy link

santam85 commented Dec 4, 2023

Command

build, serve

Description

A similar issue (#26368) has been opened and resolved already with the loader config options for custom extensions/types.

Unfortunately the implemented solution still lacks the ability to perform raw imports of .ts files. This is vital for some use-cases where documenting the examples shown in an application (IE: technical docs for a library) is achieved by including the plain source files themselves as part of the text content rendered in the app.

Describe the solution you'd like

The loader prefix used in webpack to force a particular behaviour was quite a neat solution. Anything done when defining the import file path (ie: import textContent from 'raw:./file.ts') is preferable over a generic project metadata config, imho.

Describe alternatives you've considered

The only possible solution at the moment seems to maintain a copy of the source file with a .txt extension.

@clydin
Copy link
Member

clydin commented Dec 4, 2023

A prime option for this use case would be to use import attributes which provides a standards based solution to the problem.
An example for this particular case would be:

import textContent from './file' with { loader: 'text' };

This could be implemented within the bundling system today. However, TypeScript does not yet provide a mechanism to create type definitions for import attributes. This is currently being discussed (microsoft/TypeScript#56359) and hopefully it will be available in TypeScript in an upcoming version. However, the implementation aspects could be implemented without the type support but an application would need to use ts-ignore or similar for each import which is less than ideal.

@clydin clydin added feature Issue that requests a new feature feature: under consideration Feature request for which voting has completed and the request is now under consideration labels Dec 4, 2023
@mb21
Copy link

mb21 commented Feb 22, 2024

An alternative syntax that vite is using would be:

import textContent from './file?raw'

@mb21
Copy link

mb21 commented Feb 22, 2024

@clydin that TypeScript issue you linked to talks about with { type: "css" } which would import some CSS and assign it type CSSStyleSheet. As I understood the OP, this issue is about importing a file's contents and simply assign it type string?

@clydin
Copy link
Member

clydin commented Feb 22, 2024

That is one specific (and common) use case involving the need for TypeScript to provide additional syntax to allow import attribute aware type definitions.

A standards based approach is strongly preferred over custom string methods currently employed by Webpack, Vite, etc. I believe Vite would also be interested in this approach as well once viable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
angular/build:application area: @angular-devkit/build-angular feature: under consideration Feature request for which voting has completed and the request is now under consideration feature Issue that requests a new feature state: blocked on upstream
Projects
None yet
Development

No branches or pull requests

4 participants