grunt-swn-cachebuster 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.jshintrc +13 -13
- package/Gruntfile.js +73 -73
- package/LICENSE +21 -21
- package/README.md +55 -62
- package/{tasks → lib}/replace.js +14 -13
- package/package.json +45 -42
- package/tasks/cachebuster.js +65 -70
- package/test/test.js +1 -1
- package/.npmignore +0 -3
package/.jshintrc
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
{
|
|
2
|
-
"curly": true,
|
|
3
|
-
"eqeqeq": true,
|
|
4
|
-
"immed": true,
|
|
5
|
-
"latedef": true,
|
|
6
|
-
"newcap": true,
|
|
7
|
-
"noarg": true,
|
|
8
|
-
"sub": true,
|
|
9
|
-
"undef": true,
|
|
10
|
-
"boss": true,
|
|
11
|
-
"eqnull": true,
|
|
12
|
-
"node": true
|
|
13
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"curly": true,
|
|
3
|
+
"eqeqeq": true,
|
|
4
|
+
"immed": true,
|
|
5
|
+
"latedef": true,
|
|
6
|
+
"newcap": true,
|
|
7
|
+
"noarg": true,
|
|
8
|
+
"sub": true,
|
|
9
|
+
"undef": true,
|
|
10
|
+
"boss": true,
|
|
11
|
+
"eqnull": true,
|
|
12
|
+
"node": true
|
|
13
|
+
}
|
package/Gruntfile.js
CHANGED
|
@@ -1,73 +1,73 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* grunt-swn-cachebuster
|
|
3
|
-
* https://github.com/Swaven/grunt-swn-cachebuster
|
|
4
|
-
*
|
|
5
|
-
* Copyright (c) 2017 Swaven
|
|
6
|
-
* Licensed under the MIT license.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
'use strict';
|
|
10
|
-
|
|
11
|
-
module.exports = function(grunt) {
|
|
12
|
-
|
|
13
|
-
// Project configuration.
|
|
14
|
-
grunt.initConfig({
|
|
15
|
-
jshint: {
|
|
16
|
-
all: [
|
|
17
|
-
'Gruntfile.js',
|
|
18
|
-
'tasks/*.js',
|
|
19
|
-
'<%= nodeunit.tests %>'
|
|
20
|
-
],
|
|
21
|
-
options: {
|
|
22
|
-
jshintrc: '.jshintrc'
|
|
23
|
-
}
|
|
24
|
-
},
|
|
25
|
-
|
|
26
|
-
// Before generating any new files, remove any previously-created files.
|
|
27
|
-
clean: {
|
|
28
|
-
tests: ['tmp']
|
|
29
|
-
},
|
|
30
|
-
|
|
31
|
-
// Configuration to be run (and then tested).
|
|
32
|
-
cachebuster: {
|
|
33
|
-
default_options: {
|
|
34
|
-
options: {
|
|
35
|
-
},
|
|
36
|
-
files: {
|
|
37
|
-
'tmp/default_options': ['test/fixtures/testing', 'test/fixtures/123']
|
|
38
|
-
}
|
|
39
|
-
},
|
|
40
|
-
custom_options: {
|
|
41
|
-
options: {
|
|
42
|
-
separator: ': ',
|
|
43
|
-
punctuation: ' !!!'
|
|
44
|
-
},
|
|
45
|
-
files: {
|
|
46
|
-
'tmp/custom_options': ['test/fixtures/testing', 'test/fixtures/123']
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
},
|
|
50
|
-
|
|
51
|
-
// Unit tests.
|
|
52
|
-
nodeunit: {
|
|
53
|
-
tests: ['test/*_test.js']
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
// Actually load this plugin's task(s).
|
|
59
|
-
grunt.loadTasks('tasks');
|
|
60
|
-
|
|
61
|
-
// These plugins provide necessary tasks.
|
|
62
|
-
grunt.loadNpmTasks('grunt-contrib-jshint');
|
|
63
|
-
grunt.loadNpmTasks('grunt-contrib-clean');
|
|
64
|
-
grunt.loadNpmTasks('grunt-contrib-nodeunit');
|
|
65
|
-
|
|
66
|
-
// Whenever the "test" task is run, first clean the "tmp" dir, then run this
|
|
67
|
-
// plugin's task(s), then test the result.
|
|
68
|
-
grunt.registerTask('test', ['clean', 'cachebuster', 'nodeunit']);
|
|
69
|
-
|
|
70
|
-
// By default, lint and run all tests.
|
|
71
|
-
grunt.registerTask('default', ['jshint', 'test']);
|
|
72
|
-
|
|
73
|
-
};
|
|
1
|
+
/*
|
|
2
|
+
* grunt-swn-cachebuster
|
|
3
|
+
* https://github.com/Swaven/grunt-swn-cachebuster
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) 2017 Swaven
|
|
6
|
+
* Licensed under the MIT license.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
'use strict';
|
|
10
|
+
|
|
11
|
+
module.exports = function(grunt) {
|
|
12
|
+
|
|
13
|
+
// Project configuration.
|
|
14
|
+
grunt.initConfig({
|
|
15
|
+
jshint: {
|
|
16
|
+
all: [
|
|
17
|
+
'Gruntfile.js',
|
|
18
|
+
'tasks/*.js',
|
|
19
|
+
'<%= nodeunit.tests %>'
|
|
20
|
+
],
|
|
21
|
+
options: {
|
|
22
|
+
jshintrc: '.jshintrc'
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
|
|
26
|
+
// Before generating any new files, remove any previously-created files.
|
|
27
|
+
clean: {
|
|
28
|
+
tests: ['tmp']
|
|
29
|
+
},
|
|
30
|
+
|
|
31
|
+
// Configuration to be run (and then tested).
|
|
32
|
+
cachebuster: {
|
|
33
|
+
default_options: {
|
|
34
|
+
options: {
|
|
35
|
+
},
|
|
36
|
+
files: {
|
|
37
|
+
'tmp/default_options': ['test/fixtures/testing', 'test/fixtures/123']
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
custom_options: {
|
|
41
|
+
options: {
|
|
42
|
+
separator: ': ',
|
|
43
|
+
punctuation: ' !!!'
|
|
44
|
+
},
|
|
45
|
+
files: {
|
|
46
|
+
'tmp/custom_options': ['test/fixtures/testing', 'test/fixtures/123']
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
|
|
51
|
+
// Unit tests.
|
|
52
|
+
nodeunit: {
|
|
53
|
+
tests: ['test/*_test.js']
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// Actually load this plugin's task(s).
|
|
59
|
+
grunt.loadTasks('tasks');
|
|
60
|
+
|
|
61
|
+
// These plugins provide necessary tasks.
|
|
62
|
+
grunt.loadNpmTasks('grunt-contrib-jshint');
|
|
63
|
+
grunt.loadNpmTasks('grunt-contrib-clean');
|
|
64
|
+
grunt.loadNpmTasks('grunt-contrib-nodeunit');
|
|
65
|
+
|
|
66
|
+
// Whenever the "test" task is run, first clean the "tmp" dir, then run this
|
|
67
|
+
// plugin's task(s), then test the result.
|
|
68
|
+
grunt.registerTask('test', ['clean', 'cachebuster', 'nodeunit']);
|
|
69
|
+
|
|
70
|
+
// By default, lint and run all tests.
|
|
71
|
+
grunt.registerTask('default', ['jshint', 'test']);
|
|
72
|
+
|
|
73
|
+
};
|
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2017 Swaven
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017 Swaven
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,62 +1,55 @@
|
|
|
1
|
-
# grunt-swn-cachebuster
|
|
2
|
-
|
|
3
|
-
> Appends timestamp param to import urls
|
|
4
|
-
|
|
5
|
-
## Getting Started
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
## Release History
|
|
57
|
-
|
|
58
|
-
* 2017-09-18: [v0.2.0](https://github.com/Swaven/grunt-swn-cachebuster/releases/tag/v0.2.0): error handling
|
|
59
|
-
* 2017-09-18: [v0.1.3](https://github.com/Swaven/grunt-swn-cachebuster/releases/tag/v0.1.3): add some tests
|
|
60
|
-
* 2017-06-28: [v0.1.2](https://github.com/Swaven/grunt-swn-cachebuster/releases/tag/v0.1.2): fix regex
|
|
61
|
-
* 2017-06-27: [v0.1.1](https://github.com/Swaven/grunt-swn-cachebuster/releases/tag/v0.1.1): more robust regex
|
|
62
|
-
* 2017-06-27: [v0.1.0](https://github.com/Swaven/grunt-swn-cachebuster/releases/tag/v0.1.0): first release
|
|
1
|
+
# grunt-swn-cachebuster
|
|
2
|
+
|
|
3
|
+
> Appends timestamp param to import urls
|
|
4
|
+
|
|
5
|
+
## Getting Started
|
|
6
|
+
|
|
7
|
+
This plugin requires **Node.js `>=22`** and **Grunt `^1.6.1`**.
|
|
8
|
+
|
|
9
|
+
If you haven't used [Grunt](http://gruntjs.com/) before, be sure to check out the [Getting Started](http://gruntjs.com/getting-started) guide, as it explains how to create a [Gruntfile](http://gruntjs.com/sample-gruntfile) as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
|
|
10
|
+
|
|
11
|
+
```shell
|
|
12
|
+
npm install grunt-swn-cachebuster --save-dev
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
|
|
16
|
+
|
|
17
|
+
```js
|
|
18
|
+
grunt.loadNpmTasks('grunt-swn-cachebuster');
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## The "cachebuster" task
|
|
22
|
+
|
|
23
|
+
### Overview
|
|
24
|
+
|
|
25
|
+
This task looks into source files, and edits all html and js file urls to append a timestamp query string parameter. It ignores urls that start with */bower_components*.
|
|
26
|
+
|
|
27
|
+
In your project's Gruntfile, add a section named `cachebuster` to the data object passed into `grunt.initConfig()`.
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
grunt.initConfig({
|
|
31
|
+
cachebuster: {
|
|
32
|
+
your_target: {
|
|
33
|
+
// Target-specific file lists and/or options go here.
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Usage Examples
|
|
40
|
+
|
|
41
|
+
```js
|
|
42
|
+
grunt.initConfig({
|
|
43
|
+
cachebuster: {
|
|
44
|
+
all: {
|
|
45
|
+
expand: true,
|
|
46
|
+
cwd: 'assets/',
|
|
47
|
+
src: ['*.html', 'elements/**/*.html', '!build/', 'elements/swn-app/swn-app.js'],
|
|
48
|
+
dest: 'assets/build/'
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Contributing
|
|
55
|
+
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com/).
|
package/{tasks → lib}/replace.js
RENAMED
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const rx = /(\/?bower_components)?(\/.*\.(html|js))('|"|`)/gi, // find all dependencies
|
|
4
|
-
ts = Date.now() // timestamp we'll add to all dependencies urls
|
|
5
|
-
|
|
6
|
-
module.exports = exports = function replace(content){
|
|
7
|
-
if (typeof content !== 'string')
|
|
8
|
-
throw new Error(
|
|
9
|
-
|
|
10
|
-
return content.replace(rx, ($0, $1, $2, $3, $4) => {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const rx = /(\/?bower_components)?(\/.*\.(html|js))('|"|`)/gi, // find all dependencies
|
|
4
|
+
ts = Date.now() // timestamp we'll add to all dependencies urls
|
|
5
|
+
|
|
6
|
+
module.exports = exports = function replace(content){
|
|
7
|
+
if (typeof content !== 'string')
|
|
8
|
+
throw new Error(`Content is not a string`)
|
|
9
|
+
|
|
10
|
+
return content.replace(rx, ($0, $1, $2, $3, $4) => {
|
|
11
|
+
// ignore urls to bower_components
|
|
12
|
+
return $1 ? $0 : `${$2}?v=${ts}${$4}`
|
|
13
|
+
})
|
|
14
|
+
}
|
package/package.json
CHANGED
|
@@ -1,42 +1,45 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "grunt-swn-cachebuster",
|
|
3
|
-
"description": "Appends timestamp param to import urls",
|
|
4
|
-
"version": "0.
|
|
5
|
-
"homepage": "https://github.com/Swaven/grunt-swn-cachebuster",
|
|
6
|
-
"author": {
|
|
7
|
-
"name": "Swaven",
|
|
8
|
-
"email": "antoine@swaven.com"
|
|
9
|
-
},
|
|
10
|
-
"repository": {
|
|
11
|
-
"type": "git",
|
|
12
|
-
"url": "https://github.com/Swaven/grunt-swn-cachebuster.git"
|
|
13
|
-
},
|
|
14
|
-
"bugs": {
|
|
15
|
-
"url": "https://github.com/Swaven/grunt-swn-cachebuster/issues"
|
|
16
|
-
},
|
|
17
|
-
"licenses": [
|
|
18
|
-
{
|
|
19
|
-
"type": "MIT",
|
|
20
|
-
"url": "https://github.com/Swaven/grunt-swn-cachebuster/blob/master/LICENSE-MIT"
|
|
21
|
-
}
|
|
22
|
-
],
|
|
23
|
-
"engines": {
|
|
24
|
-
"node": ">=
|
|
25
|
-
},
|
|
26
|
-
"scripts": {
|
|
27
|
-
"test": "mocha"
|
|
28
|
-
},
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
"
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
"
|
|
41
|
-
}
|
|
42
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "grunt-swn-cachebuster",
|
|
3
|
+
"description": "Appends timestamp param to import urls",
|
|
4
|
+
"version": "0.3.0",
|
|
5
|
+
"homepage": "https://github.com/Swaven/grunt-swn-cachebuster",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "Swaven",
|
|
8
|
+
"email": "antoine@swaven.com"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/Swaven/grunt-swn-cachebuster.git"
|
|
13
|
+
},
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/Swaven/grunt-swn-cachebuster/issues"
|
|
16
|
+
},
|
|
17
|
+
"licenses": [
|
|
18
|
+
{
|
|
19
|
+
"type": "MIT",
|
|
20
|
+
"url": "https://github.com/Swaven/grunt-swn-cachebuster/blob/master/LICENSE-MIT"
|
|
21
|
+
}
|
|
22
|
+
],
|
|
23
|
+
"engines": {
|
|
24
|
+
"node": ">=22.0.0"
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"test": "mocha"
|
|
28
|
+
},
|
|
29
|
+
"overrides": {
|
|
30
|
+
"js-yaml": "~3.14.2",
|
|
31
|
+
"lodash": "~4.18.1",
|
|
32
|
+
"minimatch": "^3.1.3 || ^5.1.8 || ^9.0.9"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"chai": "^4.1.2",
|
|
36
|
+
"grunt": "^1.6.1",
|
|
37
|
+
"grunt-contrib-clean": "^2.0.1",
|
|
38
|
+
"grunt-contrib-jshint": "^3.2.0",
|
|
39
|
+
"grunt-contrib-nodeunit": "^5.0.0",
|
|
40
|
+
"mocha": "^11.0.0"
|
|
41
|
+
},
|
|
42
|
+
"keywords": [
|
|
43
|
+
"gruntplugin"
|
|
44
|
+
]
|
|
45
|
+
}
|
package/tasks/cachebuster.js
CHANGED
|
@@ -1,70 +1,65 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
/*
|
|
4
|
-
* grunt-swn-cachebuster
|
|
5
|
-
* https://github.com/Swaven/grunt-swn-cachebuster
|
|
6
|
-
*
|
|
7
|
-
* Copyright (c) 2017 Swaven
|
|
8
|
-
* Licensed under the MIT license.
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
const fs = require('fs'),
|
|
12
|
-
path = require('path'),
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
})
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
}
|
|
70
|
-
}
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* grunt-swn-cachebuster
|
|
5
|
+
* https://github.com/Swaven/grunt-swn-cachebuster
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2017 Swaven
|
|
8
|
+
* Licensed under the MIT license.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const fs = require('fs'),
|
|
12
|
+
path = require('path'),
|
|
13
|
+
replace = require('../lib/replace.js'),
|
|
14
|
+
util = require('util'),
|
|
15
|
+
lstat = util.promisify(fs.lstat),
|
|
16
|
+
readFile = util.promisify(fs.readFile),
|
|
17
|
+
writeFile = util.promisify(fs.writeFile)
|
|
18
|
+
|
|
19
|
+
module.exports = function(grunt){
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
grunt.registerMultiTask('cachebuster', 'Appends timestamp param to import urls', function(){
|
|
23
|
+
const done = this.async(),
|
|
24
|
+
options = this.options(),
|
|
25
|
+
self = this
|
|
26
|
+
|
|
27
|
+
var proms = []
|
|
28
|
+
|
|
29
|
+
// get all files' content and dependencies
|
|
30
|
+
for (let i = 0; i < this.files.length; i++){
|
|
31
|
+
proms.push(updateLinks(this.files[i]))
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
Promise.all(proms)
|
|
35
|
+
.then(() => {
|
|
36
|
+
done(true)
|
|
37
|
+
})
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
// reads file, updates all dependencies urls and write at destination
|
|
41
|
+
async function updateLinks(fileInfo){
|
|
42
|
+
var filepath = fileInfo.src[0]
|
|
43
|
+
|
|
44
|
+
try{
|
|
45
|
+
let stats = await lstat(filepath)
|
|
46
|
+
|
|
47
|
+
// not a file: ignore
|
|
48
|
+
if (!stats.isFile()){
|
|
49
|
+
// console.log(`${filepath} is not a file, ignore`)
|
|
50
|
+
return Promise.resolve()
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
let data = await readFile(filepath, {encoding: 'utf8'}),
|
|
54
|
+
result = replace(data)
|
|
55
|
+
|
|
56
|
+
// write update content to output file
|
|
57
|
+
fs.mkdirSync(path.dirname(fileInfo.dest), { recursive: true })
|
|
58
|
+
await writeFile(fileInfo.dest, result, 'utf8')
|
|
59
|
+
return Promise.resolve()
|
|
60
|
+
}
|
|
61
|
+
catch(err){
|
|
62
|
+
grunt.fail.warn(`${filepath}: ${err.message}`)
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
package/test/test.js
CHANGED
package/.npmignore
DELETED