1 Star 0 Fork 56

luckyhudi/LayaAir

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
gulpfile.js 30.24 KB
一键复制 编辑 原始数据 按行查看 历史
guzhu 提交于 2024-09-05 15:43 . style: fix d.ts errors
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
const path = require('path');
const fs = require('fs');
const rimrafSync = require('rimraf').sync;
const matched = require('matched');
const ts = require('typescript');
const gulp = require('gulp');
const gulpts = require('gulp-typescript');
const concat = require('gulp-concat');
const inject = require('gulp-inject-string');
const sourcemaps = require('gulp-sourcemaps');
const rollup = require('rollup');
const glsl = require('rollup-plugin-glsl');
const rollupSourcemaps = require('rollup-plugin-sourcemaps');
const merge = require('merge2');
const tscOutPath = "./bin/tsc/";
const sourcemap = true;
//引用插件模块
const typescript = require('rollup-plugin-typescript2'); //typescript2 plugin
const samplesBathURL = './src/samples';
//编译新的库文件只需要在packsDef中配置一下新的库就可以了
const packsDef = [{
'libName': "core",
'input': [
'./layaAir/Decorators.ts',
'./layaAir/Config.ts',
'./layaAir/laya/Const.ts',
'./layaAir/laya/ModuleDef.ts',
'./layaAir/ILaya.ts',
'./layaAir/Laya.ts',
'./layaAir/LayaEnv.ts',
'./layaAir/laya/components/**/*.*',
'./layaAir/laya/display/**/*.*',
'./layaAir/laya/effect/**/*.*',
'./layaAir/laya/events/**/*.*',
'./layaAir/laya/filters/**/*.*',
'./layaAir/laya/layagl/**/*.*',
'./layaAir/laya/loaders/**/*.*',
'./layaAir/laya/maths/**/*.*',
'./layaAir/laya/media/**/*.*',
'./layaAir/laya/net/**/*.*',
// './layaAir/laya/RenderEngine/**/*.*',
'./layaAir/laya/RenderEngine/RenderEngine/NativeGLEngine/**/*.*',
'./layaAir/laya/RenderEngine/RenderEngine/WebGLEngine/**/*.*',
// './layaAir/laya/RenderEngine/RenderEngine/WebGPUEngine/**/*.*',
'./layaAir/laya/RenderEngine/RenderEnum/**/*.*',
'./layaAir/laya/RenderEngine/RenderInterface/**/*.*',
'./layaAir/laya/RenderEngine/RenderShader/**/*.*',
'./layaAir/laya/RenderEngine/*.*',
'./layaAir/laya/renders/**/*.*',
'./layaAir/laya/resource/**/*.*',
'./layaAir/laya/system/**/*.*',
'./layaAir/laya/utils/**/*.*',
'./layaAir/laya/html/**/*.*',
'./layaAir/laya/webgl/**/*.*',
'./layaAir/Config3D.ts',
],
},
{
'libName': "d3",
'input': [
'./layaAir/laya/d3/animation/**/*.*',
'./layaAir/laya/d3/component/**/*.*',
'./layaAir/laya/d3/core/**/*.*',
'./layaAir/laya/d3/depthMap/*.*',
'./layaAir/laya/d3/graphics/**/*.*',
'./layaAir/laya/d3/loaders/**/*.*',
'./layaAir/laya/d3/math/**/*.*',
'./layaAir/laya/d3/resource/**/*.*',
'./layaAir/laya/d3/shader/**/*.*',
'./layaAir/laya/d3/shadowMap/**/*.*',
'./layaAir/laya/d3/text/**/*.*',
'./layaAir/laya/d3/utils/**/*.*',
'./layaAir/laya/d3/WebXR/**/*.*',
'./layaAir/laya/d3/Input3D.ts',
'./layaAir/laya/d3/MouseTouch.ts',
'./layaAir/laya/d3/Touch.ts',
'./layaAir/laya/d3/ModuleDef.ts',
//'./layaAir/laya/d3/RenderObjs/**/*.*',
'./layaAir/laya/d3/RenderObjs/NativeOBJ/*.*',
'./layaAir/laya/d3/RenderObjs/RenderObj/*.*',
'./layaAir/laya/d3/RenderObjs/IRenderEngine3DOBJFactory.ts',
'./layaAir/laya/d3/RenderObjs/Laya3DRender.ts',
'./layaAir/laya/d3/ModuleDef.ts',
'./layaAir/ILaya3D.ts',
'./layaAir/Laya3D.ts',
// interface and enum
'./layaAir/laya/Physics3D/interface/**/*.*',
'./layaAir/laya/Physics3D/physicsEnum/**/*.*',
'./layaAir/laya/d3/physics/HitResult.ts',
'./layaAir/laya/d3/physics/PhysicsSettings.ts',
'./layaAir/laya/d3/physics/Collision.ts',
'./layaAir/laya/d3/physics/ContactPoint.ts',
],
},
{
'libName': "physics3D",
'input': [
'./layaAir/laya/d3/physics/constraints/**/*.*',
'./layaAir/laya/d3/physics/shape/**/*.*',
'./layaAir/laya/d3/physics/ModuleDef.ts',
'./layaAir/laya/d3/physics/CharacterController.ts',
'./layaAir/laya/d3/physics/Constraint3D.ts',
'./layaAir/laya/d3/physics/PhysicsCollider.ts',
'./layaAir/laya/d3/physics/PhysicsColliderComponent.ts',
'./layaAir/laya/d3/physics/PhysicsUpdateList.ts',
'./layaAir/laya/d3/physics/RaycastVehicle.ts',
'./layaAir/laya/d3/physics/RaycastWheel.ts',
'./layaAir/laya/d3/physics/Rigidbody3D.ts',
],
},
{
'libName': "gltf",
'input': [
'./layaAir/laya/gltf/**/*.*',
],
},
{
'libName': "bullet",
'input': [
// use this compile order to solve C_D problem
'./layaAir/laya/Physics3D/Bullet/btPhysicsCreateUtil.ts',
'./layaAir/laya/Physics3D/Bullet/Collider/**/*.*',
'./layaAir/laya/Physics3D/Bullet/Shape/**/*.*',
'./layaAir/laya/Physics3D/Bullet/Joint/**/*.*',
'./layaAir/laya/Physics3D/Bullet/btInteractive.ts',
'./layaAir/laya/Physics3D/Bullet/CollisionTool.ts',
'./layaAir/laya/Physics3D/Bullet/btPhysicsManager.ts',
'./layaAir/laya/Physics3D/Bullet/**/*.*',
],
},
{
'libName': "physX",
'input': [
'./layaAir/laya/Physics3D/PhysX/pxPhysicsCreateUtil.ts',
'./layaAir/laya/Physics3D/PhysX/Collider/**/*.*',
'./layaAir/laya/Physics3D/PhysX/Shape/**/*.*',
'./layaAir/laya/Physics3D/PhysX/Joint/**/*.*',
'./layaAir/laya/Physics3D/PhysX/pxPhysicsManager.ts',
'./layaAir/laya/Physics3D/PhysX/pxPhysicsMaterial.ts',
'./layaAir/laya/Physics3D/PhysX/**/*.*',
],
},
{
'libName': 'device',
'input': [
'./layaAir/laya/device/**/*.*'
],
},
{
'libName': 'tiledmap',
'input': [
'./layaAir/laya/map/**/*.*'
],
},
{
'libName': 'physics2D',
'input': [
'./layaAir/laya/physics/Collider2D/*.*',
'./layaAir/laya/physics/joint/*.*',
'./layaAir/laya/physics/IPhysiscs2DFactory.ts',
'./layaAir/laya/physics/ModuleDef.ts',
'./layaAir/laya/physics/Physics2D.ts',
'./layaAir/laya/physics/Physics2DOption.ts',
'./layaAir/laya/physics/RigidBody.ts',
'./layaAir/laya/physics/RigidBody2DInfo.ts',
'./layaAir/laya/physics/Physics2DDebugDraw.ts',
],
},
{
'libName': 'box2D',
'input': [
'./layaAir/laya/physics/factory/physics2DwasmFactory.ts',
],
},
{
'libName': 'box2D.wasm',
'input': [
'./layaAir/laya/physics/factory/physics2DwasmFactory.ts',
],
},
{
'libName': 'ui',
'input': [
'./layaAir/laya/ui/**/*.*',
'./layaAir/UIConfig.ts',
],
},
{
'libName': 'spine',
'input': [
'./layaAir/laya/spine/**/*.*'
],
},
{
'libName': 'ani',
'input': [
'./layaAir/laya/ani/**/*.*'
],
},
{
'libName': 'debugtool',
'input': [
'./extensions/debug/**/*.*'
],
},
{
"libName": 'performancetool',
'input': [
'./extensions/performanceTool/**/*.*'
],
}
];
/*
并非所有循环引用都会引起加载问题,如果两个模块只是使用对方的类型声明,没有使用继承/构造行为,是允许的。
这里忽略这类情况。
*/
const ignoreCirclarDependencyWarnings = true;
const onwarn = warning => {
let msg = warning.message;
if (warning.code === 'CIRCULAR_DEPENDENCY') {
if (ignoreCirclarDependencyWarnings)
return;
let arr = msg.split("->");
arr = arr.map(e => {
e = e.trim();
return path.basename(e, path.extname(e));
});
msg = arr.join(" -> ");
msg = "(C_D) " + msg;
console.warn(msg);
} else
console.warn(warning);
}
gulp.task('compileLayaAir', () => {
rimrafSync(tscOutPath + 'layaAir');
const proj = gulpts.createProject("./src/layaAir/tsconfig.json", {
removeComments: true,
});
return merge(
proj.src()
.pipe(sourcemaps.init())
.pipe(proj())
.pipe(sourcemaps.write('.', {
sourceRoot: './',
includeContent: false
}))
.pipe(gulp.dest(tscOutPath + 'layaAir')),
gulp.src([
'./src/layaAir/**/*.vs',
'./src/layaAir/**/*.fs',
'./src/layaAir/**/*.glsl',
'./src/layaAir/**/*.wgsl'
], {
base: "src"
})
.pipe(gulp.dest(tscOutPath))
);
});
gulp.task('compileExtension', () => {
rimrafSync(tscOutPath + 'extensions');
const proj = gulpts.createProject("./src/extensions/tsconfig.json", {
removeComments: true
});
return proj.src().pipe(proj()).pipe(gulp.dest(tscOutPath + 'extensions'));
});
gulp.task('compile', gulp.series('compileLayaAir', 'compileExtension'));
gulp.task("buildJs", async () => {
rimrafSync("./build/libs");
const rootPath = process.cwd();
const outPath = path.join(rootPath, tscOutPath);
const mentry = '[entry]';
function myMultiInput(pkgDef, files, fileSet) {
return {
resolveId(id, importer) {
if (id === mentry)
return mentry;
if (importer == null)
return;
var ext = path.extname(id);
if (ext == ".js" || ext == "") {
var importfile = path.join(importer === mentry ? rootPath : path.dirname(importer), id);
if (ext == "")
importfile += ".js";
if (!fileSet.has(importfile)) {
if (pkgDef.libName == "core")
console.warn(`external: ${path.relative(outPath, importer)} ==> ${path.relative(outPath, importfile)}`);
return {
id: 'Laya',
external: true
};
}
}
},
load(id) {
if (id === mentry)
return files.map(ele => `export * from ${JSON.stringify(tscOutPath + ele)};`).join('\n');
}
};
}
async function getFiles(input) {
var include = [];
var exclude = [];
if (typeof input === 'string') {
include = [input];
} else if (Array.isArray(input)) {
include = input;
} else {
include = input.include || [];
exclude = input.exclude || [];
}
var patterns = include.concat(exclude.map(function (pattern) {
return '!' + pattern;
}));
return matched.promise(patterns, {
cwd: path.join(process.cwd(), "./src"),
realpath: false
});
}
for (let i = 0; i < packsDef.length; ++i) {
let files = await getFiles(packsDef[i].input);
files = files.filter(ele => ele.endsWith(".ts")).map(ele => ele = ele.substring(0, ele.length - 3) + ".js");
let fileSet = new Set(files.map(ele => path.normalize(outPath + ele)));
let config = {
input: mentry,
output: {
extend: true,
globals: {
'Laya': 'Laya'
}
},
external: ['Laya'],
onwarn: onwarn,
plugins: [
myMultiInput(packsDef[i], files, fileSet),
rollupSourcemaps(),
glsl({
include: /.*(.glsl|.vs|.fs)$/,
sourceMap: sourcemap,
compress: false
})
],
};
let outputOption = {
file: path.join("./build/libs", "laya." + packsDef[i].libName + ".js"),
format: 'iife',
esModule: false,
name: 'Laya',
globals: {
'Laya': 'Laya'
},
sourcemap: sourcemap
};
if (packsDef[i].libName != "core")
outputOption.extend = true;
const bundle = await rollup.rollup(config);
await bundle.write(outputOption);
}
await new Promise(resolve => {
merge(packsDef.map(pack => {
return gulp.src(path.join("./build/libs", "laya." + pack.libName + ".js"))
.pipe(inject.replace(/var Laya = \(function \(exports.*\)/, "window.Laya = (function (exports)"))
.pipe(inject.replace(/}\)\({}, Laya\);/, "})({});"))
.pipe(inject.replace(/Laya\$1\./g, "exports."))
.pipe(inject.replace(/\(this.Laya = this.Laya \|\| {}, Laya\)/, "(window.Laya = window.Laya || {}, Laya)"))
.pipe(gulp.dest(process.platform == 'win32' ? '.' : './build/libs')); //在win下dest竟然突然变成src的相对目录
})).on("queueDrain", resolve);
});
});
//拷贝引擎的第三方js库
gulp.task("copyJsLibs", async () => {
return gulp.src([
'./src/layaAir/jsLibs/bullet.wasm',
'./src/layaAir/jsLibs/*.js',
'./src/layaAir/jsLibs/physx.release.wasm',
'./src/layaAir/jsLibs/laya.Box2D.wasm.wasm',
'!./src/layaAir/jsLibs/{laya.Box2D.js,cannon.js,bullet.js,physx.release.js,laya.Box2D.wasm.js,bullet.wasm.js,physx.wasm.js,physx.release.js.mem}'
])
.pipe(gulp.dest('./build/libs'));
});
//合并physics2D 和 box2d
gulp.task('buildBox2dPhysics', () => {
return gulp.src([
'./build/libs/laya.box2D.js',
'./src/layaAir/jsLibs/laya.Box2D.js',
]).pipe(concat('laya.box2D.js'))
.pipe(gulp.dest('./build/libs/'));
});
gulp.task('buildBox2dWasmPhysics', () => {
return gulp.src([
'./build/libs/laya.box2D.wasm.js',
'./src/layaAir/jsLibs/laya.Box2D.wasm.js',
]).pipe(concat('laya.box2D.wasm.js'))
.pipe(gulp.dest('./build/libs/'));
});
//合并bullet物理引擎库 和 编译出来的physics.bullet.js
gulp.task('buildBulletPhysics', () => {
return gulp.src([
'./build/libs/laya.bullet.js',
'./src/layaAir/jsLibs/bullet.js',
]).pipe(concat('laya.bullet.js'))
.pipe(gulp.dest('./build/libs/'));
});
//合并bullet的wasm物理库 和 编译出来的physics.bullet.js
gulp.task('buildBulletWASMPhysics', () => {
return gulp.src([
'./build/libs/laya.bullet.js',
'./src/layaAir/jsLibs/bullet.wasm.js',
]).pipe(concat('laya.bullet.wasm.js'))
.pipe(gulp.dest('./build/libs/'));
});
//合并physX的wasm物理引擎库 和 编译出来的physics.physX.js
gulp.task('buildPhysXWASMPhysics', () => {
return gulp.src([
'./build/libs/laya.physX.js',
'./src/layaAir/jsLibs/physx.wasm.js',
])
.pipe(concat('laya.physX.wasm.js'))
.pipe(gulp.dest('./build/libs/'));
});
//合并physX物理引擎库 和 编译出来的physics.physX.js
gulp.task('buildPhysXPhysics', () => {
return gulp.src([
'./build/libs/laya.physX.js',
'./src/layaAir/jsLibs/physx.release.js',
])
.pipe(concat('laya.physX.js'))
.pipe(gulp.dest('./build/libs/'));
});
gulp.task('genDts', () => {
rimrafSync("./build/temp");
rimrafSync("./build/types");
async function genDts() {
const dtsContents = [];
const dtsContentsTop = [];
const SyntaxKind = ts.SyntaxKind;
function processTree(sourceFile, rootNode, replacer) {
let code = '';
let cursorPosition = rootNode.pos;
function skip(node) {
cursorPosition = node.end;
}
function readThrough(node) {
code += sourceFile.text.slice(cursorPosition, node.pos);
cursorPosition = node.pos;
}
function visit(node) {
readThrough(node);
const replacement = replacer(node);
if (replacement != null) {
code += replacement;
skip(node);
} else {
ts.forEachChild(node, visit);
}
}
visit(rootNode);
code += sourceFile.text.slice(cursorPosition, rootNode.end);
return code;
}
let files = await matched.promise("./build/temp/**/*.d.ts", {
realpath: true,
nosort: false
});
for (let file of files) {
let file2 = file.replace(/\\/g, '/');
if (file2.indexOf("laya/RenderEngine/RenderEngine/WebGPUEngine/") != -1
|| file2.indexOf("laya/d3/RenderObjs/WebGPUOBJ/") != -1)
continue;
let inNamespace = !file.endsWith("Laya.d.ts") && !file.endsWith("Laya3D.d.ts");
let code = fs.readFileSync(file, "utf-8");
let declarationFile = ts.createSourceFile(file, code, ts.ScriptTarget.Latest, true);
function visitNode(node) {
if (node.kind == SyntaxKind.ImportDeclaration || node.kind == SyntaxKind.ImportEqualsDeclaration) { //删除所有import语句
return '';
} else if (node.kind == SyntaxKind.ExportDeclaration) { //something like "export xx;"
return '';
} else if (node.kind == SyntaxKind.ExportKeyword) { //删除所有export语句
let code = declarationFile.text.slice(node.pos, node.end);
return code.substring(0, code.length - 6);
} else if ((node.kind == SyntaxKind.DeclareKeyword || node.kind == SyntaxKind.ModuleDeclaration) && inNamespace) { //删除declare
return '';
} else if (node.kind == SyntaxKind.TypeReference) {
let code = declarationFile.text.slice(node.pos, node.end);
code = code.substring(1);
if (!inNamespace && code.indexOf(".") == -1 && !code.startsWith("Promise"))
return " Laya." + code;
else if (code.startsWith("glTF."))
return " " + code.substring(5);
}
//console.log(node.kind, node.parent?.kind, node.text);
}
const content = processTree(declarationFile, declarationFile, visitNode).trimEnd();
if (content.length == 0)
continue;
if (inNamespace) {
let lines = content.split("\n");
dtsContents.push(lines.map(l => " " + l).join("\n"));
} else
dtsContentsTop.push(content);
}
//pretty print
let code = dtsContentsTop.join("\n\n") +
"\n\ndeclare module Laya {\n\n" +
dtsContents.join("\n\n") +
"\n\n}";
let declarationFile = ts.createSourceFile("./build/types/LayaAir.d.ts", code, ts.ScriptTarget.Latest, true);
code = ts.createPrinter().printFile(declarationFile);
fs.writeFileSync("./build/types/LayaAir.d.ts", code);
rimrafSync("./build/temp");
}
const proj = gulpts.createProject("./src/layaAir/tsconfig.json", {
declaration: true,
removeComments: false,
});
return merge(
proj.src().pipe(proj()).dts.pipe(gulp.dest("./build/temp")).on("end", genDts),
gulp.src(['./src/layaAir/tslibs/*.*']).pipe(gulp.dest('./build/types')),
);
});
gulp.task('build',
gulp.series(
'compile',
'buildJs',
'copyJsLibs',
'buildBox2dPhysics',
'buildBox2dWasmPhysics',
'buildBulletWASMPhysics',
'buildBulletPhysics',
'buildPhysXWASMPhysics',
'buildPhysXPhysics',
'genDts',
));
/**
* 主要用来给laya库加上所有的Laya.xx=xx
* 主要用在
* 1. 分包的时候统计laya文件
* 2. 打包的时候导出Laya
* addLayaExpAt:string 打包的最后会替换这个字符串,加上Laya.xx=xx
* layaPath:laya所在目录。这个目录下的是laya文件,可以用来收集laya文件或者判断需要导出的类
* isLayaLib:boolean 当前打包是否是laya目录,是的话表示强制加 Laya.xx 不再判断目录
* gatherExtFiles:string[] 收集用到的laya文件。这表示是分包模式
* baseUrl:string 设置baseurl,只有分包模式用到
*/
function layaExpPlugin(options) {
let dirname = __dirname; //process.cwd();
let opt = options;
let layaPath = null;
let layafiles = null;
let baseUrl = null;
if (opt) {
layafiles = opt.gatherExtFiles;
if (layafiles && !(layafiles instanceof Array)) {
throw 'gatherExtFiles should be an Array';
}
layaPath = options.layaPath;
if (layaPath)
layaPath = path.resolve(dirname, layaPath);
baseUrl = opt.baseUrl;
if (baseUrl) {
baseUrl = path.resolve(dirname, baseUrl);
}
}
function isLayaPath(id) {
if (!layaPath)
return false;
let r = path.relative(layaPath, id);
return !r.startsWith('..');
}
return ({
load(id) { },
resolveId(id, importer) {
if (!importer)
return;
if (!layafiles) // 不收集laya文件,表示是整体打包。不排除laya文件
return;
let importfile;
if (id.startsWith('..') || id.startsWith('.'))
importfile = path.join(path.dirname(importer), id);
else if (baseUrl) {
importfile = path.join(baseUrl, id);
}
if (isLayaPath(importfile)) {
let tsfile = importfile;
tsfile += '.ts';
if (layafiles.indexOf(tsfile) < 0)
layafiles.push(tsfile);
return 'Laya';
} else { }
},
renderChunk(code, chunk, options) {
let replacestr = opt.addLayaExpAt;
let SourceMap = null;
let _code = code;
if (!replacestr)
return {
code: _code,
map: SourceMap
};
let p = code.lastIndexOf(replacestr);
if (p < 0)
return {
code: _code,
map: SourceMap
};
let expstr = 'Laya=window.Laya;\n';
let islayalib = opt.isLayaLib;
for (let mod in chunk.modules) {
if (!islayalib && !isLayaPath(mod))
continue;
// 所有的laya模块都导出
chunk.modules[mod].renderedExports.forEach(m => {
if (m === 'default') return;
if (m === 'Laya') return;
expstr += 'Laya.' + m + '=' + m + '\n';
});
}
// 插入导出的模块
let st = 'window.Laya=window.Laya||{};\n';
_code = st + code.substr(0, p) + expstr + code.substr(p + replacestr.length);
// console.log(_code);
return {
code: _code,
map: SourceMap
}
}
});
}
let baseurl = __dirname;
let layaFiles = [
path.join(baseurl, "./src/", "layaAir", "Laya.ts"),
path.join(baseurl, "./src/", "layaAir", "laya", "net", "HttpRequest.ts"),
path.join(baseurl, "./src/", "layaAir", "laya", "resource", "Resource.ts"),
path.join(baseurl, "./src/", "layaAir", "laya", "resource", "Texture.ts"),
path.join(baseurl, "./src/", "layaAir", "laya", "media", "SoundChannel.ts"),
path.join(baseurl, "./src/", "layaAir", "laya", "events", "EventDispatcher.ts"),
path.join(baseurl, "./src/", "layaAir", "laya", "utils", "Browser.ts"),
path.join(baseurl, "./src/", "layaAir", "laya", "utils", "RunDriver.ts"),
path.join(baseurl, "./src/", "layaAir", "laya", "display", "Input.ts"),
path.join(baseurl, "./src/", "layaAir", "laya", "net", "Loader.ts"),
path.join(baseurl, "./src/", "layaAir", "laya", "net", "LocalStorage.ts"),
path.join(baseurl, "./src/", "layaAir", "Config.ts"),
];
let layaexpreplace = '//__LAYARPLACEMENTHERE__//';
var curPackFiles = null; //当前包的所有的文件
var mentry = 'multientry.ts';
function mySamplesMultiInput(options) {
let packPath = options ? options.path : null; // 除了制定输入以外,这个目录下的也可以认为是内部文件,可以引用
if (packPath && !path.isAbsolute(packPath)) {
packPath = path.join(__dirname, packPath);
}
function pathInPack(p) {
if (!packPath)
return true; // 没有设置,则认为true,
let r = path.relative(packPath, p);
if (r.startsWith('..')) //TODO 如果盘符都变了这样是不对的
return false;
return true;
}
var include = [];
var exclude = [];
function configure(config) {
if (typeof config === 'string') {
include = [config];
} else if (Array.isArray(config)) {
include = config;
} else {
include = config.include || [];
exclude = config.exclude || [];
if (config.exports === false) {
exporter = function exporter(p) {
if (p.substr(p.length - 3) == '.ts') {
p = p.substr(0, p.length - 3);
}
return `import ${JSON.stringify(p)};`;
};
}
}
}
var exporter = function exporter(p) {
if (p.substr(p.length - 3) == '.ts') {
p = p.substr(0, p.length - 3);
}
return `export * from ${JSON.stringify(p)};`;
};
return ({
options(options) {
configure(options.input);
options.input = mentry;
},
resolveId(id, importer) { //entry是个特殊字符串,rollup并不识别,所以假装这里解析一下
if (id === mentry) {
return mentry;
}
if (mentry == importer)
return;
var importfile = path.join(path.dirname(importer), id);
var ext = path.extname(importfile);
if (ext != '.ts' && ext != '.glsl' && ext != '.vs' && ext != '.ps' && ext != '.fs') {
importfile += '.ts';
}
if (importfile.endsWith('.json')) {
console.log('import ', importfile);
}
if (curPackFiles.indexOf(importfile) < 0 && !pathInPack(importfile)) {
//其他包里的文件
// console.log('other pack:',id,'impo rter=', importer);
return 'Laya';
}
},
load(id) {
if (id === mentry) {
if (!include.length) {
return Promise.resolve('');
}
var patterns = include.concat(exclude.map(function (pattern) {
return '!' + pattern;
}));
return matched.promise(patterns, {
realpath: true
}).then(function (paths) {
curPackFiles = paths; // 记录一下所有的文件
return paths.map(exporter).join('\n');
});
} else {
// console.log('load ',id);
}
}
});
}
gulp.task('compileSamples', async (cb) => {
let bundleobj = {
tsconfig: samplesBathURL + '/tsconfig.json',
check: false, //Set to false to avoid doing any diagnostic checks on the code
tsconfigOverride: {
compilerOptions: {
removeComments: true
}
},
include: samplesBathURL + "/**/*.ts"
}
await rollup.rollup({
input: samplesBathURL + '/index.ts',
treeshake: false, //建议忽略
onwarn: (waring, warn) => {
if (ignoreCirclarDependencyWarnings) {
return
} else {
console.log("warnning Circular dependency:");
console.log(waring);
}
},
external: ['Laya'],
plugins: [
//mySamplesMultiInput(),
layaExpPlugin({
baseUrl: './src/layaAir',
layaPath: './src/layaAir', // 收集需要的laya文件
gatherExtFiles: layaFiles,
//addLayaExpAt:layaexpreplace,
}),
typescript(bundleobj),
glsl({
// By default, everything gets included
include: /.*(.glsl|.vs|.fs)$/,
sourceMap: false,
compress: false
}),
]
}).then(bundle => {
console.log("write bundle!")
return bundle.write({
file: './bin/rollUp/bundle.js',
format: 'iife',
name: 'Laya',
extend: true,
globals: {
'Laya': 'Laya'
},
sourcemap: false,
banner: 'window.Laya=window.Laya||{};\n',
});
});
console.time("compile laya");
let layaobj = {
tsconfig: './src/layaAir/tsconfig.json',
check: false,
tsconfigOverride: {
compilerOptions: {
removeComments: true
}
},
include: /.*(.ts)$/
}
await rollup.rollup({
input: layaFiles,
onwarn: (waring, warn) => {
if (ignoreCirclarDependencyWarnings) {
return
} else {
console.log("warnning Circular dependency:");
console.log(waring);
}
},
treeshake: false, //建议忽略
plugins: [
mySamplesMultiInput({
path: './src/layaAir'
}),
typescript(layaobj),
glsl({
include: /.*(.glsl|.vs|.fs)$/,
sourceMap: false,
compress: false
}),
]
}).then(bundle => {
console.log("write laya");
return bundle.write({
file: './bin/rollUp/laya.js',
format: 'iife',
name: 'Laya',
sourcemap: false,
//banner: 'window.Laya=window.Laya||{};\n',
});
}).catch(err => {
console.log(err);
});
console.timeEnd("compile laya");
cb();
});
gulp.task('changeLayaJS', (cb) => {
// 发布时调用编译功能,判断是否点击了编译选项
let layajsPath = path.join("./", "bin/rollUp", "laya.js");
let layajsCon = fs.readFileSync(layajsPath, "utf8");
layajsCon = layajsCon.replace(/^var Laya = /mg, "");
layajsCon = layajsCon.replace(/\({}\);\s*\n*$/mg, "(window.Laya = window.Laya || {});");
fs.writeFileSync(layajsPath, layajsCon, "utf8");
cb();
});
gulp.task('buildSamples',
gulp.series(
'compileSamples',
'changeLayaJS',
));
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
TypeScript
1
https://gitee.com/luckyhudi/LayaAir.git
[email protected]:luckyhudi/LayaAir.git
luckyhudi
LayaAir
LayaAir
LayaAir_3.1

搜索帮助