@playcraft/devkit 1.0.31-beta.14 → 1.0.31-beta.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1 +1,5 @@
|
|
|
1
|
-
"use strict";const fs=require("fs");const path=require("path");const{options}=require("../../options.js");const{mergeOptions}=require("../../utils/mergeOptions.js");const{logOptions}=require("../../utils/logOptions.js");const{resolveCreatorOutputDir}=require("./creator-cli");const{runCocosEngineBuild}=require("./cli-build");const{rebundlePreviewMerge}=require("./rebundle-preview.js");const{mergeWebMobile}=require("./merge");const{detectCurrentTheme,loadThemePayload,writeGeneratedThemeModule}=require("./theme-assets");const{listThemeAssetNames,themeAssetsDirForProject}=require("./omit-playable-weight");const{emitChannel}=require("../../artifacts/emit");const{isSocialMediaNetwork,stripSocialShareFromDir}=require("./strip-social-share");function resolveWebMobileDir(root,buildOptions){const cocos=buildOptions.cocos||{};const fromCreator=resolveCreatorOutputDir(root,cocos.buildConfig||"build-configs/web-mobile.json");const explicit=cocos.webMobileDir?path.resolve(root,cocos.webMobileDir):null;if(cocos.skipEngineBuild){return explicit||fromCreator||path.resolve(root,"dist/web-mobile")}if(fromCreator){if(explicit&&explicit!==fromCreator){console.warn(`[cocos] webMobileDir (${path.relative(root,explicit)}) != Creator output (${path.relative(root,fromCreator)}); using Creator buildPath/outputName`)}return fromCreator}return explicit||path.resolve(root,"dist/web-mobile")}async function mergeTemplate(root,buildOptions){const webMobileDir=resolveWebMobileDir(root,buildOptions);const singleFilePath=path.join(root,"build","single-file-3x.html");const themeConfig=loadBuildsThemeConfig(root);const themeName=resolveThemeName(root,buildOptions,themeConfig);const themeAssetNames=listThemeAssetNames(themeAssetsDirForProject(root,themeName,themeConfig));await mergeWebMobile(webMobileDir,singleFilePath,{enableSplash:!!(buildOptions.cocos&&buildOptions.cocos.enableSplash),obfuscateLevel:buildOptions.obfuscateLevel,themeAssetNames,isZip:false});return fs.readFileSync(singleFilePath,"utf8")}function resolveThemeName(root,buildOptions,themeConfig){return buildOptions.themeName||detectCurrentTheme(root,themeConfig)||"default"}function loadBuildsPackConfig(root){const configPath=path.join(root,"builds.config.js");if(!fs.existsSync(configPath))return{};try{delete require.cache[configPath];return require(configPath)||{}}catch{return{}}}function cliSetsFflate(){return process.argv.slice(2).some(arg=>arg==="--fflate-compression"||arg.startsWith("--fflate-compression="))}/** builds.config.js owns fflate for Cocos unless the CLI flag is present. */function withBuildsPackFlags(root,buildOptions){const config=loadBuildsPackConfig(root);if(typeof config.fflateCompression!=="boolean"||cliSetsFflate())return buildOptions;return{...buildOptions,fflateCompression:config.fflateCompression}}function loadBuildsThemeConfig(root){const configPath=path.join(root,"builds.config.js");if(!fs.existsSync(configPath))return{sourceDir:"src/theme",entryFile:"src/theme/index.ts"};try{delete require.cache[configPath];const config=require(configPath);return{sourceDir:"src/theme",entryFile:"src/theme/index.ts",...(config.themes||{})}}catch{return{sourceDir:"src/theme",entryFile:"src/theme/index.ts"}}}function stripPackedChannel(outDir,network){if(isSocialMediaNetwork(network)){console.log("[cocos] social_media: keep AiX hooks (inject IIFE later)");return}const packed=stripSocialShareFromDir(outDir);console.log(`[cocos] stripped social-share from ${packed.changed}/${packed.files} files in ${path.relative(process.cwd(),outDir)||"."}`)}
|
|
1
|
+
"use strict";const fs=require("fs");const path=require("path");const{options}=require("../../options.js");const{mergeOptions}=require("../../utils/mergeOptions.js");const{logOptions}=require("../../utils/logOptions.js");const{resolveCreatorOutputDir}=require("./creator-cli");const{runCocosEngineBuild}=require("./cli-build");const{rebundlePreviewMerge}=require("./rebundle-preview.js");const{mergeWebMobile}=require("./merge");const{detectCurrentTheme,loadThemePayload,writeGeneratedThemeModule}=require("./theme-assets");const{listThemeAssetNames,themeAssetsDirForProject}=require("./omit-playable-weight");const{emitChannel}=require("../../artifacts/emit");const{isSocialMediaNetwork,stripSocialShareFromDir}=require("./strip-social-share");function resolveWebMobileDir(root,buildOptions){const cocos=buildOptions.cocos||{};const fromCreator=resolveCreatorOutputDir(root,cocos.buildConfig||"build-configs/web-mobile.json");const explicit=cocos.webMobileDir?path.resolve(root,cocos.webMobileDir):null;if(cocos.skipEngineBuild){return explicit||fromCreator||path.resolve(root,"dist/web-mobile")}if(fromCreator){if(explicit&&explicit!==fromCreator){console.warn(`[cocos] webMobileDir (${path.relative(root,explicit)}) != Creator output (${path.relative(root,fromCreator)}); using Creator buildPath/outputName`)}return fromCreator}return explicit||path.resolve(root,"dist/web-mobile")}async function mergeTemplate(root,buildOptions){const webMobileDir=resolveWebMobileDir(root,buildOptions);const singleFilePath=path.join(root,"build","single-file-3x.html");const themeConfig=loadBuildsThemeConfig(root);const themeName=resolveThemeName(root,buildOptions,themeConfig);const themeAssetNames=listThemeAssetNames(themeAssetsDirForProject(root,themeName,themeConfig));await mergeWebMobile(webMobileDir,singleFilePath,{enableSplash:!!(buildOptions.cocos&&buildOptions.cocos.enableSplash),obfuscateLevel:buildOptions.obfuscateLevel,themeAssetNames,isZip:false});return fs.readFileSync(singleFilePath,"utf8")}function resolveThemeName(root,buildOptions,themeConfig){return buildOptions.themeName||detectCurrentTheme(root,themeConfig)||"default"}function loadBuildsPackConfig(root){const configPath=path.join(root,"builds.config.js");if(!fs.existsSync(configPath))return{};try{delete require.cache[configPath];return require(configPath)||{}}catch{return{}}}function cliSetsFflate(){return process.argv.slice(2).some(arg=>arg==="--fflate-compression"||arg.startsWith("--fflate-compression="))}/** builds.config.js owns fflate for Cocos unless the CLI flag is present. */function withBuildsPackFlags(root,buildOptions){const config=loadBuildsPackConfig(root);if(typeof config.fflateCompression!=="boolean"||cliSetsFflate())return buildOptions;return{...buildOptions,fflateCompression:config.fflateCompression}}function loadBuildsThemeConfig(root){const configPath=path.join(root,"builds.config.js");if(!fs.existsSync(configPath))return{sourceDir:"src/theme",entryFile:"src/theme/index.ts"};try{delete require.cache[configPath];const config=require(configPath);return{sourceDir:"src/theme",entryFile:"src/theme/index.ts",...(config.themes||{})}}catch{return{sourceDir:"src/theme",entryFile:"src/theme/index.ts"}}}function stripPackedChannel(outDir,network){if(isSocialMediaNetwork(network)){console.log("[cocos] social_media: keep AiX hooks (inject IIFE later)");return}const packed=stripSocialShareFromDir(outDir);console.log(`[cocos] stripped social-share from ${packed.changed}/${packed.files} files in ${path.relative(process.cwd(),outDir)||"."}`)}function generatedThemeRel(buildOptions){return buildOptions.cocos&&buildOptions.cocos.generatedThemeFile||"assets/scripts/platform/theme.generated.ts"}/**
|
|
2
|
+
* Channel emit already inlines window.__PLAYCRAFT_THEME_ASSETS__.
|
|
3
|
+
* If those data URLs also live in theme.generated.ts, Cocos bakes a second
|
|
4
|
+
* copy into main/index.js and the HTML grows by the whole theme (~3MB+).
|
|
5
|
+
*/function stripThemeAssetsForEngineCompile(root,themeName,themeConfig,generatedRel){let payload;try{payload=loadThemePayload(root,themeName,themeConfig)}catch(err){console.warn(`[cocos] theme compile strip skipped: ${err.message}`);return null}writeGeneratedThemeModule(path.join(root,generatedRel),{...payload,assetRecordByPath:{}});const bytes=Object.values(payload.assetRecordByPath||{}).reduce((n,v)=>n+String(v).length,0);console.log(`[cocos] omit ${Object.keys(payload.assetRecordByPath||{}).length} THEME_ASSETS from engine compile (${(bytes/1024).toFixed(0)}kb, inject at emit)`);return payload}function restoreGeneratedTheme(root,generatedRel,payload){if(!payload)return;writeGeneratedThemeModule(path.join(root,generatedRel),payload)}async function prepareCocosTemplate(customOptions){const root=process.cwd();const buildOptions=withBuildsPackFlags(root,mergeOptions(options,customOptions));const themeConfig=loadBuildsThemeConfig(root);const themeName=resolveThemeName(root,buildOptions,themeConfig);const generated=generatedThemeRel(buildOptions);const compilePayload=stripThemeAssetsForEngineCompile(root,themeName,themeConfig,generated);try{await runCocosEngineBuild(root,buildOptions);await rebundlePreviewMerge(root);return await mergeTemplate(root,buildOptions)}finally{restoreGeneratedTheme(root,generated,compilePayload)}}async function emitCocosChannel(templateHtml,customOptions){const root=process.cwd();const buildOptions=withBuildsPackFlags(root,mergeOptions(options,customOptions));const themeConfig=loadBuildsThemeConfig(root);const themeName=resolveThemeName(root,buildOptions,themeConfig);let themePayload={themeName,mergedData:{},i18nOverride:{},assetRecordByPath:{}};try{themePayload=loadThemePayload(root,themeName,themeConfig)}catch(err){console.warn(`[cocos] theme payload skipped: ${err.message}`)}const generated=buildOptions.cocos&&buildOptions.cocos.generatedThemeFile||"assets/scripts/platform/theme.generated.ts";writeGeneratedThemeModule(path.join(root,generated),themePayload);const outDir=path.resolve(root,buildOptions.outDir);await emitChannel({templateHtml,buildOptions,themePayload,themeConfig,outDir});stripPackedChannel(outDir,buildOptions.network)}async function runCocosBuild(customOptions){const buildOptions=withBuildsPackFlags(process.cwd(),mergeOptions(options,customOptions));logOptions(buildOptions);const templateHtml=await prepareCocosTemplate(buildOptions);await emitCocosChannel(templateHtml,customOptions)}async function packCocosBatch({config,themes,tasks,outputDir,projectRoot,isChannelFold2Zip}){const buildOptionsBase=mergeOptions(options,{engine:"cocos",googlePlayUrl:config.googlePlayUrl,appStoreUrl:config.appStoreUrl,isChannelFold2Zip,obfuscateLevel:config.obfuscateLevel,fflateCompression:config.fflateCompression});if(config.cocos)Object.assign(buildOptionsBase.cocos,config.cocos);if(buildOptionsBase.cocos.cleanCache!==false&&process.env.PLAYABLE_KEEP_COCOS_CACHE!=="1"){buildOptionsBase.cocos.cleanCache=true}const themeConfig=config.themes||loadBuildsThemeConfig(projectRoot);const compileTheme=themes[0]||detectCurrentTheme(projectRoot,themeConfig)||"default";const generated=generatedThemeRel(buildOptionsBase);const compilePayload=stripThemeAssetsForEngineCompile(projectRoot,compileTheme,themeConfig,generated);let templateHtml;try{await runCocosEngineBuild(projectRoot,buildOptionsBase);await rebundlePreviewMerge(projectRoot);templateHtml=await mergeTemplate(projectRoot,buildOptionsBase)}finally{restoreGeneratedTheme(projectRoot,generated,compilePayload)}const results=[];for(const theme of themes){let themePayload;try{themePayload=loadThemePayload(projectRoot,theme,themeConfig)}catch(err){console.error(`[cocos] theme ${theme}: ${err.message}`);for(const task of tasks){results.push({success:false,theme,channel:task.channel,orientation:task.orientation,error:err.message})}continue}for(const task of tasks){const orientationValue=task.orientation||"both";const fileName=config.naming&&typeof config.naming.generator==="function"?config.naming.generator(theme,task.channel):`${theme}-${task.channel}`;const tempDir=path.join(projectRoot,`${config.build.tempDirPrefix||"dist-temp"}-cocos-${theme}-${task.channel}-${orientationValue}`);try{const channelOptions=mergeOptions(buildOptionsBase,{network:task.channel,playcraftOrientationValue:orientationValue,filename:fileName||options.filename,outDir:tempDir,isChannelFold2Zip});await emitChannel({templateHtml,buildOptions:channelOptions,themePayload,themeConfig,outDir:tempDir});stripPackedChannel(tempDir,task.channel);const themeDir=path.join(outputDir,theme);fs.mkdirSync(themeDir,{recursive:true});for(const file of fs.readdirSync(tempDir)){const src=path.join(tempDir,file);if(fs.statSync(src).isDirectory())continue;fs.copyFileSync(src,path.join(themeDir,file))}results.push({success:true,theme,channel:task.channel,orientation:orientationValue})}catch(error){results.push({success:false,theme,channel:task.channel,orientation:orientationValue,error:error.message})}finally{if(config.build.cleanTemp!==false&&fs.existsSync(tempDir)){fs.rmSync(tempDir,{recursive:true,force:true})}}}}return results}async function syncThemeForPreview(projectRoot,themeName,themeConfig,generatedRel){const payload=loadThemePayload(projectRoot,themeName,themeConfig);writeGeneratedThemeModule(path.join(projectRoot,generatedRel),payload);return payload}module.exports={runCocosBuild,prepareCocosTemplate,emitCocosChannel,packCocosBatch,mergeTemplate,syncThemeForPreview};
|