Skip to content

Commit

Permalink
fixes #19, fixes #21, removing existing source maps, also adding sour…
Browse files Browse the repository at this point in the history
…ce maps for combined output
  • Loading branch information
dbashford committed Dec 18, 2014
1 parent e1e1ac0 commit 0f7b799
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 45 deletions.
22 changes: 9 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@ The `'combine'` module configuration contains an array of `folders` that configu

By default, binary files, like images, are excluded from merging and this cannot be configured. Other exclusions can be added via the config, as can an order to the files get added.

If inline source maps are in a file that is being combined, they will be removed. The module has the ability to add its own source maps and it will add them by default during `mimosa watch`. Source maps are turned off during `mimosa build`.

When `mimosa build` is used, by default mimosa-combine cleans up the files it uses to build the combined file.

When `mimosa clean` or `mimosa watch` with the `--clean` flag is run, the `combine` module will clean up the files it has written.

# Config
# Default Config

```javascript
combine: {
sourceMap: true,
folders: [],
transforms:[],
removeCombined: {
Expand All @@ -36,7 +39,9 @@ combine: {
}
```

# Example Config
The default config is empty. This module will not do anything unless folders are configured.

# Example Folder Config

```javascript
combine: {
Expand All @@ -52,21 +57,12 @@ combine: {
return transformedText;
}
]
}],
transforms:[
function(inputText,inputName,outputName) {
// transform text
return transformedText;
}
],
removeCombined: {
enabled:true,
exclude:[]
}
}]
}
```

