From 2526b6916f676905c8fcccc6a9b830258c5ab073 Mon Sep 17 00:00:00 2001 From: Karl Horky Date: Wed, 9 Dec 2015 15:28:33 +0100 Subject: [PATCH 1/4] Add option for outputFilename --- README.md | 6 ++---- index.js | 9 +++++---- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 767d22c..d48d76d 100644 --- a/README.md +++ b/README.md @@ -20,10 +20,8 @@ var blessPlugin = require('bless-webpack-plugin'); ``` ## API -### `blessPlugin([blessOptions[, pattern]])` +### `blessPlugin([blessOptions[, pattern[, outputFilename]]])` - `blessOptions` is an options object for bless. It will be passed directly to it. - `pattern` a regular expression to find assets that should be transformed with bless. Default: `/\.css$/`. - - - +- `outputFilename` a different filename for the blessed css files. If set, the original (unblessed) files will be retained. diff --git a/index.js b/index.js index e51ccb2..391ca65 100644 --- a/index.js +++ b/index.js @@ -1,8 +1,7 @@ var bless = require('bless'); var RawSource = require('webpack/lib/RawSource'); -var _ = require('lodash'); -module.exports = function(options, pattern) { +module.exports = function(options, pattern, outputFilename) { pattern = pattern || /\.css$/; options = options || {}; @@ -26,13 +25,15 @@ module.exports = function(options, pattern) { .filter(pattern.test.bind(pattern)) .forEach(function(name) { pending++; - new bless.Parser({ output: name, options : options }) + new bless.Parser({ output: outputFilename || name, options : options }) .parse(assets[name].source(), function(err, files) { if (err) { done(err); return; } - delete assets[name]; + if (!outputFilename) { + delete assets[name]; + } files.forEach(function(file) { assets[file.filename] = new RawSource(file.content); }); From e555f9c3bd1aad002ca51e566500e9ab0996b175 Mon Sep 17 00:00:00 2001 From: Karl Horky Date: Fri, 11 Dec 2015 00:37:28 +0100 Subject: [PATCH 2/4] Add variables to outputFilename parameter --- README.md | 4 +++- index.js | 12 +++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d48d76d..c22a21c 100644 --- a/README.md +++ b/README.md @@ -24,4 +24,6 @@ var blessPlugin = require('bless-webpack-plugin'); - `blessOptions` is an options object for bless. It will be passed directly to it. - `pattern` a regular expression to find assets that should be transformed with bless. Default: `/\.css$/`. -- `outputFilename` a different filename for the blessed css files. If set, the original (unblessed) files will be retained. +- `outputFilename` a filename for the blessed css files. If set, the original (unblessed) files will also be emitted. The following variables can be used: + - `[filebase]` The base name of the asset, without path or extension + - `[file]` The full name of the asset, including path and extension diff --git a/index.js b/index.js index 391ca65..354ec5a 100644 --- a/index.js +++ b/index.js @@ -4,12 +4,13 @@ var RawSource = require('webpack/lib/RawSource'); module.exports = function(options, pattern, outputFilename) { pattern = pattern || /\.css$/; options = options || {}; + outputFilename = outputFilename || '[file]'; return { apply: function(compiler) { compiler.plugin("this-compilation", function(compilation) { compilation.plugin("optimize-assets", function(assets, callback) { - var pending = 0; + var pending = 0, basename, output; function done(err) { pending--; @@ -25,13 +26,18 @@ module.exports = function(options, pattern, outputFilename) { .filter(pattern.test.bind(pattern)) .forEach(function(name) { pending++; - new bless.Parser({ output: outputFilename || name, options : options }) + basename = name.substr(name.lastIndexOf('/') + 1, name.lastIndexOf('.')); + output = compilation.getPath(outputFilename, { + filename: name, + basename: basename + }); + new bless.Parser({ output: output, options : options }) .parse(assets[name].source(), function(err, files) { if (err) { done(err); return; } - if (!outputFilename) { + if (outputFilename === name) { delete assets[name]; } files.forEach(function(file) { From da1b1b2f5661a5bf49f6294619347c84614c2e8f Mon Sep 17 00:00:00 2001 From: Karl Horky Date: Fri, 11 Dec 2015 00:56:19 +0100 Subject: [PATCH 3/4] Fix error when no extension present --- index.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 354ec5a..c0fec12 100644 --- a/index.js +++ b/index.js @@ -26,7 +26,10 @@ module.exports = function(options, pattern, outputFilename) { .filter(pattern.test.bind(pattern)) .forEach(function(name) { pending++; - basename = name.substr(name.lastIndexOf('/') + 1, name.lastIndexOf('.')); + basename = name.substring(name.lastIndexOf('/') + 1); + if (basename.lastIndexOf('.') !== -1) { + basename = basename.substring(0, basename.lastIndexOf('.')); + } output = compilation.getPath(outputFilename, { filename: name, basename: basename From 3d0d925fe9e7326d93f8f2d17161f28756fcaea6 Mon Sep 17 00:00:00 2001 From: Karl Horky Date: Wed, 16 Dec 2015 16:01:52 +0100 Subject: [PATCH 4/4] Use path.basename() from node --- index.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index c0fec12..6bda5e1 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,6 @@ var bless = require('bless'); var RawSource = require('webpack/lib/RawSource'); +var path = require('path'); module.exports = function(options, pattern, outputFilename) { pattern = pattern || /\.css$/; @@ -26,10 +27,7 @@ module.exports = function(options, pattern, outputFilename) { .filter(pattern.test.bind(pattern)) .forEach(function(name) { pending++; - basename = name.substring(name.lastIndexOf('/') + 1); - if (basename.lastIndexOf('.') !== -1) { - basename = basename.substring(0, basename.lastIndexOf('.')); - } + basename = path.basename(name, path.extname(name)); output = compilation.getPath(outputFilename, { filename: name, basename: basename