Skip to content

feat(@angular/build): support import attribute based loader configuration #28040

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

Merged
merged 1 commit into from
Jul 15, 2024

Conversation

clydin
Copy link
Member

@clydin clydin commented Jul 11, 2024

When using the application builder, a loader import attribute is now available for use with import statements and expressions. The presence of the import attribute takes precedence over all other loading behavior including JS/TS and any loader build option values. This allows per file control over loading behavior. For general loading for all files of an otherwise unsupported file type, the loader build option is recommended.

For the import attribute, the following loader values are supported:

  • text - inlines the content as a string
  • binary - inlines the content as a Uint8Array
  • file - emits the file and provides the runtime location of the file

Unfortunately, at this time, TypeScript does not support type definitions that are based on import attribute values. The use of @ts-expect-error or the use of individual type definition files (assuming the file is only imported with the same loader attribute) is currently required.

Additionally, the TypeScript module option must be set to esnext to allow TypeScript to successfully build the application code.

As an example, an SVG file can be imported as text via:

// @ts-expect-error TypeScript cannot provide types based on attributes yet
import contents from './some-file.svg' with { loader: 'text' };

When using the development server and a file that is referenced from a Node.js package with a loader attribute, the package must be excluded from prebundling via the development server prebundle option. This does not apply to relative file references.

This feature provides the bundling aspects of #26575. TypeScript type definition support would provide the full feature set for that issue but is currently blocked on TypeScript upstream.

@clydin clydin added the target: minor This PR is targeted for the next minor release label Jul 11, 2024
@angular-robot angular-robot bot added the detected: feature PR contains a feature commit label Jul 11, 2024
@clydin clydin force-pushed the application/loader-import-attribute branch from 6303751 to 1d15d22 Compare July 13, 2024 00:09
…tion

When using the application builder, a `loader` import attribute is now available
for use with import statements and expressions. The presence of the import
attribute takes precedence over all other loading behavior including JS/TS and
any `loader` build option values. This allows per file control over loading
behavior. For general loading for all files of an otherwise unsupported file
type, the `loader` build option is recommended.

For the import attribute, the following loader values are supported:
* `text` - inlines the content as a string
* `binary` - inlines the content as a Uint8Array
* `file` - emits the file and provides the runtime location of the file

Unfortunately, at this time, TypeScript does not support type definitions
that are based on import attribute values. The use of `@ts-expect-error`
or the use of individual type definition files (assuming the file is only
imported with the same loader attribute) is currently required.

Additionally, the TypeScript `module` option must be set to `esnext` to
allow TypeScript to successfully build the application code.

As an example, an SVG file can be imported as text via:
```
// @ts-expect-error TypeScript cannot provide types based on attributes yet
import contents from './some-file.svg' with { loader: 'text' };
```

When using the development server and a file that is referenced from a Node.js
package with a loader attribute, the package must be excluded from prebundling
via the development server `prebundle` option. This does not apply to relative
file references.
@clydin clydin force-pushed the application/loader-import-attribute branch from 1d15d22 to 3272ebb Compare July 13, 2024 00:11
@clydin clydin marked this pull request as ready for review July 13, 2024 00:22
@clydin clydin added the action: review The PR is still awaiting reviews from at least one requested reviewer label Jul 13, 2024
@clydin clydin requested a review from alan-agius4 July 13, 2024 00:24
@alan-agius4 alan-agius4 added action: merge The PR is ready for merge by the caretaker and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Jul 15, 2024
@clydin clydin merged commit 24aaf1e into angular:main Jul 15, 2024
33 checks passed
@clydin clydin deleted the application/loader-import-attribute branch July 15, 2024 12:08
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Aug 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker detected: feature PR contains a feature commit target: minor This PR is targeted for the next minor release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants