Skip to content

Commit

Permalink
Replace locals behaviour with transformLocals behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
jahredhope committed Mar 26, 2018
1 parent 81d44ec commit 9438bb7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 49 deletions.
51 changes: 23 additions & 28 deletions README.md
Expand Up @@ -101,14 +101,33 @@ Note that this will still be executed for each entry in your `paths` array in yo
// The path currently being rendered:
locals.path;

// An object containing all assets:
locals.assets;

// Advanced: Webpack's stats object:
locals.webpackStats;
```

Any additional locals provided in your config are also available.
## Custom locals

To customise the locals provided to your render function you can use the `locals` option.
A function passed to this option will be called before each render.
The result will be used when rendering.

```js
const template = ejs.compile(templateSource);

module.exports = {

...

plugins: [
new StaticSiteGeneratorPlugin({
locals: ({ path }) => {
template,
path
}
})
]
}
```

## Crawl mode

Expand Down Expand Up @@ -250,30 +269,6 @@ module.exports = {
};
```

## Customise locals

This plugin will pass default and user provided locals directly to the render function.

If required you can pass a function to manipulate these locals with the `transformLocals` option.
The function will be ran for each rendered path and the output will be passed into the render function.

```js
module.exports = {
...,
plugins: [
new StaticSiteGeneratorPlugin({
transformLocals: (data) => {
const assets = Object.keys(data.webpackStats.compilation.assets);
const css = assets.filter(value => value.match(/\.css$/));
const js = assets.filter(value => value.match(/\.js$/));
return ({ css, js, ...data});
}
})
})
]
}
```

## Related projects

- [react-router-to-array](https://github.com/alansouzati/react-router-to-array) - useful for avoiding hardcoded lists of routes to render
Expand Down
48 changes: 30 additions & 18 deletions index.js
Expand Up @@ -5,17 +5,20 @@ var cheerio = require('cheerio');
var url = require('url');
var Promise = require('bluebird');

function defaultLocalsTransform(locals) {
return locals;
}

function StaticSiteGeneratorWebpackPlugin(options) {
if (arguments.length > 1) {
options = legacyArgsToOptions.apply(null, arguments);
}

options = options || {};
options = normalizeOptions(options);

this.entry = options.entry;
this.paths = Array.isArray(options.paths) ? options.paths : [options.paths || '/'];
this.locals = options.locals;
this.transformLocals = options.transformLocals;
this.locals = options.locals || defaultLocalsTransform;
this.globals = options.globals;
this.crawl = Boolean(options.crawl);
}
Expand Down Expand Up @@ -50,7 +53,7 @@ StaticSiteGeneratorWebpackPlugin.prototype.apply = function(compiler) {
throw new Error('Export from "' + self.entry + '" must be a function that returns an HTML string. Is output.libraryTarget in the configuration set to "umd"?');
}

renderPaths(self.crawl, self.locals, self.paths, self.transformLocals, render, assets, webpackStats, compilation)
renderPaths(self.crawl, self.locals, self.paths, render, assets, webpackStats, compilation)
.nodeify(done);
} catch (err) {
compilation.errors.push(err.stack);
Expand All @@ -60,23 +63,13 @@ StaticSiteGeneratorWebpackPlugin.prototype.apply = function(compiler) {
});
};

function renderPaths(crawl, userLocals, paths, transformLocals, render, assets, webpackStats, compilation) {
function renderPaths(crawl, transformLocals, paths, render, assets, webpackStats, compilation) {
var renderPromises = paths.map(function(outputPath) {
var locals = {
var locals = transformLocals({
path: outputPath,
assets: assets,
webpackStats: webpackStats
};

for (var prop in userLocals) {
if (userLocals.hasOwnProperty(prop)) {
locals[prop] = userLocals[prop];
}
}

if (transformLocals) {
locals = transformLocals(locals);
}
});

var renderPromise = render.length < 2 ?
Promise.resolve(render(locals)) :
Expand All @@ -102,7 +95,7 @@ function renderPaths(crawl, userLocals, paths, transformLocals, render, assets,
path: key
});

return renderPaths(crawl, userLocals, relativePaths, transformLocals, render, assets, webpackStats, compilation);
return renderPaths(crawl, transformLocals, relativePaths, render, assets, webpackStats, compilation);
}
});

Expand Down Expand Up @@ -220,6 +213,25 @@ function relativePathsFromHtml(options) {
});
}

function normalizeOptions(legacyOptions) {
var options = Object.assign({}, legacyOptions);

if (options.locals && typeof options.locals !== 'function') {
var userLocals = options.locals;
options.locals = (defaultLocals) => {
return Object.assign(
{
webpackStats: defaultLocals.webpackStats,
path: defaultLocals.path,
assets: defaultLocals.assets
},
userLocals
);
};
}
return options;
}

function legacyArgsToOptions(entry, paths, locals, globals) {
return {
entry: entry,
Expand Down
6 changes: 3 additions & 3 deletions test/success-cases/transform-locals/webpack.config.js
Expand Up @@ -20,9 +20,9 @@ module.exports = {
plugins: [
new StaticSiteGeneratorPlugin({
entry: 'main',
transformLocals: locals => ({
chunks: Object.keys(locals.webpackStats.compilation.assets).map(
file => `${locals.webpackStats.compilation.outputOptions.publicPath}${file}`
locals: ({ path, webpackStats }) => ({
chunks: Object.keys(webpackStats.compilation.assets).map(
file => `${webpackStats.compilation.outputOptions.publicPath}${file}`
)
})
})
Expand Down

0 comments on commit 9438bb7

Please sign in to comment.