Skip to content

Commit 944396d

Browse files
Merge pull request #8 from shipshapecode/config-headers
Allow passing additional redirects in the config
2 parents 79b8a41 + 72f576c commit 944396d

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,37 @@ ember install ember-cli-netlify
2121
Usage
2222
------------------------------------------------------------------------------
2323

24-
Just define a `.netlifyheaders` and/or a `.netlifyredirects` file in the root of your project and this
24+
### .netlifyheaders and/or .netlifyredirects
25+
26+
There are a few ways to use this addon. The first one is to define a `.netlifyheaders`
27+
and/or a `.netlifyredirects` file in the root of your project and this
2528
addon will copy the output to `dist/_headers` and `dist/_redirects` respectively.
2629

30+
### ember-cli-build.js
31+
32+
The second way is to define an `ember-cli-netlify` hash in your `ember-cli-build.js`.
33+
You can combine these methods, and anything defined in the config hash will be
34+
appended to the existing files. Currently, only redirects are supported, not headers.
35+
36+
```js
37+
'ember-cli-netlify': {
38+
redirects: [
39+
'https://blog.shipshape.io/* https://shipshape.io/blog/:splat 301!',
40+
'https://blog.shipshape.io/* https://shipshape.io/blog/:splat 301!'
41+
]
42+
}
43+
```
44+
45+
### Using in an addon
46+
47+
The final option, for addon authors, is to declare redirects for ember-cli-netlify during compilation.
48+
To do so, you will want to:
49+
50+
* Add `"ember-cli-netlify-plugin"` to your addon's package.json keywords array.
51+
* Define a `netlifyRedirects()` function in your addon's main file, that returns an array of redirects.
52+
* Advise your addon's users to install & configure `ember-cli-netlify` in the host application.
53+
54+
2755
Contributing
2856
------------------------------------------------------------------------------
2957

index.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,46 @@ const fs = require('fs-extra');
55
module.exports = {
66
name: require('./package').name,
77
outputReady() {
8+
const netlifyOptions = this.app.options['ember-cli-netlify'];
9+
const pluginRedirectFunctions = loadNetlifyRedirects(this);
10+
11+
let redirectsFromPlugins = pluginRedirectFunctions.reduce((redirects, pluginRedirectFunction) => {
12+
return redirects.concat(pluginRedirectFunction());
13+
}, []);
14+
815
if (fs.pathExistsSync('.netlifyheaders')) {
916
fs.copySync('.netlifyheaders', 'dist/_headers', { clobber: true });
1017
}
1118

1219
if (fs.pathExistsSync('.netlifyredirects')) {
1320
fs.copySync('.netlifyredirects', 'dist/_redirects', { clobber: true });
21+
} else if (netlifyOptions && netlifyOptions.redirects) {
22+
fs.writeFileSync('dist/_redirects');
23+
}
24+
25+
const stream = fs.createWriteStream('dist/_redirects', { flags: 'a' });
26+
27+
if (netlifyOptions && netlifyOptions.redirects) {
28+
netlifyOptions.redirects.forEach((redirect) => {
29+
stream.write(`${redirect}\n`);
30+
});
1431
}
32+
33+
if (redirectsFromPlugins && redirectsFromPlugins.length) {
34+
redirectsFromPlugins.forEach((redirect) => {
35+
stream.write(`${redirect}\n`);
36+
});
37+
}
38+
39+
stream.end();
1540
}
1641
};
42+
43+
function loadNetlifyRedirects(context) {
44+
const addons = context.project.addons || [];
45+
46+
return addons
47+
.filter((addon) => addon.pkg.keywords.includes('ember-cli-netlify-plugin'))
48+
.filter((addon) => typeof addon.netlifyRedirects === 'function')
49+
.map((addon) => addon.netlifyRedirects.bind(addon));
50+
}

0 commit comments

Comments
 (0)