Skip to content

Commit

Permalink
feat(config): new option 'importScriptsGlobPatterns'
Browse files Browse the repository at this point in the history
  • Loading branch information
josex2r committed Jun 16, 2020
1 parent f0c78e5 commit 722c038
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 13 deletions.
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,21 @@ ENV['ember-cli-workbox'] = {

importScriptsTransform(importScripts) {
return importScripts.map((importScript) => `https://example-cdn.com/${importScript}`);
}
},

importScriptsGlobPatterns: [
'assets/service-workers/*.js'
]
};
```

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

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

Expand Down Expand Up @@ -103,7 +108,7 @@ runtimeCaching: [
]
```

Note that `importScripts` parameter is overriden by this addon to include all js files on `/public/assets/service-workers/*` folder.
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.

> 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).
Expand Down
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ module.exports = {

mergeOptions(options, {
enabled: isProdBuild,
debug: !isProdBuild
debug: !isProdBuild,
importScriptsGlobPatterns: [
'assets/service-workers/*.js',
]
});

this.options = options;
Expand Down
10 changes: 7 additions & 3 deletions lib/broccoli-workbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,13 @@ class BroccoliWorkbox extends Plugin {
cleanPromise = cleanDir(workboxDirectory);
}

const filesToIncludeInSW = glob.sync('assets/service-workers/*.js', {
cwd: workboxOptions.globDirectory
});
const filesToIncludeInSW = this.options.importScriptsGlobPatterns.reduce((acc, pattern) => {
const patterns = glob.sync(pattern, {
cwd: workboxOptions.globDirectory
});

return [...acc, ...patterns];
}, []);

workboxOptions.importScripts = filesToIncludeInSW;

Expand Down
7 changes: 6 additions & 1 deletion node-tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ describe('Addon is enabled for production build', function() {
});

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

it('produces a sw engine file, which is imported on sw.js', () => {
assertFileExists(outputFilePath('engines-dist/my-engine/assets/service-workers/engine.js'));
assertContains(outputSWPath, /"engines-dist\/my-engine\/assets\/service-workers\/engine.js"/);
});

it('applies importScriptsTransform', () => {
assertContains(outputSWPath, process.env.IMPORT_SCRIPTS_PREFIX);
});
Expand Down Expand Up @@ -144,4 +150,3 @@ describe('Addon is disabled for development', function() {
});
});
});

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('bar');

0 comments on commit 722c038

Please sign in to comment.