Domanda

Sto sviluppando un piccolo progetto di app Web (ColdFusion) e sto provando a mantenere il mio progetto diviso in più file durante lo sviluppo, ma distribuisco solo un file al completamento.

Ho riferimenti a file esterni, ad esempio:

<script type="text/javascript" src="jquery-1.2.6.pack.js"></script>
<link type="text/css" rel="stylesheet" href="project.css" />

E quando creo il mio progetto, voglio che i file siano inclusi e incorporati nel singolo file del prodotto finito.

<script type="text/javascript">eval(function(p,a,c,k,e,r) [...]</script>
<style type="text/css">div{font:normal;} [...]</style>

Ad ogni modo, non sembra che ci sia un modo semplice per farlo. Qualcuno lo sa?

È stato utile?

Soluzione 3

Rispondere alla mia domanda dopo alcune ore di hacking ...

<script language="groovy" src="build.groovy" />

e questo script groovy sostituisce qualsiasi file javascript o css di riferimento con il contenuto del file stesso.

f = new File("${targetDir}/index.cfm")
fContent = f.text
fContent = jsReplace(fContent)
fContent = cssReplace(fContent)
f.write(fContent)

// JS Replacement
def jsReplace(htmlFileText) {
    println "Groovy: Replacing Javascript includes"
    // extract all matched javascript src links
    def jsRegex = /<script [^>]*src=\"([^\"]+)\"><\/script>/
    def matcher = (htmlFileText =~ jsRegex)
    for (i in matcher) {
        // read external files in
        def includeText = new File(matcher.group(1)).text
        // sanitize the string for being regex replace string (dollar signs like jQuery/Prototype will screw it up)
        includeText = java.util.regex.Matcher.quoteReplacement(includeText)
        // weak compression (might as well)
        includeText = includeText.replaceAll(/\/\/.*/, "") // remove single-line comments (like this!)
        includeText = includeText.replaceAll(/[\n\r\f\s]+/, " ") // replace all whitespace with single space
        // return content with embedded file
        htmlFileText = htmlFileText.replaceFirst('<script [^>]*src="'+ matcher.group(1) +'"[^>]*></script>', '<script type="text/javascript">'+ includeText+'</script>');
    }
    return htmlFileText;
}

// CSS Replacement
def cssReplace(htmlFileText) {
    println "Groovy: Replacing CSS includes"
    // extract all matched CSS style href links
    def cssRegex = /<link [^>]*href=\"([^\"]+)\"[^>]*>(<\/link>)?/
    def matcher = (htmlFileText =~ cssRegex)
    for (i in matcher) {
        // read external files in
        def includeText = new File(matcher.group(1)).text
        // compress CSS
        includeText = includeText.replaceAll(/[\n\r\t\f\s]+/, " ")
        // sanitize the string for being regex replace string (dollar signs like jQuery/Prototype will screw it up)
        includeText = java.util.regex.Matcher.quoteReplacement(includeText)
        // return content with embedded file
        htmlFileText = htmlFileText.replaceFirst('<link [^>]*href="'+ matcher.group(1) +'"[^>]*>(<\\/link>)?', '<style type=\"text/css\">'+ includeText+'</style>');
    }
    return htmlFileText;
}

Quindi immagino che lo faccia per me. Funziona abbastanza bene ed è estensibile. Sicuramente non il migliore Groovy di sempre, ma è uno dei miei primi. Inoltre, ha richiesto alcuni vasetti con percorso di classe per essere compilato. Ho perso traccia di ciò, ma credo che sia il motore javax.scripting, groovy-engine.jar e groovy-all-1.5.6.jar

Altri suggerimenti

Fa questo quello che vuoi?

<property
    name="filename"
    value="jquery-1.2.6.pack.js"
/>

<loadfile
    property="contents"
    srcfile="${filename}"
/>

<replace dir=".">
    <include name="index.cfm"/>
    <replacetoken><![CDATA[<script type="text/javascript" src="${filename}"></script>]]></replacetoken>
    <replacevalue><![CDATA[<script type="text/javascript">${contents}</script>]]></replacevalue>
</replace>

Per una soluzione in formica pura, prova quanto segue:

<target name="replace">
    <property name="js-filename" value="jquery-1.2.6.pack.js"/>
    <property name="css-filename" value="project.css"/>
    <loadfile property="js-file" srcfile="${js-filename}"/>
    <loadfile property="css-file" srcfile="${css-filename}"/>
    <replace file="input.txt">
        <replacefilter token="&lt;script type=&quot;text/javascript&quot; src=&quot;${js-filename}&quot;&gt;&lt;/script&gt;" value="&lt;script type=&quot;text/javascript&quot;&gt;${js-file}&lt;/script&gt;"/>
        <replacefilter token="&lt;link type=&quot;text/css&quot; rel=&quot;stylesheet&quot; href=&quot;${css-filename}&quot; /&gt;" value="&lt;style type=&quot;text/css&quot;&gt;${css-file}&lt;/style&gt;"/>
    </replace>
</target>

L'ho provato e ha funzionato come previsto. Nel testo da sostituire e il valore che inserisci invece tutti i caratteri '& Lt;', '& Gt;' e '"' dovrebbe essere citato come & amp; lt ;, & amp; gt; e & amp; quot.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top