Skip to content

Commit

Permalink
Merge pull request #205 from jameslnewell/regexp
Browse files Browse the repository at this point in the history
Add support for RegExp in caches
  • Loading branch information
NekR committed Mar 14, 2017
2 parents b69e179 + bcece4b commit 0f9ae03
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 10 deletions.
2 changes: 1 addition & 1 deletion 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).

> Default: `'all'`.
Expand Down
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).

[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 @@ -393,7 +393,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
9 changes: 9 additions & 0 deletions lib/misc/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ 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 @@ -401,12 +401,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
9 changes: 8 additions & 1 deletion src/misc/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ 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
if (pattern instanceof RegExp) {
return {
match: str => pattern.test(str)
};
}

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

Expand All @@ -19,7 +26,7 @@ export function hasMagic(pattern, options) {
}
}

return false
return false;
}

export function getSource(source) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CACHE MANIFEST
#ver:f2435b5e97ab165cde456b61eb28bf8bc103076e

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": "f2435b5e97ab165cde456b61eb28bf8bc103076e",
"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: '[hash]'
});

0 comments on commit 0f9ae03

Please sign in to comment.