[LayaAirIDE 2.0]如何使用vscode编译laya项目

一、环境:
win10, vscode, LayaAirIDE2.6.0, nodejs v7.9.0(使用IDE相同的版本)
 
二、优势:
1、使用vscode编译项目
2、使用vscode定制gulp构建任务
3、脱离LayaAirIDE 编辑器独立编译
4、跨平台编译构建
5、使用LayaAirIDE node_modules库
 
三、laya项目文件夹下 .vscode/launch.json 添加配置
 
{
"name":"compile",
"type": "node",
"request": "launch",
//nodejs 可执行文件
"runtimeExecutable": "D:\\workspace\\layabox\\node-v7.9.0-win-x64\\node.exe",
//使用Laya编辑器下的 gulpjs
"program": "D:\\workspace\\layabox\\LayaAirIDE_beta\\resources\\app\\node_modules\\gulp\\bin\\gulp.js",
"args": [
//使用Laya编辑器node_module
"--cwd=D:\\workspace\\layabox\\LayaAirIDE_beta\\resources\\app",
"--gulpfile=${workspaceFolder}/.laya/compile.js",
"compile"
]
}
四、laya项目文件夹下 .laya/compile.js 修改代码
1、修改ideModuleDir、workSpaceDir解析规则
//获取Node插件和工作路径
let ideModuleDir;
let workSpaceDir;
for(var argv of process.argv)
{
if(argv.indexOf("--cwd=") == 0)
{
ideModuleDir = argv.split("=")[1] + "\\node_modules\\";
}
else if(argv.indexOf("--gulpfile=") == 0)
{
workSpaceDir = argv.split("=")[1].replace("/.laya/compile.js", "");
}
}
2、修改typescript编译配置
typescript({
cacheRoot:workSpaceDir + "/.rpt2_cache",
tsconfig:workSpaceDir + "/tsconfig.json",
check: true, //Set to false to avoid doing any diagnostic checks on the code
tsconfigOverride:{compilerOptions:{removeComments: true}},
include:/.*.ts/,
}),
3、compile.js 完整代码如下
// v1.2.2
//获取Node插件和工作路径
[b]let ideModuleDir;
let workSpaceDir;
for(var argv of process.argv)
{
if(argv.indexOf("--cwd=") == 0)
{
ideModuleDir = argv.split("=")[1] + "\\node_modules\\";
}
else if(argv.indexOf("--gulpfile=") == 0)
{
workSpaceDir = argv.split("=")[1].replace("/.laya/compile.js", "");
}
}
[/b]
const gulp = require(ideModuleDir + "gulp");
const rollup = require(ideModuleDir + "rollup");
const typescript = require(ideModuleDir + 'rollup-plugin-typescript2');//typescript2 plugin
const glsl = require(ideModuleDir + 'rollup-plugin-glsl');

// 如果是发布时调用编译功能,增加prevTasks
let prevTasks = "";
if (global.publish) {
prevTasks = ["loadConfig"];
}

gulp.task("compile", prevTasks, function () {
// 发布时调用编译功能,判断是否点击了编译选项
if (global.publish && !global.config.compile) {
return;
} else if (global.publish && global.config.compile) {
// 发布时调用编译,workSpaceDir使用publish.js里的变量
workSpaceDir = global.workSpaceDir;
}

return rollup.rollup({
input: workSpaceDir + '/src/Main.ts',
onwarn:(waring,warn)=>{
if(waring.code == "CIRCULAR_DEPENDENCY"){
console.log("warnning Circular dependency:");
console.log(waring);
}
},
treeshake: false, //建议忽略
plugins: [
typescript({
[b]cacheRoot:workSpaceDir + "/.rpt2_cache",
[/b] tsconfig:workSpaceDir + "/tsconfig.json",
check: true, //Set to false to avoid doing any diagnostic checks on the code
tsconfigOverride:{compilerOptions:{removeComments: true}},
include:/.*.ts/,
}),
glsl({
// By default, everything gets included
include: /.*(.glsl|.vs|.fs)$/,
sourceMap: false,
compress:false
}),
/*terser({
output: {
},
numWorkers:1,//Amount of workers to spawn. Defaults to the number of CPUs minus 1
sourcemap: false
})*/
]
}).then(bundle => {
return bundle.write({
file: workSpaceDir + '/bin/js/bundle.js',
format: 'iife',
name: 'laya',
sourcemap: false
});
}).catch(err=>{
console.log(err);

})
});
五、完成以上步骤配置,即可脱离layaIDE而使用vscode编译项目了^_^
D:\workspace\layabox\node-v7.9.0-win-x64\node.exe --inspect-brk=48982 ..\LayaAirIDE_beta\resources\app\node_modules\gulp\bin\gulp.js --cwd=D:\workspace\layabox\LayaAirIDE_beta\resources\app --gulpfile=D:\workspace\layabox\DepthTextureTest/.laya/compile.js compile 
Debugger listening on port 48982.
Warning: This is an experimental feature and could change at any time.
laya_ide_time.require.single.gulp-util: 7.87ms
gulp.js:8
laya_ide_time.require.single.pretty-hrtime: 2.2ms
gulp.js:8
laya_ide_time.require.single.chalk: 10.4ms
gulp.js:8
laya_ide_time.require.single.semver: 5.05ms
gulp.js:8
laya_ide_time.require.single.archy: 1.76ms
gulp.js:8
laya_ide_time.require.single.liftoff: 32.5ms
gulp.js:8
laya_ide_time.require.single.tildify: 2.62ms
gulp.js:8
laya_ide_time.require.single.interpret: 1.57ms
gulp.js:8
laya_ide_time.require.single.v8flags: 6.1ms
gulp.js:8
laya_ide_time.require.single.../lib/completion: 0.875ms
gulp.js:8
laya_ide_time.require.single.minimist: 2.13ms
gulp.js:8
laya_ide_time.require.single.../lib/taskTree: 0.718ms
gulp.js:8
laya_ide_time.require.single.path: 0.0239ms
gulp.js:8
laya_ide_time.gulp.all.require: 82.6ms
gulp.js:27
laya_ide_time.require.single.../package: 0.696ms
gulp.js:8
laya_ide_time.gulp.exec: 5.33ms
gulp.js:251
env.modulePackage:3.9.1
gulp.js:119
Working directory changed to D:\workspace\layabox\LayaAirIDE_beta\resources\app
index.js:36
laya_ide_time.require.single.D:\workspace\layabox\DepthTextureTest/.laya/compile.js: 594ms
gulp.js:8
Using gulpfile D:\workspace\layabox\DepthTextureTest\.laya\compile.js
index.js:36
laya_ide_time.require.single.D:\workspace\layabox\LayaAirIDE_beta\resources\app\node_modules\gulp\index.js: 0.158ms
gulp.js:8
Starting 'compile'...
index.js:36
laya_ide_time.gulp.taskcompile: 2.53e+03ms
gulp.js:216
Finished 'compile' after 2.53 s
index.js:36
laya.ide.complete
已邀请:

Calvin

赞同来自:

使用回layaIDE后编译会报错,可以改成这样
let ideModuleDir;
let workSpaceDir;
for(var argv of process.argv)
{
if(argv.indexOf("gulp.js") > -1)
{
ideModuleDir = argv.split("node_modules")[0] + "node_modules\\";
ideModuleDir = ideModuleDir.charAt(0).toUpperCase() + ideModuleDir.substr(1);
}
else if(argv.indexOf("--gulpfile=") == 0)
{
workSpaceDir = argv.split("=")[1].replace("/.laya/compile.js", "").replace("\\.laya\\compile.js", "");
}
}

要回复问题请先

商务合作
商务合作