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
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/caches.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>


> Default: `'all'`.

Expand Down Expand Up @@ -43,4 +43,4 @@ AppCache doesn't support conditional or delayed assets loading and by default ig
AppCache: {
caches: ['main', 'additional', 'optional']
}
```
```
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.

2 changes: 1 addition & 1 deletion docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>


[More details about `caches`](caches.md)

Expand Down
11 changes: 10 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,16 @@ var OfflinePlugin = (function () {

var magic = undefined;

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

if (magic) {
var matched = undefined;

for (var i = 0, len = assets.length; i < len; i++) {
Expand Down
10 changes: 10 additions & 0 deletions lib/misc/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ var isAbsolutePath = _path2['default'].isAbsolute;
// (glob.hasMagic)

function hasMagic(pattern, options) {

//support RegExp as well as glob
if (pattern instanceof RegExp) {
return {
match: function match(str) {
return pattern.test(str);
}
};
}

var minimatch = new _minimatch.Minimatch(pattern, options);
var set = minimatch.set;

Expand Down
20 changes: 14 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,12 +400,20 @@ export default class OfflinePlugin {

let magic;

if (
!isAbsoluteURL(cacheKey) &&
cacheKey[0] !== '/' &&
cacheKey.indexOf('./') !== 0 &&
(magic = hasMagic(cacheKey))
) {
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) {
let matched;

for (let i = 0, len = assets.length; i < len; i++) {
Expand Down
10 changes: 9 additions & 1 deletion src/misc/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.

if (pattern instanceof RegExp) {
return {
match: str => pattern.test(str)
};
}

const minimatch = new Minimatch(pattern, options);
const set = minimatch.set;

Expand Down Expand Up @@ -49,4 +57,4 @@ export function isAbsoluteURL(url) {
return /^(?:\w+:)?\/\//.test(url);
}

export { isAbsolutePath }
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.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CACHE MANIFEST
#ver:da39a3ee5e6b4b0d3255bfef95601890afd80709

CACHE:
../main.js

NETWORK:
*
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!doctype html>
<html manifest="manifest.appcache"></html>
50 changes: 50 additions & 0 deletions tests/legacy/fixtures/paths-regex/__expected/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};

/******/ // The require function
/******/ function __webpack_require__(moduleId) {

/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;

/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };

/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);

/******/ // Flag the module as loaded
/******/ module.loaded = true;

/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }


/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;

/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;

/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";

/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports) {



/***/ }
/******/ ]);
18 changes: 18 additions & 0 deletions tests/legacy/fixtures/paths-regex/__expected/sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var __wpo = {
"assets": {
"main": [
"./main.js"
],
"additional": [],
"optional": []
},
"externals": [],
"hashesMap": {
"fe8df8db17e5f8edb5c44ada1a7fdd9fe1a5012c": "./main.js"
},
"strategy": "changed",
"responseStrategy": "cache-first",
"version": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"name": "webpack-offline",
"relativePaths": true
};
Empty file.
6 changes: 6 additions & 0 deletions tests/legacy/fixtures/paths-regex/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = __CONFIG__({
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.

});