Skip to content

Use asset modules and resourceQuery #22252

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 2 commits into from
Dec 8, 2021
Merged

Use asset modules and resourceQuery #22252

merged 2 commits into from
Dec 8, 2021

Conversation

alan-agius4
Copy link
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

it('works when using the same css file in `styles` and `stylesUrl`', async () => {
host.writeMultipleFiles({
'src/styles.css': `
Copy link
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
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

@@ -11,7 +11,7 @@ import { BASE_OPTIONS, BROWSER_BUILDER_INFO, describeBuilder } from '../setup';

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
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
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
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.

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
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.

@@ -11,7 +11,7 @@ import { BASE_OPTIONS, BROWSER_BUILDER_INFO, describeBuilder } from '../setup';

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
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

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