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

feat: add option to cache pattern.transform (pattern.cache) #176

Merged
merged 1 commit into from
Dec 14, 2017

Conversation

alexander-akait
Copy link
Member

@alexander-akait alexander-akait commented Nov 24, 2017

Notable Changes

  • implement serialize for cache key to invalidate cache after pattern.transform changed
  • add tests

Copy link
Member

@michael-ciniawsky michael-ciniawsky left a comment

Choose a reason for hiding this comment

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

Why not using memoization instead and make it the default for transform ?

src/writeFile.js Outdated
};

if (pattern.cache) {
const cacheKey = pattern.cacheKey
Copy link
Member

Choose a reason for hiding this comment

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

Do we really need to ability to customize the cacheKey via pattern.options ?

Copy link
Member Author

Choose a reason for hiding this comment

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

@michael-ciniawsky yep, we can use package inside transform function which get options, to invalidate cache you should pass this variables as cacheKey

src/writeFile.js Outdated
if (pattern.cache) {
const cacheKey = pattern.cacheKey
? pattern.cacheKey
: JSON.stringify(serialize({
Copy link
Member

Choose a reason for hiding this comment

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

Could we use/generate an id or hash for patterns to avoid serialization ?

Copy link
Member Author

Choose a reason for hiding this comment

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

@michael-ciniawsky we can generate md5 or sha algo for this

Copy link
Member Author

Choose a reason for hiding this comment

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

@michael-ciniawsky after testing with difference arguments and platform, seems serialize is best solution, btw we can do this in uglify plugin

@michael-ciniawsky michael-ciniawsky added this to the 4.3.0 milestone Nov 24, 2017
@alexander-akait
Copy link
Member Author

@michael-ciniawsky to allow cache between webpack builds, we do this in uglify plugin

@alexander-akait alexander-akait force-pushed the feature-cache branch 7 times, most recently from 38e700c to 7c0e7c4 Compare December 5, 2017 14:28
@alexander-akait
Copy link
Member Author

@michael-ciniawsky friendly ping

@alexander-akait alexander-akait changed the title [WIP] feat: option for caching transform function feat: option for caching transform function Dec 5, 2017
src/writeFile.js Outdated
globalRef.cacheDir = findCacheDir({ name: 'copy-webpack-plugin' });
}

const cacheKey = pattern.cacheKey
Copy link
Member

Choose a reason for hiding this comment

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

Do we really need pattern.cacheKey (Custom cacheKey) ? I'm not in favor of it, seems unnecessary

Copy link
Member Author

Choose a reason for hiding this comment

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

@michael-ciniawsky we need another opinion 😄

Copy link
Member Author

Choose a reason for hiding this comment

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

@michael-ciniawsky btw we can remove this option, because we use pattern as part of cache key and if you want invalidate cache you can use cache option (example cache: '/path/to/' + crypto.createHash('md5').update(JSON.stringify(something) || serialize(something)).digest("hex"))

Copy link
Member

Choose a reason for hiding this comment

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

@evilebottnawi If you add it to the cache pattern option itself { cache: { key: '...' } } || { cache: key } I'm ok with it, but adding to many (related) options 'top scope' of a pattern is unnecessary (bloat) and can be confusing for folks new to copy-webpack-plugin e.g if they assume pattern.cacheKey is mandatory to use for some reason (many don't fully read the docs). There are already too many options for patterns and most folks will tend to use simple patterns like { from: '', to: '' } || { from: '', to: '', cache: true } which is sufficient && straight forward for most of the plugin use cases.

Copy link
Member Author

Choose a reason for hiding this comment

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

@michael-ciniawsky btw, sometimes i have external variables for function inside in transofrm and i can't invalidate cache without this option, i can only enable or disable cache, but this not convenient. Example i use nunjucks inside transform, some template render long time and i want to cache this operation, also i know when i should invalidate cache and cacheKey solve this problem.

Copy link
Member

Choose a reason for hiding this comment

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

Yep, but this already goes under 'advanced' usage in the scope of copy-webpack-plugin imo and wouldn't this work with { cache: ${customCacheKey} instead of { cache: true, cacheKey: ${customCacheKey} } for some reason ?

src/writeFile.js Outdated
const cacheKey = pattern.cacheKey
? pattern.cacheKey
: serialize({
name: name,
Copy link
Member

Choose a reason for hiding this comment

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

serialize({
   name,
   version,
   pattern,
   content
})

tests/index.js Outdated
.then(() => {
return cacache
.ls(cacheDir)
.then((cacheEntriesList) => {
Copy link
Member

Choose a reason for hiding this comment

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

cacheEntriesList => cacheEntries

tests/index.js Outdated
return cacache
.ls(cacheDir)
.then((cacheEntriesList) => {
const cacheEntriesListKeys = Object.keys(cacheEntriesList);
Copy link
Member

Choose a reason for hiding this comment

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

cacheEntriesListKeys => cacheKeys

const cacheKeys = Object.keys(cacheEntries)

src/writeFile.js Outdated
globalRef.cacheDir = findCacheDir({ name: 'copy-webpack-plugin' });
}

const cacheKey = pattern.cacheKey
Copy link
Member

Choose a reason for hiding this comment

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

@evilebottnawi If you add it to the cache pattern option itself { cache: { key: '...' } } || { cache: key } I'm ok with it, but adding to many (related) options 'top scope' of a pattern is unnecessary (bloat) and can be confusing for folks new to copy-webpack-plugin e.g if they assume pattern.cacheKey is mandatory to use for some reason (many don't fully read the docs). There are already too many options for patterns and most folks will tend to use simple patterns like { from: '', to: '' } || { from: '', to: '', cache: true } which is sufficient && straight forward for most of the plugin use cases.

tests/index.js Outdated

expect(cacheKeys).to.have.lengthOf(1);

cacheKeys.forEach((serializedCacheEntry) => {
Copy link
Member

Choose a reason for hiding this comment

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

cacheKeys.forEach((cacheKey) => {
  // eslint-disable-next-line no-eval
  cacheKey = eval(`(${cacheKey})`);
 
  expect(cacheKey.pattern.from).to.equal(from);
});

Copy link
Member Author

Choose a reason for hiding this comment

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

@alexander-akait
Copy link
Member Author

@michael-ciniawsky friendly ping, move key inside cache option.

Copy link
Member

@michael-ciniawsky michael-ciniawsky left a comment

Choose a reason for hiding this comment

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

👍

@michael-ciniawsky michael-ciniawsky changed the title feat: option for caching transform function feat: add option to cache pattern.transform (pattern.cache) Dec 12, 2017
@michael-ciniawsky michael-ciniawsky merged commit 20c143b into master Dec 14, 2017
@michael-ciniawsky michael-ciniawsky deleted the feature-cache branch December 14, 2017 04:24
@michael-ciniawsky michael-ciniawsky removed this from the 4.3.0 milestone Dec 14, 2017
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants