yeoman / grunt-usemin

[UNMAINTAINED] Replaces references to non-optimized scripts or stylesheets into a set of HTML files (or any templates/views)
BSD 2-Clause "Simplified" License
1.22k stars 339 forks source link

.NET MVC Project cshtml files not being updated #625

Open Giovanni-Mattucci opened 8 years ago

Giovanni-Mattucci commented 8 years ago

I'm having trouble with usemin not updating URLs within my .cshtml files. Here is my current setup in my gruntfile.js:

// Renames files for browser caching purposes
filerev: {
    dist: {
      src: [
        '<%= config.distDir %>/Content/css/{,*/}*.css',
        '<%= config.distDir %>/Scripts/{,*/}*.js',
        '<%= config.distDir %>/Content/images/{,*/}*.*'
      ]
    }
  },

  // Performs rewrites based on rev and usemin blocks in html
  usemin: {
    options: {
      assetsDirs: [
        '<%= config.distDir %>',
        '<%= config.distDir %>/Scripts'
      ],
      patterns: {
        jsmaprefs: [
          [/(main\.js\.map)/, 'Update js to reference our revved map']
        ]
      }
    },
    html: ['<%= config.distDir %>/Views/Shared/_Layout.cshtml'],
    css: ['<%= config.distDir %>/Content/css/{,*/}*.css'],
    jsmaprefs: ['<%= config.distDir %>/Scripts/{,*/}*.js']
  }

Does usemin only work with .html files? Or will it scan any file and replace those URL strings?

As an FYI I utilized this yeoman generator to create this file initially.

XmlmXmlmX commented 7 years ago

No, for me it works with cshtml-files. Correct path? Two of my collegues have the same problem. For me it works. No errors.

vankooch commented 7 years ago

Hi,

I've had the same problems here but now I've found a working version. The trick is to remove the starting~ from every href and srcattribute in your cshtml files.

Does not work:

<link rel="stylesheet" href="~/styles/bootstrap.css">

Works:

<link rel="stylesheet" href="/styles/bootstrap.css">

My Gruntfile is:

        // Usemin bundler
        "useminPrepare": {
            options: {
                dest: "<%= config.release.wwwroot %>",
                root: "<%= config.debug.wwwroot %>"
            },
            html: "<%= config.debug.root %>/Views/Shared/_Layout.cshtml",
            css: "<%= config.debug.styles %>/*.css"
        },

        // Performs rewrites based on rev and the useminPrepare configuration
        "usemin": {
            options: {
                assetsDirs: ["<%= config.release.wwwroot %>"]
            },
            html: ["<%= config.release.root %>/Views/*/*.cshtml"],
            css: ["<%= config.release.styles %>/*.css"]
        },

        // Add version tag to filenames
        "filerev": {
            dist: {
                src: [
                    "<%= config.release.styles %>/*.css",
                    "<%= config.release.scripts %>/*.js",
                    "<%= config.release.images %>/**/*"
                ]
            }
        },

The problem is that this will brake all the Area views because the links wont work anymore...

angel006z commented 7 years ago

Gruntfile use to relative path; but I hope to support “〜/” and “/”.

angel006z commented 7 years ago

+1

angel006z commented 7 years ago

+1

angel006z commented 7 years ago

+99