node-sass-middleware icon indicating copy to clipboard operation
node-sass-middleware copied to clipboard

Custom Source filename

Open crephix opened this issue 8 years ago • 2 comments
trafficstars

Hey, I am using:

		app.use(
			sass({
				src: path.join(__dirname, '/common/sass'),
				dest: path.join(__dirname, '../wwwroot/stylesheets'),
				prefix: '/stylesheets',
			})
		)

but it will only pick up the sass file if I call it "style.scss" instead of "bootstrap.scss" for example. Is there a way to specify the source filename? I'd like to keep it named bootstrap.scss instead of style.scss.

Same question applies to the dest filename, can I give it a custom name which differs from the source css filename?

crephix avatar May 11 '17 11:05 crephix

Hey @crephix. Thanks for reaching out! 🌮

At the moment, we only replace the .css extension with .scss from requested path to deduce the source filename.

What do you think if we extend the API by delegating this control to the consumer though a user-defined source-file provider function? Something like:

sassMiddlewareOptions = {
  sourceFileProvider: (requestContext) => getSourceFilenameFor ( requestContext.requestedPath );
};

// as an alternative to:

sassMiddlewareOptions = {
  src: path.join(__dirname, '/common/sass'),
  dest: path.join(__dirname, '../wwwroot/stylesheets'),
  prefix: '/stylesheets',
};

where middleware will inject the consumer's initial options and request objects to the requestContext and consumer will return an absolute path to .s[ac]ss file (String).

Additionally, we can also extend this approach to in-memory strings compilation, which node-sass provides OOTB:

sassMiddlewareOptions = {
  sourceProvider: (requestContext) => getSourceFor ( requestContext.requestedPath );
};

here in getSourceFor(..) function, consumer might be reading source content from (for instance) a remote server based on the requested path. With these options, middleware will asyncronously proceed once the source provider function returns the source contents or path to compile.

am11 avatar May 11 '17 18:05 am11

Sounds like a great enhancement for me.

crephix avatar May 12 '17 09:05 crephix