6

我正在尝试从我的 grunt Web 应用程序中的文件中删除“.html”。

http://testing.com/one/应该从该文件夹返回 index.html,但如果没有尾部斜杠 ( http://testing.com/one ) 它应该检查 one.html

grunt-connect-rewrite 似乎与我能找到的示例一起工作得很好,但是从 .html 文件中删除文件扩展名似乎让我很生气。这里的规则类似于我在 .htaccess 文件中使用的规则。

connect: {
    server: {
        options: {
            port: 9000,
            keepalive: true,
            base: 'dist',
            middleware: function(connect, options) {
              return [
                rewriteRulesSnippet, 
                // Serve static files
                connect.static(require('path').resolve(options.base))
              ];
            }
        },
        rules: {
            '^(.*)\.html$': '/$1'
        }
    }
}

所以问题是,在这里使用的正确规则是什么?

4

3 回答 3

5

答案对我不起作用,所以我一直在玩,直到找到解决方案。

正则表达式:

from: '(^((?!css|html|js|img|fonts|\/$).)*$)',
to: "$1.html"

软件包版本:

"grunt-contrib-watch": "~0.5.3",
"grunt-contrib-connect": "~0.5.0",
"grunt-connect-rewrite": "~0.2.0"

完整的工作 Gruntfile:

var rewriteRulesSnippet = require("grunt-connect-rewrite/lib/utils").rewriteRequest;
module.exports = function(grunt) {
  grunt.initConfig({
    watch: {
      html: {
        files: "**/*.html"
      }
    },
    connect: {
      options: {
        port: 9000,
        hostname: "127.0.0.1"
      },
      rules: [{
        from: '(^((?!css|html|js|img|fonts|\/$).)*$)',
        to: "$1.html"
      }],
      dev: {
        options: {
          base: "./",
          middleware: function(connect, options) {
            return [rewriteRulesSnippet, connect["static"](require("path").resolve(options.base))];
          }
        }
      },
    }
  });
  grunt.loadNpmTasks("grunt-connect-rewrite");
  grunt.loadNpmTasks("grunt-contrib-connect");
  grunt.loadNpmTasks("grunt-contrib-watch");
  grunt.registerTask("default", ["configureRewriteRules", "connect:dev", "watch"]);
};
于 2013-12-05T16:58:06.640 回答
1

规则应该反过来,就像这样。

rules: {'(.*)(?!\.html|\.jpg|\.css)' : '$1.html'}

这将匹配末尾没有“.html”、“.jpg”或“.css”的所有内容,并将 html 添加到其末尾。确保添加所有不想匹配的扩展名(或匹配所有扩展名的正则表达式)。


以下是我如何实现 grunt connect rewrite 以防万一有人在寻找它:


命令行:

npm install grunt-connect-rewrite --save-dev

在你的 grunt 文件中包含 grunt 任务:

grunt.loadNpmTasks('grunt-connect-rewrite’);

保存片段

var rewriteRulesSnippet = require('grunt-connect-rewrite/lib/utils').rewriteRequest; 

设置配置

grunt.initConfig({
    connect: {
        options: {
            port: 9000,
            hostname: 'localhost'
            base:'<%= yeoman.app %>', //make sure you have a base specified for this example
        },
        rules: {
            '^/index_dev.html$': '/src/index.html',
            '^/js/(.*)$': '/src/js/$1',
            '^/css/(.*)$': '/public/css/$1'
        }
    }
})

将中间件添加到上述选项块中:

options: {
    port: 9000,
    livereload: 35729,
    // change this to '0.0.0.0' to access the server from outside
    hostname: '*',
    debug: true,
    base:'<%= yeoman.app %>',
    middleware: function(connect, options){
      if (!Array.isArray(options.base)) {
        options.base = [options.base];
      }
      var middlewares = [rewriteRulesSnippet];
      options.base.forEach(function(base) {
        // Serve static files.
        middlewares.push(connect.static(base));
      });
      return middlewares;
    }
}

在底部添加任务:

grunt.registerTask('server', function (target) {
    grunt.task.run([
      'configureRewriteRules',
      //...
    ]);
});
于 2013-11-19T01:23:08.803 回答
0
rules: {
    // http://testing.com/one -> http://testing.com/one.html
    '^(.*[^/])$': '$1.html',    
    // http://testing.com/one/ -> http://testing.com/one/index.html
    '^(.*)/$': '$1/index.html'
}

应该做的伎俩。

于 2013-11-26T17:38:56.767 回答