-
Notifications
You must be signed in to change notification settings - Fork 12k
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
feat(@angular-devkit/build-angular): allow configuring loaders for custom file extensions in application builder #26371
Conversation
35bea0f
to
95284b9
Compare
…stom file extensions in application builder When using the `application` builder, a new `loader` option is now available for use. The option allows a project to define the type of loader to use with a specified file extension. A file with the defined extension can then used within the application code via an import statement or dynamic import expression, for instance. The available loaders that can be used are: * `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 * `empty` - considers the content to be empty and will not include it in bundles The loader option is an object-based option with the keys used to define the file extension and the values used to define the loader type. An example to inline the content of SVG files into the bundled application would be as follows: ``` loader: { ".svg": "text" } ``` An SVG file can then be imported: ``` import contents from './some-file.svg'; ``` Additionally, TypeScript needs to be aware of the module type for the import to prevent type-checking errors during the build. This can be accomplished with an additional type definition file within the application source code (`src/types.d.ts`, for example) with the following or similar content: ``` declare module "*.svg" { const content: string; export default content; } ``` The default project configuration is already setup to use any type definition files present in the project source directories. If the TypeScript configuration for the project has been altered, the tsconfig may need to be adjusted to reference this newly added type definition file.
95284b9
to
7f33e38
Compare
@clydin I'm trying to import one of the .ts files of my angular project as a raw text file, does this allow me to achieve that? If I configure a loader of How can I achieve that? |
You currently cannot if it's a |
where exactly do I add the loader? what are the configurations I need in the angular.json file? this is what I have: |
@ranitg It is a new build option for the |
Thanks! I got it to work |
My use case requires me to import source files as text to showcase how to achieve the examples shown in the app. I'm really looking forward for such a feature, I'm keen to create a proposal for it in a PR but I'd need a little guidance. |
Will this option be supported in |
I have a similar use case I have a web app that embeds the notorious |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
When using the
application
builder, a newloader
option is now available for use. The option allows a project to define the type of loader to use with a specified file extension. A file with the defined extension can then be used within the application code via an import statement or dynamic import expression, for instance.The available loaders that can be used are:
text
- inlines the content as a stringbinary
- inlines the content as a Uint8Arrayfile
- emits the file and provides the runtime location of the fileempty
- considers the content to be empty and will not include it in bundlesThe loader option is an object-based option with the keys used to define the file extension and the values used to define the loader type.
An example to inline the content of SVG files into the bundled application would be as follows:
An SVG file can then be imported:
Additionally, TypeScript needs to be aware of the module type for the import to prevent type-checking errors during the build. This can be accomplished with an additional type definition file within the application source code (
src/types.d.ts
, for example) with the following or similar content:The default project configuration is already setup to use any type definition files present in the project source directories. If the TypeScript configuration for the project has been altered, the tsconfig may need to be adjusted to reference this newly added type definition file.