Dynamically Add Minified Embedded Resource

1.7k Views Asked by At

Is there a way to dynamically add an embedded resource during the build process? I am currently using Ajax-Minifier to reduce two files down into one. The result works great. However I need to include this file as an embedded resource when the code is running in release mode (currently toggled by a Preprocessor directive of if(!DEBUG)

Here's a simple version of the target command I am using in my MSBUILD

<Target Name="TestMinifiy" Condition=" '$(ConfigurationName)' == 'Release' ">
    <ItemGroup>
      <MyScript Include="..\Test1.js" />
      <MyScript Include="..\Test2.js" />
    </ItemGroup>
    <AjaxMin JsSourceFiles="@(MyScript)" JsCombinedFileName="$(IntermediateOutputPath)\Final.js" />
</Target>

So basically I would like to include Final.js as an embedded resource, I then can reference within the code.

From the C# side this is what I am looking to do:

#if (!DEBUG)
[assembly: WebResource("Final.js", "text/javascript")]
#endif

Any help or ideas would be appreciated.

EDIT:

I've changed the JsCombinedFileName to $(IntermediateOutputPath)\Final.js. So that should create the file. I've then created a separate target to embed this file.

   <Target Name="EmbedResource" Condition=" '$(ConfigurationName)' == 'Release' ">
        <Message Text="Copying into Embedded..." Importance="high" />
        <ItemGroup>
          <EmbeddedResource Include="$(IntermediateOutputPath)\Final.js" >
            <LogicalName>
              Final.js
            </LogicalName>
          </EmbeddedResource>
        </ItemGroup>
      </Target>

However, still no luck.

1

There are 1 best solutions below

7
On

(Updated to better answer the question.)

I believe that the problem you are running into is that the minification is occurring as part of the build, which means that the generated final.js file is not available within the TestMinify target. The embedding itself can be included within an item group like this.

<Target Name="Build" Condition=" '$(ConfigurationName)' == 'Release' ">
    <!-- other stuff -->

    <ItemGroup>
        <EmbeddedResource Include="final.js" />

        <!-- other stuff -->
    </ItemGroup>
</Target>

To avoid duplication in the main build target, the conditional can also be placed directly in the EmbeddedResource tag like this.

<Target Name="Build">
    <!-- other stuff -->

    <ItemGroup>
        <EmbeddedResource Condition=" '$(Configuration)' == 'Release' " Include="final.js" />

        <!-- other stuff -->
    </ItemGroup>
</Target>

You can then call your TestMinify target before your main Build target.

Alternatively, you can create a prebuild event to generate the file. It would need to call an executable rather than a MSBuild task.

Here is an example of how the prebuild event might look.

<PropertyGroup>
  <PreBuildEvent>if "$(ConfigurationName)" equ "Release" (copy "$(ProjectDir)source.js" "$(ProjectDir)final.js")</PreBuildEvent>
</PropertyGroup>

In this second scenario, the Build target with the EmbeddedResource could be set up in either of the ways mentioned previously.