@asgoth 's answer sicuramente avrebbe funzionato, ma capito paio di altre opzioni pure per 'iniettare'(o rimuovere piuttosto) codice durante il processo di compilazione.
Come documentato nello example build.js file, è possibile utilizzare la build pragmas
per includere/escludere snippet di codice durante il processo di generazione.
//Specify build pragmas. If the source files contain comments like so:
//>>excludeStart("fooExclude", pragmas.fooExclude);
//>>excludeEnd("fooExclude");
//Then the comments that start with //>> are the build pragmas.
//excludeStart/excludeEnd and includeStart/includeEnd work, and the
//the pragmas value to the includeStart or excludeStart lines
//is evaluated to see if the code between the Start and End pragma
//lines should be included or excluded. If you have a choice to use
//"has" code or pragmas, use "has" code instead. Pragmas are harder
//to read, but they can be a bit more flexible on code removal vs.
//has-based code, which must follow JavaScript language rules.
//Pragmas also remove code in non-minified source, where has branch
//trimming is only done if the code is minified via UglifyJS or
//Closure Compiler.
pragmas: {
fooExclude: true
},
//Same as "pragmas", but only applied once during the file save phase
//of an optimization. "pragmas" are applied both during the dependency
//mapping and file saving phases on an optimization. Some pragmas
//should not be processed during the dependency mapping phase of an
//operation, such as the pragma in the CoffeeScript loader plugin,
//which wants the CoffeeScript compiler during the dependency mapping
//phase, but once files are saved as plain JavaScript, the CoffeeScript
//compiler is no longer needed. In that case, pragmasOnSave would be used
//to exclude the compiler code during the save phase.
pragmasOnSave: {
//Just an example
excludeCoffeeScript: true
},
ho potuto vedere in azione sul jquery.mobile
code, che probabilmente è un buon posto per imparare AMD
e requirejs
da.
Ecco cosa ha funzionato per me:
AppLogger.js:
/* global console: false */
define(function() {
var debugEnabled = false;
//>>excludeStart('appBuildExclude', pragmas.appBuildExclude);
debugEnabled = true;
//>>excludeEnd('appBuildExclude');
return {
log:function (message) {
if (debugEnabled && console) {
console.log('APP DEBUG: ' + message);
}
}
};
});
Gruntfile.js:
requirejs:{
compile:{
options:{
baseUrl:"js/",
mainConfigFile:"js/main.js",
name:'main',
out:'js/main.min.js',
pragmas:{ appBuildExclude:true }
}
}
}
Con questa configurazione per la requirejs
nel mio Gruntfile
, la sezione all'interno delle direttive di excludeStart
e excludeEnd
sono stati rimossi dal file compilato/costruito.
Sto ancora imparando requirejs
, quindi nessuno afferma che questa è la migliore pratica per questo genere di cose, ma questo sicuramente ha funzionato per me.
Grazie per la risposta. Quello che stavo cercando è leggermente diverso e ho cercato di chiarirlo con un aggiornamento alla domanda. –
Modificato la mia risposta. Vedi se questo aiuta. – asgoth
Questo sicuramente funzionerebbe, ma ho trovato un'altra opzione di compilare 'pragmas' dal [r.js example build file] (https://github.com/jrburke/r.js/blob/master/build/example.build js). Questo apparentemente è ciò che 'jquery.mobile' [utilizza anche] (https://github.com/jquery/jquery-mobile/blob/master/js/jquery.mobile.events.js). Ho accettato la tua risposta, ma aggiungerò un'altra delle mie con ciò che funziona per me. Grazie! –