Skip to content

Commit

Permalink
fixes #21, adding module config, tweaking default r.js config when mo…
Browse files Browse the repository at this point in the history
…dule config provided.
  • Loading branch information
dbashford committed Sep 22, 2013
1 parent b9e1c44 commit 54a1cd8
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 29 deletions.
10 changes: 9 additions & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ exports.defaults = function() {
},
optimize: {
inferConfig: true,
modules: null,
overrides: {}
}
}
};
};

exports.placeholder = function() {
return "\t\n\n # require: # configuration for requirejs options.\n # exclude:[] # Regex or string paths. Paths can be absolute or relative to the\n # watch.javascriptDir. These files will be excluded from all\n # require module functionality. That includes AMD verification and\n # being considered a root level file to be optimized.\n # commonConfig: \"common\" # The path from 'javascriptDir' to the location of common requirejs\n # config. This is config shared across multiple requirejs modules.\n # This should be either a require.config({}) or a requirejs.config({}) function call.\n # Defaults to the value `common` - referring to a file named common.js in the root of javascriptDir.\n # Does not need to exist, so can be left alone if a commonConfig is not\n # being used.\n # tracking: # every time mimosa starts up, mimosa-require needs to be able to\n # build a dependency graph for the codebase. It can do that by\n # processing all the files, but that means each file needs to be\n # processed when mimosa watch starts which slows down startup.\n # tracking allows mimosa-require to write interim state to the file\n # system so that from one mimosa run to another it can persist the\n # important information and not need the entire application to be\n # rebuilt\n # enabled: true # whether or not tracking is enabled\n # path: \".mimosa/require/tracking.json\" # the path to the tracking file relative to the\n # root of the project.\n # verify: # settings for requirejs path verification\n # enabled: true # Whether or not to perform verification\n # optimize :\n # inferConfig:true # Mimosa figures out all you'd need for a simple r.js optimizer run.\n # If you rather Mimosa not do that, set inferConfig to false and\n # provide your config in the overrides section. See here\n # https://github.com/dbashford/mimosa#requirejs-optimizer-defaults\n # to see what the defaults are.\n # overrides: # Optimization configuration and Mimosa overrides. If you need to\n # make tweaks uncomment this line and add the r.js config\n # (http://requirejs.org/docs/optimization.html#options) as new\n # paramters inside the overrides ojbect. To unset Mimosa's defaults,\n # set a property to null.\n #\n # overrides can also be a function that takes mimosa-require's\n # inferred config for each module. This allows the inferred config\n # to be updated and enhanced instead of just overridden.";
return "\t\n\n # require: # configuration for requirejs options.\n # exclude:[] # Regex or string paths. Paths can be absolute or relative to the\n # watch.javascriptDir. These files will be excluded from all\n # require module functionality. That includes AMD verification and\n # being considered a root level file to be optimized.\n # commonConfig: \"common\" # The path from 'javascriptDir' to the location of common requirejs\n # config. This is config shared across multiple requirejs modules.\n # This should be either a require.config({}) or a requirejs.config({}) function call.\n # Defaults to the value `common` - referring to a file named common.js in the root of javascriptDir.\n # Does not need to exist, so can be left alone if a commonConfig is not\n # being used.\n # tracking: # every time mimosa starts up, mimosa-require needs to be able to\n # build a dependency graph for the codebase. It can do that by\n # processing all the files, but that means each file needs to be\n # processed when mimosa watch starts which slows down startup.\n # tracking allows mimosa-require to write interim state to the file\n # system so that from one mimosa run to another it can persist the\n # important information and not need the entire application to be\n # rebuilt\n # enabled: true # whether or not tracking is enabled\n # path: \".mimosa/require/tracking.json\" # the path to the tracking file relative to the\n # root of the project.\n # verify: # settings for requirejs path verification\n # enabled: true # Whether or not to perform verification\n # optimize :\n # inferConfig:true # Mimosa figures out all you'd need for a simple r.js optimizer run.\n # If you rather Mimosa not do that, set inferConfig to false and\n # provide your config in the overrides section. See here\n # https://github.com/dbashford/mimosa#requirejs-optimizer-defaults\n # to see what the defaults are.\n # modules: # If using a modules config, place it here. mimosa-require will use\n # the modules config directly, but also base many other r.js config\n # options based on a modules setup instead of a single file setup.\n # overrides: # Optimization configuration and Mimosa overrides. If you need to\n # make tweaks uncomment this line and add the r.js config\n # (http://requirejs.org/docs/optimization.html#options) as new\n # paramters inside the overrides ojbect. To unset Mimosa's defaults,\n # set a property to null.\n #\n # overrides can also be a function that takes mimosa-require's\n # inferred config for each module. This allows the inferred config\n # to be updated and enhanced instead of just overridden.";
};

