express-react-views icon indicating copy to clipboard operation
express-react-views copied to clipboard

App with multiple `views`

Open ravinggenius opened this issue 7 years ago • 2 comments

I want to set the babel.only option without overwriting the remaining babel defaults. After looking through the source code, I found the only way to set babel.only was to copy the defaults and pass those through too.

server.engine('jsx', expressReact.createEngine({
	babel: {
		// glob includes all view directories in app, plus helper views like layout
		only: `${__dirname}/**/*.jsx`,
		presets: [
			'react',
			[
				'env',
				{
					targets: {
						node: 'current'
					}
				}
			]
		]
	}
}));

Would you be open to a pull request to add an extraPaths option? It would essentially change this line

assign({only: options.settings.views}, engineOptions.babel)

to

assign({only: [options.extraPaths, options.settings.views]}, engineOptions.babel)

This way I won't have to copy all the babel options just to set a custom/expanded babel.only.

ravinggenius avatar Feb 25 '18 18:02 ravinggenius

IIRC this should already work if using multiple views (since #109). options.settings.views is coming from express and might be an array. Or at least that used to be true. I think rather than thread more options through we should figure out why options.settings.views doesn't already contain the path you want. Would you be able to help work that out? If we need to pass more paths through in options, so be it but would be nice to avoid if possible.

zpao avatar Mar 03 '18 20:03 zpao

My files are grouped by domain instead of type (db access, routes, views for users are all together for instance). Each router sets a separate views directory (routes.set('views', path.join(__dirname, 'views'));), which is not know to the main app. The main app sets up the view engine:

server.engine('jsx', expressReact.createEngine({
	extraPaths: `${__dirname}/**/*.jsx`
}));

server.set('view engine', 'jsx');

I have to pass in the extra glob so layouts and shared components can be processed.

ravinggenius avatar Mar 04 '18 04:03 ravinggenius