Skip to content

Commit

Permalink
feat(webpack): render for each locale (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattcompiles committed Jul 17, 2017
1 parent 03e9039 commit c4b2a98
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,39 @@ module.exports = {
}
```

### Locales

Often we render multiple versions of our application for different locations, eg. Australia & New Zealand. To render an HTML file for each location you can use the locales option in `sku.config.js`. Locales are preferable to [monorepos](#monorepo-support) when you need to render multiple versions of your HTML file but only need one version of each of the assets (JS, CSS, images, etc). Note: You can use `locales` inside a monorepo project.

The `locales` option accepts an array of strings representing each locale you want to render HTML files for.

```js
module.exports = {
locales: ['AU', 'NZ']
}
```

For each locale, sku will call your `render.js` function and pass it the locale as a parameter.

```js
const render = ({ locale }) => (
`<div>Rendered for ${locale}</div>`
)
```

The name of the HTML file that is generated will be suffixed by `-{locale}`.

eg.
```js
module.exports = {
locales: ['AU', 'NZ']
}
```

will create `index-AU.html` & `index-NZ.html`.

Note: When running the app in dev mode only one HTML file will be created, defaulting to the first listed locale.

### Monorepo Support

If you need to build multiple projects in the same repo, you can provide an array of config objects.
Expand Down
4 changes: 3 additions & 1 deletion config/builds.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const builds = buildConfigs
const name = buildConfig.name || '';
const env = buildConfig.env || {};
const entry = buildConfig.entry || {};
const locales = buildConfig.locales || [''];

const paths = {
seekStyleGuide: path.join(cwd, 'node_modules/seek-style-guide'),
Expand All @@ -49,7 +50,8 @@ const builds = buildConfigs
return {
name,
env,
paths
paths,
locales
};
});

Expand Down
14 changes: 12 additions & 2 deletions config/webpack/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ const svgLoaders = [
}
];

const buildWebpackConfigs = builds.map(({ name, paths, env }) => {
const buildWebpackConfigs = builds.map(({ name, paths, env, locales }) => {
const envVars = lodash
.chain(env)
.mapValues((value, key) => {
Expand Down Expand Up @@ -222,7 +222,17 @@ const buildWebpackConfigs = builds.map(({ name, paths, env }) => {
},
plugins: [
new webpack.DefinePlugin(envVars),
new StaticSiteGeneratorPlugin()
...locales.slice(0, isProductionBuild ? locales.length : 1).map(
locale =>
new StaticSiteGeneratorPlugin({
locals: {
locale
},
paths: `index${isProductionBuild && locale
? `-${locale}`
: ''}.html`
})
)
]
}
];
Expand Down

0 comments on commit c4b2a98

Please sign in to comment.