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 support for regex in cache paths #205

Merged
merged 7 commits into from
Mar 14, 2017
Merged

add support for regex in cache paths #205

merged 7 commits into from
Mar 14, 2017

Conversation

jameslnewell
Copy link
Contributor

Hi,

My app has files named like this:

  • client.[hash].js
  • client.[id].[hash].js

I want to include client.[hash].js in my main cache and client.[id].[hash].js in my optional cache. Using a glob makes this difficult (impossible?) because a glob like client.*.js always matches both files.

I want to be able to use a RegExp for the cache configuration options in order to get around this problem e.g. /client.[^.]+.js/

@NekR
Copy link
Owner

NekR commented Mar 12, 2017

Hi, @jameslnewell! I'm fine with this change, just need some tests for that to make sure config doesn't break, i.e. glob matching works the same with change and without.

@jameslnewell
Copy link
Contributor Author

🆒 Thanks, will work on that now. Will there be any ramifications of not checking for absolute paths when using a regexp? https://github.com/jameslnewell/offline-plugin/blob/30802324b097ecdbed1cdd3a93f9f1823da045dd/src/index.js#L405

@NekR
Copy link
Owner

NekR commented Mar 12, 2017

I think it should be something like this:

(typeof cacheKey === 'string' || cacheKey instanceof RegExp) &&
!isAbsoluteURL(cacheKey) &&
cacheKey[0] !== '/' &&
cacheKey.indexOf('./') !== 0 &&
(magic = hasMagic(cacheKey))

isAbsoluteURL and other checks are needed in any case, being it glob pattern or regexp.

@jameslnewell
Copy link
Contributor Author

cacheKey.indexOf fails for regex and cacheKey[0] returns undefined

@NekR
Copy link
Owner

NekR commented Mar 13, 2017

@jameslnewell yeah, my bad, you got me confused about checking for absolute paths.

There is a check for absolute url only to make sure that glob is valid and to not have false positives in hasMagic function (because http://something would be weird file system glob).

I think it's mostly fine as you did now. Let's try this:

let magic;

if (typeof cacheKey === 'string') {
  magic =
    !isAbsoluteURL(cacheKey) &&
    cacheKey[0] !== '/' &&
    cacheKey.indexOf('./') !== 0 &&
    hasMagic(cacheKey);
} else if (cacheKey instanceof RegExp) {
  magic = hasMagic(cacheKey);
} else {
  // Ignore non-string and non-RegExp keys
  return;
}

if (magic) {
  // matching with magic...
}

@jameslnewell
Copy link
Contributor Author

OK how's that? :shipit:

Copy link
Owner

@NekR NekR left a comment

Choose a reason for hiding this comment

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

Also please rename the test to caches-regexp.

export { isAbsolutePath }
Copy link
Owner

Choose a reason for hiding this comment

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

No line break in the end please here.

@@ -6,6 +6,14 @@ const isAbsolutePath = path.isAbsolute;
// Based on https://github.com/isaacs/node-glob/blob/master/glob.js#L83
// (glob.hasMagic)
export function hasMagic(pattern, options) {

//support RegExp as well as glob
Copy link
Owner

Choose a reason for hiding this comment

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

// Support and remove empty line between function name and the comment please.

caches: {
main: [/\.js$/]
},
version: 'da39a3ee5e6b4b0d3255bfef95601890afd80709'
Copy link
Owner

Choose a reason for hiding this comment

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

version: '[hash]' instead.

docs/caches.md Outdated
@@ -8,7 +8,7 @@ ___________________________________
Tells to the plugin what to cache and how.

* `'all'`: means that everything (all the webpack output assets) and URLs listed in `externals` option will be cached on install.
* `Object`: Object with 3 possible sections (properties) of type `Array<string>`: `main`, `additional`, `optional`. All sections are optional and by default are empty (no assets added).
* `Object`: Object with 3 possible sections (properties) of type `Array<string|RegExp>`: `main`, `additional`, `optional`. All sections are optional and by default are empty (no assets added).
Copy link
Owner

Choose a reason for hiding this comment

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

Array<string | RegExp>

docs/options.md Outdated
@@ -8,7 +8,7 @@ _Also see list of default options [here](https://github.com/NekR/offline-plugin/
Allows you to define what to cache and how.

* `'all'`: means that everything (all the webpack output assets) and URLs listed in `externals` option will be cached on install.
* `Object`: Object with 3 possible `Array<string>` sections (properties): `main`, `additional`, `optional`. All sections are optional and by default are empty (no assets added).
* `Object`: Object with 3 possible `Array<string|RegExp>` sections (properties): `main`, `additional`, `optional`. All sections are optional and by default are empty (no assets added).
Copy link
Owner

Choose a reason for hiding this comment

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

Array<string | RegExp>

docs/caches.md Outdated
```
Copy link
Owner

Choose a reason for hiding this comment

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

What is this change? Line break in the end?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah soz, atom's adding them automatically

Copy link
Owner

Choose a reason for hiding this comment

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

oh, I see. I don't have any special feeling about trailing like breaks, I just don't like unnecessary/unrelated changes because who knows when you will need to merge things and why they could break then.

@NekR
Copy link
Owner

NekR commented Mar 13, 2017

Tests fail.

@NekR
Copy link
Owner

NekR commented Mar 13, 2017

Okay, thank you @jameslnewell! I think I'll merge it tomorrow because I don't have time right for the final review.

@jameslnewell
Copy link
Contributor Author

👍 thanks

@NekR
Copy link
Owner

NekR commented Mar 14, 2017

Okay, merging this, many thanks, @jameslnewell!

(I'm not sure when will have a version out with this since all this changes go to v5 and there will be some delay before it's out)

@NekR NekR merged commit 0f9ae03 into NekR:master Mar 14, 2017
@jameslnewell jameslnewell deleted the regexp branch March 14, 2017 19:46
@jameslnewell
Copy link
Contributor Author

no problem

@NekR NekR mentioned this pull request Apr 12, 2017
Closed
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants