回答
是的,你可以。需要时您只需执行gulp taskname
。您需要对执行gulp命令的脚本执行ajax请求。 FI,你可以做一个工作示例与此骨架,使用和的NodeJS表示:
gulpfile
gulp.task('minify' , function(){
console.log('gulp called!');
gulp.src('scripts/src/test.js')
.pipe(minify())
.pipe(gulp.dest('scripts/bin/'));
});
server.js
var express = require('express')
var app = express();
var exec = require('child_process').exec;
app.use(express.static('public'))
app.post('/send' , function(req , res){
console.log('this was called');
exec('gulp minify');
res.send('success!');
});
app.listen(3000, function() {
console.log('Example app listening on port 3000!');
});
公共/ index.html的
<html>
<head>
</head>
<body>
<button onclick=send()>Execute gulp minify</button>
</body>
<script
src="https://code.jquery.com/jquery-3.1.1.min.js"
integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
crossorigin="anonymous"></script>
<script>
function send(){
$.ajax({
url : 'http://localhost:3000/send',
method : 'POST',
success : function(res){
console.log(res);
},
error : function(response){
console.log('Error!');
console.log(response);
}
});
}
</script>
</html>
的package.json
{
"name": "gulp-from-script",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.14.0",
"gulp-uglify": "^2.0.0"
}
}
脚本/ src目录/ test.js
function test(){
console.log('this file should be minified to bin/test.js on click');
//end.
}
随着该样板,你可以只安装依赖 npm install
再服: node server.js
然后去 localhost:3000/index.html
点击该按钮后,缩小的一个文件应该会出现在/scripts/bin/test.js
当然,你需要在系统上安装和的NodeJS NPM来得到这个工作。只是一个演示如何做的实例,可以使用任何其他服务器端技术来完成,唯一的一点是从服务器端的后控制器调用gulpfile任务。
那么,这会对任何事情有用吗?。正如桑德里纳佩雷拉所说,吞咽是一个任务跑步者意味着建立您的项目的前端资产。您不应该从Web应用程序的用户触发的脚本中调用它。
但是,你可以建立这种系统的创建一个本地网站或应用在本地主机/来管理自己的作品,或者在你的机器执行其他任务,从UI。我不知道这是否仅仅是一个好的或可取的想法,但它肯定是可行的。
谢谢你的非常详细的答案!我必须找到一个更好的解决方案来从窗体构建/生成静态html。我正在** ** reactjs **,我认为这是一个好的开始。再次感谢你。 – mstroiu
- 1. 从gulp任务运行npm脚本
- 2. 在CruiseControl.NET上运行gulp任务
- 3. gulp-imagemin任务运行出错
- 4. 通过npm自动运行gulp任务
- 5. linting后可以运行gulp任务吗?
- 6. 如何从dockerfile运行gulp任务
- 7. 在Codekit Hook中运行Gulp任务
- 8. 用循环运行Gulp任务
- 9. Gulp-browserSync任务只运行一次
- 10. gulp 4.0:运行系列任务
- 11. 完成其他任务后运行gulp任务
- 12. gulp多次执行任务
- 13. 使用gulp任务运行rails服务器和protrator
- 14. 如何在执行时运行gulp任务
- 15. Gulp任务顺序 - 串行和并行
- 16. Gulp依赖任务
- 17. Gulp构建任务
- 18. 在运行Gulp任务时在插件'gulp-browserify'中获取Js语法错误
- 19. Gulp投掷“解析文件时为只读”错误,同时运行Gulp任务
- 20. 当运行generator-gulp-webapp(含gulp-jade)的'build'任务时产生一个空index.html
- 21. Gulp任务正在无限循环中运行
- 22. 以编程方式运行Gulp 4任务
- 23. Gulp错误:ENOENT,lstat正在运行任务
- 24. 如何在Visual Studio代码中运行gulp任务?
- 25. 没有在弹性豆杆上运行的Gulp任务
- 26. gulp任务运行请求显示日志消息太晚
- 27. 在Gulp任务中多次运行手柄
- 28. 运行多个任务时,Gulp不起作用
- 29. 如何在终端中运行gulp任务
- 30. 如何在一个gulp任务中运行多个操作?
据我所知,一饮而尽任务意味着管理一个项目,而不是在应用程序本身使用。你使用什么框架/语言? –
我正在考虑使用** reactjs **。你怎么看 ? – mstroiu