Skip to content

Commit 468578e

Browse files
Enabled addons using netlifyRedirects()
1 parent ac5fafe commit 468578e

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ appended to the existing files. Currently, only redirects are supported, not hea
4242
}
4343
```
4444

45+
### Using in an addon
46+
47+
Addon authors may declare redirects for ember-cli-netlify during compilation. To do so, you will want to:
48+
49+
* Add `"ember-cli-netlify-plugin"` to your addon's package.json keywords array.
50+
* Define a `netlifyRedirects()` function in your addon's main file, that returns an array of redirects.
51+
* Advise your addon's users to install & configure `ember-cli-netlify` in the host application.
52+
4553

4654
Contributing
4755
------------------------------------------------------------------------------

index.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module.exports = {
66
name: require('./package').name,
77
outputReady() {
88
const netlifyOptions = this.app.options['ember-cli-netlify'];
9+
const redirectsFromPlugins = loadNetlifyRedirects(this);
910

1011
if (fs.pathExistsSync('.netlifyheaders')) {
1112
fs.copySync('.netlifyheaders', 'dist/_headers', { clobber: true });
@@ -17,12 +18,29 @@ module.exports = {
1718
fs.writeFileSync('dist/_redirects');
1819
}
1920

21+
const stream = fs.createWriteStream('dist/_redirects', { flags: 'a' });
22+
2023
if (netlifyOptions && netlifyOptions.redirects) {
21-
const stream = fs.createWriteStream('dist/_redirects', { flags: 'a' });
2224
netlifyOptions.redirects.forEach((redirect) => {
2325
stream.write(`${redirect}\n`);
2426
});
25-
stream.end();
2627
}
28+
29+
if (redirectsFromPlugins && redirectsFromPlugins.length) {
30+
redirectsFromPlugins.forEach((redirect) => {
31+
stream.write(`${redirect}\n`);
32+
});
33+
}
34+
35+
stream.end();
2736
}
2837
};
38+
39+
function loadNetlifyRedirects(context) {
40+
const addons = context.project.addons || [];
41+
42+
return addons
43+
.filter((addon) => addon.pkg.keywords.includes('ember-cli-netlify-plugin'))
44+
.filter((addon) => typeof addon.netlifyRedirects === 'function')
45+
.map((addon) => addon.netlifyRedirects.bind(addon));
46+
}

0 commit comments

Comments
 (0)