2

I have a number of solutions. There is much overlap, so that code goes into another solution of "common" code, and I reference it as a DLL or git submodule.

But there's also lots of common JavaScript and CSS, and even some .config files and XML. I need to duplicate them for every solution!

How can I pull shared js/css into a solution? Keep in mind the shared assembly's DLL won't contain those static files.

Also, importantly, I need to be able to reference those js/css files in MVC's bundling system (that's the whole point, after all).

4 Answers 4

3

If you are going to have a lot of this, build your own nuget packages and host them on a share or on your own private nuget server. It's remarkably easy to do, you get to execute scripts if necessary when the package is added to your project, and nuget packages are versioned, so you when you update your library, you can release an updated package with an incremented version number, and they will coexist.

Sign up to request clarification or add additional context in comments.

8 Comments

I'm sure this works, but for a very small team, or one man show, this is too much effort. Surely there's a straightforward way?...
@hbob its not really that much effort. seems like a good solution to me.
I cringe everytime a hear a developer say "too much effort". How much is a reusable system with pretty much one time setup costs of time and resources that allows you to have seemless code reuse and greater team dynamics worth? Sheesh. Developers use to actually have to write hundreds of lines of code just to add 2+2. Modern developers have it easy.
@ChrisPratt My answer is "too many moving parts". In a small team, or one man show, you want as simple as possible. You'd be surprised at how quickly the complexity creeps up on you, and drowns you. If there's no straightforward way, then of course, but surely there is...
This is the straight-forward way. That's the point it seems you're missing.
|
2

There's no way to truly share static resources between projects, and especially between solutions - at least in the sense that you have only one copy of the file. For most things, adding an item as a link will usually work, but static resources don't fall under that umbrella, especially if you need them to work with the bundling system.

However, you aren't without hope. The best way to achieve this is actually to create your own Nuget package. You can have a project/solution that contains all these common resources and use that to build a Nuget package from. Then, you can install this Nuget package in your other projects/solutions. When you make changes to the static resources, release an update to the Nuget package and then upgrade all the projects that utilize it. You can even create a private Nuget repo so that the packages are only available internally.

Here's a few resources to help you in that endeavor:

Comments

2

OK I found a way which works, is simple, self-contained - and does not need a local nuget server (which is overkill for something so simple, and I prefer less tools and less servers if I can help it).

Note: my common code, styles, scripts, everything are contained in a git submodule in my solution. If you don't work this way, just adapt the paths below to point to your shared stuff.

Add to pre-build event to delete all files (not required, but deletes files from destination which were deleted in the source):

rd /S /Q "$(ProjectDir)Scripts\include"
rd /S /Q "$(ProjectDir)Styles\include"
md "$(ProjectDir)Scripts\include"
md "$(ProjectDir)Styles\include"

Add to post-build event to copy files:

xcopy /D /S /F /Y $(SolutionDir)gitsubmodulename\projectname\Styles\*.min.css $(ProjectDir)Styles\include
xcopy /D /S /F /Y $(SolutionDir)gitsubmodulename\projectname\Scripts\*.js $(ProjectDir)Scripts\include

If using git, add those directories to .gitignore:

/*/Scripts/include/
/*/Styles/include/

Pros:

  • At every build, new/changed files are pulled into your web project
  • Since they are placed within the scripts/styles directories, they can be served in both debug and release modes
  • Bundling works exactly as you'd expect. To deploy, just do a regular bin deployment
  • You can make changes to the source files (in the git submodule, not the web project), and build, and the changes are available immediately (no need to edit, repack, update, etc. nuget packages)

Cons:

  • ?

2 Comments

This could be simpler: Add Existing File -> Add as Link..reminds me Photoshop/Illustrator :) Hth...
@EdSF No, that works in release mode but not in debug mode. My way works for both.
0

Just toying with some ideas, but some possibilities:

  • a post-build copy into the appropriate bin directory
  • a T4 template (which runs on build) which pulls in the external files

Though they are both "hacky"...

Also
- A working method to include them as linked files, but only works in RELEASE mode

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.