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

Add processor cache in webpack loader #1912

Merged
merged 1 commit into from Jan 25, 2022
Merged

Add processor cache in webpack loader #1912

merged 1 commit into from Jan 25, 2022

Conversation

wooorm
Copy link
Member

@wooorm wooorm commented Jan 25, 2022

Backports: wooorm/xdm@decafe9.

Related-to: GH-1468.

@wooorm wooorm added 🦋 type/enhancement This is great to have 🗄 area/interface This affects the public interface 🧒 semver/minor This is backwards-compatible change labels Jan 25, 2022
@vercel

This comment has been minimized.

@codecov-commenter

This comment has been minimized.

@wooorm wooorm merged commit e0b697a into main Jan 25, 2022
@wooorm wooorm deleted the webpack-cache branch January 25, 2022 08:36
@@ -21,20 +27,25 @@ export function loader(value, callback) {
/** @type {Defaults} */
const defaults = this.sourceMap ? {SourceMapGenerator} : {}
const options = /** @type {CompileOptions} */ (this.getOptions())
const config = {...defaults, ...options}
Copy link
Member

Choose a reason for hiding this comment

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

The config object is constructed here. This is always a new object by reference. It’s then used as a key in the weak map cache. Once the loader is done processing a file, the config object is unused and garbage collected, meaning it’s removed from the cache.

This means the cache is never used.

If options is always the same object, that could be used.

Copy link
Member Author

Choose a reason for hiding this comment

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

Is options a reference to the same object? Seems to weird that this.getOptions() would do that.

Copy link
Member

Choose a reason for hiding this comment

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

I don’t know. That’s why I wrote if.

I think it might be the same object if options are passed to the loader as an object, but not when they are passed as a query parameter. I’m really just guessing here though.

Copy link
Member Author

Choose a reason for hiding this comment

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

We can still use this.query though?

Copy link
Member Author

Choose a reason for hiding this comment

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

…assuming that worked before, you tested it?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I tested it back then. If query is the raw object, that’s probably our best option. Just beware it might also be a string, which can’t be used as a key for weak maps.

It was heavily inspired by ts-loader. They use a similar approach here.

Also when I implemented it, this was compatible with Webpack 4. The use of getOptions() indicate this is no longer the case. I don’t know if the use of Webpack 5 opens up better alternatives.

Copy link
Member Author

Choose a reason for hiding this comment

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

ts-loader has a similar approach but caches on webpack compilers. Should we use those instead?

Copy link
Member

Choose a reason for hiding this comment

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

I think it doesn’t affect most users, as most users instantiate a single compiler, but it’s still a good idea.

Copy link
Member Author

Choose a reason for hiding this comment

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

wooorm added a commit that referenced this pull request Jan 26, 2022
Related-to: GH-1468.
Related-to: GH-1912.

Reviewed-by: Remco Haszing <remcohaszing@gmail.com>
Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🗄 area/interface This affects the public interface 🧒 semver/minor This is backwards-compatible change 🦋 type/enhancement This is great to have
Development

Successfully merging this pull request may close these issues.

None yet

3 participants