我正在尝试设置我的Gruntfile来使用grunt连接npm模块。当试图使用命令grunt clean server启动服务器时,我遇到了问题。它错误的行:
Warning: root path must be a string Use --force to continue.我真的不知道我搞砸了什么配置,可能需要另一组眼睛。这是我的Gruntfile
/* global module, conf */
var modRewrite = require('connect-modrewrite');
var mountFolder = function(connect, dir) {
return connect.static(require('path').resolve(dir));
};
module.exports = function(grunt) {
grunt.initConfig({
copy: {
base: {
files: [
{src: "index.html", dest: "BUILD/index.html"},
{expand: true, src: "app/**", dest: "BUILD/"},
{expand: true, src: "assets/**", dest: "BUILD/"}
]
}
},
connect: {
proxies: [
{
context: "/wwff",
host: "localhost",
port: "8080"
}
],
/**
* Task defines a server at 9000,
* watching the BUILD directory
*/
dev: {
options: {
port: "9000",
hostname: '*',
base: "BUILD",
middleware: function(connect, options) {
var proxySnippet = require('grunt-connect-proxy/lib/utils').proxyRequest;
return [
// include the proxy first
proxySnippet,
modRewrite([
'!\\.html|\\.js|\\.swf|\\.json|\\.xml|\\.css|\\.png|\\.jpg|\\.gif|\\.ico|\\.aff|\\.msi|\\.zip|\\.dic$ /index.html [L]'
]),
// serve static files
connect.static(options.base),
// make empty directories browsable
connect.directory(options.base)
];
}
}
}
},
/*
* This task watches the application and asset directories and
* deploys any changes to the dev server
*/
watch: {
static: {
files: [ "app/**/*.js", "app/**/*.html"],
tasks: ["build"]
}
},
clean: {
build: ["BUILD/"],
temp: ["tmp"]
}
});
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-connect-proxy');
grunt.loadNpmTasks('grunt-contrib-clean');
/*
* Main BUILD Task
*/
grunt.registerTask("build", "Copies app and asset files to the BUILD directory.", function() {
grunt.task.run("copy:base");
});
grunt.registerTask("server", "Stand up a node server for development.", function() {
grunt.task.run(["build", "configureProxies:dev", "connect:dev", "watch"]);
});
grunt.event.on('watch', function(action, filepath, target) {
grunt.log.writeln(target + ': ' + filepath + ' has ' + action);
});
};当我使用--verbose标志时,我会得到以下行:
Verifying property connect.dev exists in config...OK
File: [no files]
Options: protocol="http", port="9000", hostname="*", base="BUILD", directory=null, keepalive=false, debug=false, livereload=false, open=false, useAvailablePort=false, onCreateServer=null, middleware=undefined在我看来,问题是middleware是undefined,但我不知道为什么。
任何帮助都是非常感谢的。
发布于 2014-11-29 05:24:31
嗯,我不明白怎么回事,但是我的middleware函数中的middleware似乎变成了一个数组,第一个值是我的BUILD目录。
因此,我的middleware函数的以下代码片段可以工作:
middleware: function(connect, options) {
var proxySnippet = require('grunt-connect-proxy/lib/utils').proxyRequest;
return [
// include the proxy first
proxySnippet,
modRewrite(['!\\.html|\\.js|\\.swf|\\.json|\\.xml|\\.css|\\.png|\\.jpg|\\.gif|\\.ico|\\.aff|\\.msi|\\.zip|\\.dic$ /index.html [L]']),
// serve static files
connect.static(options.base[0]),
// make empty directories browsable
connect.directory(options.base[0])
];
}在上述片段中,重要的部分是我的options.base现在是options.base[0]。如果有人能解释为什么会这样,那将是非常感谢的。
发布于 2015-05-20 16:19:19
游戏有点晚了,但是在更新版的grunt-connect中,base总是一个数组。您可以使您的中间件与以下任何一个版本兼容:
middleware: function (connect, options) {
// Older versions of grunt-connect.
if (!Array.isArray(options.base)) {
options.base = [options.base];
}
var middlewares = [
require('connect-livereload')()
];
// Serve static files.
options.base.forEach(function(base) {
middlewares.push(connect.static(base));
});
return middlewares;
}有关使用grunt-connect代理的示例,请参阅https://github.com/drewzboto/grunt-connect-proxy上的文档。
这比仅仅安装在options.base[0]的第一个基地要好。
发布于 2015-01-14 19:22:14
原因是options.base是一个数组。options.base引用数组中的第一个(在本例中仅指)项(它是表示根路径的字符串)。
https://stackoverflow.com/questions/27176920
复制相似问题