使用grunt-contrib-connect和grunt-connect-rewrite删除文件扩展名 [英] Removing file extension using grunt-contrib-connect and grunt-connect-rewrite

查看:277
本文介绍了使用grunt-contrib-connect和grunt-connect-rewrite删除文件扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从我的grunt web应用程序中的文件中移除'.html'。

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



grunt-connect-rewrite似乎可以正常工作,我可以找到,但从.html文件中删除文件扩展名似乎在杀死我。这里的规则与我在.htaccess文件中使用的规则类似。

  connect:{
服务器:{
选项:{
端口:9000,
keepalive:true,
base:'dist',
中间件:function(connect,options){
return [
rewriteRulesSnippet,
// Serve static文件
connect.static(require('path')。resolve(options.base))
];
}
},
规则:{
'^(。*)\.html $':'/ $ 1'
}
}

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

解决方案

答案并不适用于我,因此我一直在玩弄它,直到找到解决方案。



正则表达式:

  from:'(^((?!css | html | js | img |字体| \ / $)。)* $)',
到:$ 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-重写/ 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:
}:
dev:{
options:{
ase:./,
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]);
};


I'm trying to remove '.html' from files in my grunt web app.

http://testing.com/one/ should return index.html from that folder, but if there is no trailing slash (http://testing.com/one) it should check for one.html

The grunt-connect-rewrite seems to be working fine with examples that I can find, but removing file extensions from .html files seems to be killing me. The rule here is one similar to what i'd use in an .htaccess file.

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'
        }
    }
}

So the question is, what is the correct rule to use here?

解决方案

The answers didn't work for me so I played around with it until I found a solution.

Regex:

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

Package versions:

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

Complete working 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"]);
};

这篇关于使用grunt-contrib-connect和grunt-connect-rewrite删除文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