Skip to content

Commit 748f823

Browse files
authored
Merge pull request #96 from BBVAEngineering/glob-for-sw-files
feat(config): new option 'importScriptsGlobPatterns'
2 parents f0c78e5 + 722c038 commit 748f823

File tree

5 files changed

+31
-13
lines changed

5 files changed

+31
-13
lines changed

README.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,21 @@ ENV['ember-cli-workbox'] = {
4545

4646
importScriptsTransform(importScripts) {
4747
return importScripts.map((importScript) => `https://example-cdn.com/${importScript}`);
48-
}
48+
},
49+
50+
importScriptsGlobPatterns: [
51+
'assets/service-workers/*.js'
52+
]
4953
};
5054
```
5155

52-
| Property | Type | Description |
53-
|:------------------------:|:----------:|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------:|
54-
| `enabled` | `Boolean` | Addon is enabled. Defaults `true` for production builds |
55-
| `debug` | `Boolean` | Log serviceworker states (registering, updating, etc) |
56-
| `autoRegister` | `Boolean` | Enable the sw registration before initializing the application |
57-
| `importScriptsTransform` | `Function` | Allows for transformation of array sent to workbox [importScripts](https://developers.google.com/web/tools/workbox/modules/workbox-build#generateSW-importScripts) |
56+
| Property | Type | Description |
57+
|:---------------------------:|:----------:|:------------------------------------------------------------------------------------------------------------------------------------------------------------------:|
58+
| `enabled` | `Boolean` | Addon is enabled. Defaults `true` for production builds |
59+
| `debug` | `Boolean` | Log serviceworker states (registering, updating, etc) |
60+
| `autoRegister` | `Boolean` | Enable the sw registration before initializing the application |
61+
| `importScriptsTransform` | `Function` | Allows for transformation of array sent to workbox [importScripts](https://developers.google.com/web/tools/workbox/modules/workbox-build#generateSW-importScripts) |
62+
| `importScriptsGlobPatterns` | `Array` | Define files that are going to be imported using [importScripts](https://developers.google.com/web/tools/workbox/modules/workbox-build#generateSW-importScripts) |
5863

5964
You can further customize ember-cli-workbox by setting **workbox configurations** in your `config/environment.js`:
6065

@@ -103,7 +108,7 @@ runtimeCaching: [
103108
]
104109
```
105110

106-
Note that `importScripts` parameter is overriden by this addon to include all js files on `/public/assets/service-workers/*` folder.
111+
Note that `importScripts` parameter is overriden by this addon to include all js files on `/public/assets/service-workers/*` folder. If you want to change this path use `importScriptsGlobPatterns` option.
107112

108113
> For more details on Workbox configuration take a look at: [Workbox Google Developers](https://developers.google.com/web/tools/workbox/reference-docs/latest/module-workbox-build).
109114

index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ module.exports = {
4444

4545
mergeOptions(options, {
4646
enabled: isProdBuild,
47-
debug: !isProdBuild
47+
debug: !isProdBuild,
48+
importScriptsGlobPatterns: [
49+
'assets/service-workers/*.js',
50+
]
4851
});
4952

5053
this.options = options;

lib/broccoli-workbox.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,13 @@ class BroccoliWorkbox extends Plugin {
8686
cleanPromise = cleanDir(workboxDirectory);
8787
}
8888

89-
const filesToIncludeInSW = glob.sync('assets/service-workers/*.js', {
90-
cwd: workboxOptions.globDirectory
91-
});
89+
const filesToIncludeInSW = this.options.importScriptsGlobPatterns.reduce((acc, pattern) => {
90+
const patterns = glob.sync(pattern, {
91+
cwd: workboxOptions.globDirectory
92+
});
93+
94+
return [...acc, ...patterns];
95+
}, []);
9296

9397
workboxOptions.importScripts = filesToIncludeInSW;
9498

node-tests/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ describe('Addon is enabled for production build', function() {
8282
});
8383

8484
it('precaches assets', () => {
85+
assertContains(outputFilePath('sw.js'), /engines-dist\/my-engine\/assets\/service-workers\/engine.js/);
8586
assertContains(outputFilePath('sw.js'), /assets\/service-workers\/skip-waiting.js/);
8687
assertContains(outputFilePath('sw.js'), /assets\/dummy\.[cjs|]/);
8788
assertContains(outputFilePath('sw.js'), /vendor\.[cjs|]/);
@@ -94,6 +95,11 @@ describe('Addon is enabled for production build', function() {
9495
assertContains(outputSWPath, /"assets\/service-workers\/skip-waiting.js"/);
9596
});
9697

98+
it('produces a sw engine file, which is imported on sw.js', () => {
99+
assertFileExists(outputFilePath('engines-dist/my-engine/assets/service-workers/engine.js'));
100+
assertContains(outputSWPath, /"engines-dist\/my-engine\/assets\/service-workers\/engine.js"/);
101+
});
102+
97103
it('applies importScriptsTransform', () => {
98104
assertContains(outputSWPath, process.env.IMPORT_SCRIPTS_PREFIX);
99105
});
@@ -144,4 +150,3 @@ describe('Addon is disabled for development', function() {
144150
});
145151
});
146152
});
147-
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('bar');

0 commit comments

Comments
 (0)