* `combine`: root for mimosa-config configuration
* `combine.sourceMap`: whether or not to generate source maps for combined files. This will not take into account any manipulations made by any transform functions, and it will not use existing source maps. Existing source maps are automatically stripped as they serve no purpose in a combined situation.
* `combine.folders`: array of folders to combine
* `combine.folders.folder`: a string, the path to the folder to combine. Path is relative to the watch config settings. Path can also be absolute.
* `combine.folders.output`: a string, the path to the output file result of the combine. Path is relative to the watch config settings. Path can also be absolute.
Expand Down
4 changes: 3 additions & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
exports.defaults = function() {
return {
combine: {
sourceMap: true,
transforms: [],
folders: [],
removeCombined: {
Expand All @@ -14,14 +15,15 @@ exports.defaults = function() {
};

exports.placeholder = function() {
return "\t\n\n combine:\n transforms:[] # an array of transform functions to use to alter files\n # before they are combined\n folders: [] # Configuration for folder combining. See\n # https://github.com/dbashford/mimosa-combine for details on how to set up\n # entries in the folders array\n removeCombined: # configuration for removing combined files\n enabled:true # when set to true, during 'mimosa build' only, mimosa-combine will remove\n # the files that were merged into single files\n exclude:[] # mimosa-combine will not remove any of these files.\n";
return "\t\n\n combine:\n sourceMap: true # whether or not to generate a source map during developmen\n # (mimosa watch, not build)\n transforms:[] # an array of transform functions to use to alter files\n # before they are combined\n folders: [] # Configuration for folder combining. See\n # https://github.com/dbashford/mimosa-combine for details on how to set up\n # entries in the folders array\n removeCombined: # configuration for removing combined files\n enabled:true # when set to true, during 'mimosa build' only, mimosa-combine will remove\n # the files that were merged into single files\n exclude:[] # mimosa-combine will not remove any of these files.\n";
};

exports.validate = function(config, validators) {
var combine, combines, errorStart, errors, transform, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2, _ref3;
errors = [];
errorStart = "combine.folders";
if (validators.ifExistsIsObject(errors, "combine", config.combine)) {
validators.ifExistsIsBoolean(errors, "combine.sourceMap", config.combine.sourceMap);
if (validators.ifExistsIsArray(errors, "combine.transforms", config.combine.transforms)) {
_ref = config.combine.transforms;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
Expand Down
48 changes: 33 additions & 15 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"use strict";
var config, fs, logger, path, registration, wrench, _, __cleanUpDirectories, __getEncoding, __getFileText, __mergeDirectory, __processIncludeExclude, __removeAllCombined, __transformText, _checkForMerge, _cleanCombined, _mergeAll;
var MagicString, config, fs, logger, path, registration, sourceMap, wrench, _, __cleanUpDirectories, __getEncoding, __getFileText, __mergeDirectory, __processIncludeExclude, __removeAllCombined, __transformText, _checkForMerge, _cleanCombined, _mergeAll;

fs = require("fs");

Expand All @@ -9,6 +9,10 @@ wrench = require('wrench');

_ = require('lodash');

MagicString = new require('magic-string');

sourceMap = require("convert-source-map");

config = require('./config');

logger = null;
Expand Down Expand Up @@ -65,7 +69,7 @@ _checkForMerge = function(mimosaConfig, options, next) {
}
}
if (doit) {
__mergeDirectory(combine);
__mergeDirectory(mimosaConfig, combine);
}
}
}
Expand All @@ -80,7 +84,7 @@ _mergeAll = function(mimosaConfig, options, next) {
_ref = mimosaConfig.combine.folders;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
combine = _ref[_i];
files = __mergeDirectory(combine);
files = __mergeDirectory(mimosaConfig, combine);
combinedFiles.push.apply(combinedFiles, files);
}
if (mimosaConfig.isBuild && mimosaConfig.combine.removeCombined.enabled) {
Expand All @@ -92,6 +96,7 @@ _mergeAll = function(mimosaConfig, options, next) {

__transformText = function(folderCombineConfig, inputFileName, inputFileText) {
var outputFileName, transform, transformedText, transforms, _i, _len;
inputFileText = sourceMap.removeComments(inputFileText);
transforms = folderCombineConfig.transforms;
if (transforms && transforms.length) {
outputFileName = folderCombineConfig.output;
Expand Down Expand Up @@ -134,8 +139,8 @@ __processIncludeExclude = function(combine, folderFiles) {
return folderFiles;
};

__mergeDirectory = function(combine) {
var addFileText, folderFiles, outputFileText, removeFiles;
__mergeDirectory = function(mimosaConfig, combine) {
var addFileText, bundle, bundleOutput, comment, converter, folderFiles, map, removeFiles;
if (!fs.existsSync(combine.folder)) {
return logger.warn("mimosa-combine: combine folder [[ " + combine.folder + " ]] does not exist");
}
Expand All @@ -159,14 +164,21 @@ __mergeDirectory = function(combine) {
logger.info("mimosa-combine: there are no files to combine for configuration");
return [];
}
outputFileText = "";
removeFiles = [];
bundle = new MagicString.Bundle();
addFileText = function(fileName) {
var fileText, transformedText;
var appendText, fileText, transformedText;
fileText = __getFileText(fileName);
if (fileText) {
transformedText = __transformText(combine, fileName, fileText);
return outputFileText += transformedText != null ? transformedText : "";
appendText = "\n\n";
if (path.extname(fileName) === ".js") {
appendText = ";\n";
}
return bundle.addSource({
filename: fileName,
content: new MagicString(transformedText != null ? transformedText : "").append(appendText)
});
} else {
return removeFiles.push(fileName);
}
Expand All @@ -187,7 +199,18 @@ __mergeDirectory = function(combine) {
wrench.mkdirSyncRecursive(path.dirname(combine.output), 0x1ff);
}
logger.success("mimosa-combine: wrote combined file [[ " + combine.output + " ]]");
fs.writeFileSync(combine.output, outputFileText);
bundleOutput = bundle.toString();
if (mimosaConfig.combine.sourceMap && !mimosaConfig.isBuild) {
map = bundle.generateMap({
file: combine.output,
includeContent: true,
hires: true
});
converter = sourceMap.fromObject(map);
comment = converter.toComment();
bundleOutput += "\n/*#" + comment.slice(3) + "*/";
}
fs.writeFileSync(combine.output, bundleOutput);
if (combine.order != null) {
folderFiles = folderFiles.concat(combine.order);
}
Expand Down Expand Up @@ -251,15 +274,10 @@ __getFileText = function(fileName, text) {
var fileText;
fileText = fs.readFileSync(fileName);
if (__getEncoding(fileText) !== 'binary') {
fileText = fileText.toString('utf8').trim();
if (logger.isDebug()) {
logger.debug("Adding [[ " + fileName + " ]] to output");
}
if (path.extname(fileName) === ".js") {
return fileText + ";\n";
} else {
return fileText + "\n\n";
}
return fileText.toString('utf8').trim();
} else {
if (logger.isDebug()) {
logger.debug("NOT adding [[ " + fileName + " ]] to output");
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mimosa-combine",
"version": "2.1.0",
"version": "3.0.0",
"homepage": "https://github.com/dbashford/mimosa-combine",
"author": "David Bashford",
"description": "A Mimosa folder combining module",
Expand All @@ -22,7 +22,9 @@
],
"dependencies": {
"wrench": "1.5.8",
"lodash": "2.4.1"
"lodash": "2.4.1",
"magic-string":"0.2.4",
"convert-source-map":"0.4.1"
},
"license": "MIT",
"engines": {
Expand Down
7 changes: 6 additions & 1 deletion src/config.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

exports.defaults = ->
combine:
sourceMap: true
transforms:[]
folders: []
removeCombined:
Expand All @@ -14,6 +15,8 @@ exports.placeholder = ->
\t
combine:
sourceMap: true # whether or not to generate a source map during developmen
# (mimosa watch, not build)
transforms:[] # an array of transform functions to use to alter files
# before they are combined
folders: [] # Configuration for folder combining. See
Expand All @@ -33,6 +36,8 @@ exports.validate = (config, validators) ->

if validators.ifExistsIsObject(errors, "combine", config.combine)

validators.ifExistsIsBoolean(errors, "combine.sourceMap", config.combine.sourceMap)

if validators.ifExistsIsArray(errors, "combine.transforms", config.combine.transforms)
for transform in config.combine.transforms
if Object.prototype.toString.call(transform) isnt '[object Function]'
Expand Down Expand Up @@ -75,7 +80,7 @@ exports.validate = (config, validators) ->
errors.push "Installed version of Mimosa does not support combine.folders.include. Need Mimosa version 2.3.22 for this feature. You may want to use older version of mimosa-combine."

continue if errors.length > 0

combine.transforms = (combine.transforms ? []).concat( config.combine.transforms ? [] )

else
Expand Down
45 changes: 32 additions & 13 deletions src/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ path = require "path"

wrench = require 'wrench'
_ = require 'lodash'
MagicString = new require('magic-string')

sourceMap = require("convert-source-map")
config = require './config'

logger = null
Expand Down Expand Up @@ -46,14 +48,14 @@ _checkForMerge = (mimosaConfig, options, next) ->
doit = true

if doit
__mergeDirectory combine
__mergeDirectory mimosaConfig, combine

next()

_mergeAll = (mimosaConfig, options, next) ->
combinedFiles = []
for combine in mimosaConfig.combine.folders
files = __mergeDirectory combine
files = __mergeDirectory mimosaConfig, combine
combinedFiles.push files...

if mimosaConfig.isBuild and mimosaConfig.combine.removeCombined.enabled
Expand All @@ -63,6 +65,11 @@ _mergeAll = (mimosaConfig, options, next) ->
next()

__transformText = (folderCombineConfig, inputFileName, inputFileText) ->
# remove source maps if they are there
# the fact that we are combining means
# source maps won't work at all
inputFileText = sourceMap.removeComments inputFileText

transforms = folderCombineConfig.transforms
if transforms and transforms.length
outputFileName = folderCombineConfig.output
Expand Down Expand Up @@ -99,7 +106,7 @@ __processIncludeExclude = (combine, folderFiles) ->

folderFiles

__mergeDirectory = (combine) ->
__mergeDirectory = (mimosaConfig, combine) ->
unless fs.existsSync combine.folder
return logger.warn "mimosa-combine: combine folder [[ #{combine.folder} ]] does not exist"

Expand All @@ -122,17 +129,23 @@ __mergeDirectory = (combine) ->
logger.info "mimosa-combine: there are no files to combine for configuration"
return []

outputFileText = ""
removeFiles = []
bundle = new MagicString.Bundle();

addFileText = (fileName) ->
fileText = __getFileText fileName
if fileText
transformedText = __transformText combine, fileName, fileText
outputFileText += transformedText ? ""
appendText = "\n\n"
if path.extname(fileName) is ".js"
appendText = ";\n"
bundle.addSource
filename: fileName,
content: new MagicString(transformedText ? "").append(appendText)
else
removeFiles.push fileName


if combine.order?
folderFiles = _.difference(folderFiles, combine.order)
combine.order.forEach (orderFile) ->
Expand All @@ -148,7 +161,19 @@ __mergeDirectory = (combine) ->
wrench.mkdirSyncRecursive path.dirname(combine.output), 0o0777

logger.success "mimosa-combine: wrote combined file [[ #{combine.output} ]]"
fs.writeFileSync combine.output, outputFileText

bundleOutput = bundle.toString()
if mimosaConfig.combine.sourceMap and not mimosaConfig.isBuild
map = bundle.generateMap
file: combine.output
includeContent: true
hires: true

converter = sourceMap.fromObject(map)
comment = converter.toComment()
bundleOutput += "\n/*#" + comment.slice(3) + "*/"

fs.writeFileSync combine.output, bundleOutput

# rejoin order files for possible removal
if combine.order?
Expand Down Expand Up @@ -197,15 +222,9 @@ __cleanUpDirectories = (folders) ->
__getFileText = (fileName, text) ->
fileText = fs.readFileSync fileName
if __getEncoding(fileText) isnt 'binary'
fileText = fileText.toString('utf8').trim()

if logger.isDebug()
logger.debug "Adding [[ #{fileName} ]] to output"

if path.extname(fileName) is ".js"
fileText + ";\n"
else
fileText + "\n\n"
fileText.toString('utf8').trim()
else
if logger.isDebug()
logger.debug "NOT adding [[ #{fileName} ]] to output"
Expand Down

0 comments on commit 0f7b799

Please sign in to comment.