exports.validate = function(config, validators) {
Expand All @@ -39,6 +40,13 @@ exports.validate = function(config, validators) {
validators.ifExistsIsBoolean(errors, "require.verify.enabled", config.require.verify.enabled);
}
if (validators.ifExistsIsObject(errors, "require.optimize", config.require.optimize)) {
if (validators.ifExistsIsArrayOfObjects(errors, "require.optimize.modules", config.require.optimize.modules)) {
if (config.require.optimize.modules) {
if (!(config.require.optimize.modules.length > 0)) {
errors.push("require.optimize.modules array, when provided, cannot be empty.");
}
}
}
validators.ifExistsIsBoolean(errors, "require.optimize.inferConfig", config.require.optimize.inferConfig);
if (config.require.optimize.overrides != null) {
obj = config.require.optimize.overrides;
Expand Down
36 changes: 21 additions & 15 deletions lib/tasks/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,34 +82,40 @@ Optimizer = (function() {
var baseUrl, configFile, name, runConfig;
runConfig = typeof config.require.optimize.overrides === "object" ? _.clone(config.require.optimize.overrides, true) : {};
runConfig.optimize = config.isOptimize && config.isMinify ? "none" : "uglify2";
configFile = fs.existsSync(config.require.commonConfig) ? config.require.commonConfig : file;
baseUrl = path.join(config.watch.compiledDir, config.watch.javascriptDir);
name = file.replace(baseUrl + path.sep, '').replace('.js', '').split(path.sep).join('/');
if (!((runConfig.logLevel != null) || runConfig.logLevel === null)) {
runConfig.logLevel = 3;
}
if (!((runConfig.baseUrl != null) || runConfig.baseUrl === null)) {
runConfig.baseUrl = baseUrl;
}
configFile = fs.existsSync(config.require.commonConfig) ? config.require.commonConfig : file;
if (!((runConfig.mainConfigFile != null) || runConfig.mainConfigFile === null)) {
runConfig.mainConfigFile = configFile;
if (!((runConfig.wrap != null) || runConfig.wrap === null)) {
runConfig.wrap = true;
}
if (!((runConfig.findNestedDependencies != null) || runConfig.findNestedDependencies === null)) {
runConfig.findNestedDependencies = true;
}
if (!((runConfig.include != null) || runConfig.include === null)) {
runConfig.include = [name];
}
if (!((runConfig.insertRequire != null) || runConfig.insertRequire === null)) {
runConfig.insertRequire = [name];
}
if (!((runConfig.wrap != null) || runConfig.wrap === null)) {
runConfig.wrap = true;
if (!((runConfig.mainConfigFile != null) || runConfig.mainConfigFile === null)) {
runConfig.mainConfigFile = configFile;
}
if (!((runConfig.name != null) || runConfig.name === null)) {
runConfig.name = 'almond';
if (config.require.optimize.modules) {
runConfig.dir = path.relative(config.root, config.watch.compiledJavascriptDir);
runConfig.modules = _.clone(config.require.optimize.modules);
runConfig.keepBuildDir = true;
} else {
name = file.replace(baseUrl + path.sep, '').replace('.js', '').split(path.sep).join('/');
if (!((runConfig.include != null) || runConfig.include === null)) {
runConfig.include = [name];
}
if (!((runConfig.insertRequire != null) || runConfig.insertRequire === null)) {
runConfig.insertRequire = [name];
}
if (!((runConfig.name != null) || runConfig.name === null)) {
runConfig.name = 'almond';
}
runConfig.out = runConfig.out === null ? void 0 : runConfig.out ? path.join(runConfig.baseUrl, runConfig.out) : path.join(runConfig.baseUrl, name.split('/').join(path.sep) + "-built.js");
}
runConfig.out = runConfig.out === null ? void 0 : runConfig.out ? path.join(runConfig.baseUrl, runConfig.out) : path.join(runConfig.baseUrl, name.split('/').join(path.sep) + "-built.js");
if (typeof config.require.optimize.overrides === "function") {
config.require.optimize.overrides(runConfig);
}
Expand Down
8 changes: 8 additions & 0 deletions src/config.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ exports.defaults = ->
enabled: true
optimize :
inferConfig:true
modules:null
overrides:{}

exports.placeholder = ->
Expand Down Expand Up @@ -51,6 +52,9 @@ exports.placeholder = ->
# provide your config in the overrides section. See here
# https://github.com/dbashford/mimosa#requirejs-optimizer-defaults
# to see what the defaults are.
# modules: # If using a modules config, place it here. mimosa-require will use
# the modules config directly, but also base many other r.js config
# options based on a modules setup instead of a single file setup.
# overrides: # Optimization configuration and Mimosa overrides. If you need to
# make tweaks uncomment this line and add the r.js config
# (http://requirejs.org/docs/optimization.html#options) as new
Expand All @@ -72,6 +76,10 @@ exports.validate = (config, validators) ->
validators.ifExistsIsBoolean(errors, "require.verify.enabled", config.require.verify.enabled)

if validators.ifExistsIsObject(errors, "require.optimize", config.require.optimize)
if validators.ifExistsIsArrayOfObjects(errors, "require.optimize.modules", config.require.optimize.modules)
if config.require.optimize.modules
unless config.require.optimize.modules.length > 0
errors.push "require.optimize.modules array, when provided, cannot be empty."
validators.ifExistsIsBoolean(errors, "require.optimize.inferConfig", config.require.optimize.inferConfig)
if config.require.optimize.overrides?
obj = config.require.optimize.overrides
Expand Down
35 changes: 22 additions & 13 deletions src/tasks/builder.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,34 @@ class Optimizer
else
"uglify2"

configFile = if fs.existsSync config.require.commonConfig then config.require.commonConfig else file
baseUrl = path.join config.watch.compiledDir, config.watch.javascriptDir
name = file.replace(baseUrl + path.sep, '').replace('.js', '').split(path.sep).join('/')

runConfig.logLevel = 3 unless runConfig.logLevel? or runConfig.logLevel is null
runConfig.baseUrl = baseUrl unless runConfig.baseUrl? or runConfig.baseUrl is null

configFile = if fs.existsSync config.require.commonConfig then config.require.commonConfig else file
runConfig.wrap = true unless runConfig.wrap? or runConfig.wrap is null
runConfig.findNestedDependencies = true unless runConfig.findNestedDependencies? or runConfig.findNestedDependencies is null
runConfig.mainConfigFile = configFile unless runConfig.mainConfigFile? or runConfig.mainConfigFile is null

runConfig.findNestedDependencies = true unless runConfig.findNestedDependencies? or runConfig.findNestedDependencies is null
runConfig.include = [name] unless runConfig.include? or runConfig.include is null
runConfig.insertRequire = [name] unless runConfig.insertRequire? or runConfig.insertRequire is null
runConfig.wrap = true unless runConfig.wrap? or runConfig.wrap is null
runConfig.name = 'almond' unless runConfig.name? or runConfig.name is null
runConfig.out = if runConfig.out is null
undefined
else if runConfig.out
path.join runConfig.baseUrl, runConfig.out
# When using modules, have different default config
# need dir instead out out, need to keep build dir or r.js removes it
if config.require.optimize.modules
runConfig.dir = path.relative config.root, config.watch.compiledJavascriptDir
runConfig.modules = _.clone config.require.optimize.modules
runConfig.keepBuildDir = true
else
path.join runConfig.baseUrl, name.split('/').join(path.sep) + "-built.js"

name = file.replace(baseUrl + path.sep, '').replace('.js', '').split(path.sep).join('/')
runConfig.include = [name] unless runConfig.include? or runConfig.include is null
runConfig.insertRequire = [name] unless runConfig.insertRequire? or runConfig.insertRequire is null
runConfig.name = 'almond' unless runConfig.name? or runConfig.name is null
runConfig.out = if runConfig.out is null
undefined
else if runConfig.out
path.join runConfig.baseUrl, runConfig.out
else
path.join runConfig.baseUrl, name.split('/').join(path.sep) + "-built.js"


if typeof config.require.optimize.overrides is "function"
config.require.optimize.overrides runConfig
Expand Down

0 comments on commit 54a1cd8

Please sign in to comment.