Skip to content

Use asset modules and resourceQuery#22252

Merged
alan-agius4 merged 2 commits intoangular:masterfrom
alan-agius4:asset-modules-webpack
Dec 8, 2021
Merged

Use asset modules and resourceQuery#22252
alan-agius4 merged 2 commits intoangular:masterfrom
alan-agius4:asset-modules-webpack

Conversation

@alan-agius4
Copy link
Copy Markdown
Collaborator

refactor(@ngtools/webpack): remove direct angular resource loader
We remove the custom direct resource loader which is used for JIT when directTemplateLoading is enabled. Instead, use Webpack's asset modules which were introduced in version 5.

To the resource URL, we also add a query parameter, ngResource. This is used to be filter request based on a query. See https://webpack.js.org/guides/asset-modules/#replacing-inline-loader-syntax for more information.

fix(@angular-devkit/build-angular): differentiate components and global styles using file query instead of filename.
Previously, we introduced the ngResource query to Angular component resources we now use it with resourceQuery to differentiate between global and components styles, since in some cases while unlikely a file can be used as a component and global style.

Closes #7245

@alan-agius4 alan-agius4 added the target: minor This PR is targeted for the next minor release label Nov 30, 2021
@google-cla google-cla bot added the cla: yes label Nov 30, 2021
@alan-agius4 alan-agius4 requested a review from clydin November 30, 2021 12:08
@alan-agius4 alan-agius4 marked this pull request as ready for review November 30, 2021 12:09
Comment thread packages/ngtools/webpack/src/resource_loader.ts
Comment thread packages/ngtools/webpack/src/resource_loader.ts Outdated
Comment thread packages/ngtools/webpack/src/resource_loader.ts
Comment thread packages/ngtools/webpack/src/transformers/replace_resources.ts

it('works when using the same css file in `styles` and `stylesUrl`', async () => {
host.writeMultipleFiles({
'src/styles.css': `
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is used as a global style by default right? Is there some way we can explicitly list this as a global file as part of this test? As is, we're implicitly depending on this default which could change and invalidate the intent of the test but probably leave it still passing.

Copy link
Copy Markdown
Collaborator Author

@alan-agius4 alan-agius4 Dec 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that is correct. Added to explicitly set the styles value to src/styles.css

const MAIN_OUTPUT = 'dist/main.js';
const NAMED_LAZY_OUTPUT = 'dist/src_lazy-module_ts.js';
const UNNAMED_LAZY_OUTPUT = 'dist/8.js';
const UNNAMED_LAZY_OUTPUT = 'dist/459.js';
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just out of my own curiosity, where does this come from and why does it need to change? Is this some kind of hash of a lazy loaded module which happened to change slightly from this commit?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ID generation is internal to Webpack, I think it changed due to the changes in resource paths due to the additional of the query parameter.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, thanks for the clarification.

Comment thread packages/angular_devkit/build_angular/src/webpack/configs/common.ts Outdated
Comment thread packages/angular_devkit/build_angular/src/webpack/configs/styles.ts Outdated
We remove the custom direct resource loader which is used for JIT when `directTemplateLoading` is enabled. Instead, use Webpack's [asset modules](https://webpack.js.org/guides/asset-modules/) which were introduced in version 5.

To the resource URL, we also add a query parameter, `ngResource`. This is used to be filter request based on a query. See https://webpack.js.org/guides/asset-modules/#replacing-inline-loader-syntax for more information.
…al styles using file query instead of filename

Previously, we introduced the `ngResource` query to Angular component resources we now use it with `resourceQuery` to differentiate between global and components styles, since in some cases while unlikely a file can be used as a component and global style.

Closes #7245
Copy link
Copy Markdown
Collaborator

@dgp1130 dgp1130 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, up to you if you want to wait until January for Charles' input, no rush on this I think.

const MAIN_OUTPUT = 'dist/main.js';
const NAMED_LAZY_OUTPUT = 'dist/src_lazy-module_ts.js';
const UNNAMED_LAZY_OUTPUT = 'dist/8.js';
const UNNAMED_LAZY_OUTPUT = 'dist/459.js';
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, thanks for the clarification.

@alan-agius4 alan-agius4 added the action: merge The PR is ready for merge by the caretaker label Dec 6, 2021
@alan-agius4 alan-agius4 merged commit 9bacba3 into angular:master Dec 8, 2021
@alan-agius4 alan-agius4 deleted the asset-modules-webpack branch December 8, 2021 07:33
@angular-automatic-lock-bot
Copy link
Copy Markdown

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 Jan 8, 2022
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 target: minor This PR is targeted for the next minor release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"Expected 'styles' to be an array of strings." when a stylesheet is referenced in a Component's styleUrls[] and .angular-cli.json's app[0].styles[]

2 participants