Skip to content

Commit

Permalink
support bundle-adapter workflow (cocos#11337)
Browse files Browse the repository at this point in the history
* update require

* support bundle-adapter workflow

update

* update adapter require

* add timer

* update

* update lock

update lock

* remove node version for npm test CI

* with node version 16.15

* install jsdom

* use 16.15
  • Loading branch information
PPpro authored Jun 8, 2022
1 parent 5e8f4a2 commit 549108d
Show file tree
Hide file tree
Showing 31 changed files with 38,521 additions and 12,362 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/web-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

strategy:
matrix:
node-version: [12.x]
node-version: [16.15]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/web-npm_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:

- uses: actions/setup-node@v2
with:
node-version: '12.13'
node-version: '16.15'

- name: install gulp
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/web-quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 12
node-version: 16.15
- run: npm ci
- name: Run ESLint
shell: pwsh
Expand Down
46,311 changes: 34,019 additions & 12,292 deletions package-lock.json

Large diffs are not rendered by default.

13 changes: 11 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"module": "index.js",
"scripts": {
"server": "http-server . -p 8002 -o playground/index.html",
"postinstall": "gulp build-debug-infos && gulp build-declarations && npm run fetch-ral && npm run install:typedoc-plugin",
"postinstall": "gulp build-debug-infos && gulp build-declarations && npm run bundle-adapter && npm run fetch-ral && npm run install:typedoc-plugin",
"build": "gulp build",
"build:dev": "gulp build-h5-source",
"build:min": "gulp build-h5-minified",
Expand All @@ -20,6 +20,7 @@
"checkout-engine-version": "node ./scripts/checkout-engine-version.js",
"varify-engine-version": "node ./scripts/varify-engine-version.js",
"fetch-ral": "node ./scripts/fetch-ral.js",
"bundle-adapter": "node ./scripts/bundle-adapter.js",
"clear": "node ./scripts/clear.js",
"clear-platform": "node ./native/utils/clear-platform.js",
"update:native-external": "node ./native/utils/update-external.js",
Expand Down Expand Up @@ -59,6 +60,7 @@
"jest": "^26.6.3",
"jest-extended": "^1.1.0",
"jest-matcher-deep-close-to": "^2.0.1",
"jsdom": "^19.0.0",
"path": "^0.12.7",
"pngjs": "^6.0.0",
"spectorjs": "^0.9.15",
Expand All @@ -74,10 +76,17 @@
"yargs": "^12.0.5"
},
"dependencies": {
"@babel/plugin-proposal-export-default-from": "^7.17.12",
"@cocos/box2d": "1.0.1",
"@cocos/bullet": "1.0.2",
"@cocos/cannon": "1.2.8",
"@cocos/dragonbones-js": "^1.0.1",
"@cocos/physx": "1.0.7"
"@cocos/physx": "1.0.7",
"babelify": "^10.0.0",
"browserify": "^17.0.0",
"gulp-rename": "^2.0.0",
"gulp-uglify": "^3.0.2",
"vinyl-buffer": "^1.0.1",
"vinyl-source-stream": "^2.0.0"
}
}
4 changes: 2 additions & 2 deletions platforms/minigame/common/engine/AssetManager.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const cacheManager = require('../cache-manager');
const cacheManager = require('./cache-manager');
const { fs, downloadFile, readText, readArrayBuffer, readJson, loadSubpackage, getUserDataPath, exists } = window.fsUtils;

const REGEX = /^https?:\/\/.*/;
Expand Down Expand Up @@ -235,7 +235,7 @@ function downloadBundle (nameOrUrl, options, onComplete) {
js = `assets/${bundleName}/index.${suffix}js`;
}
}
require('../../../' + js);
require('./' + js);
options.__cacheBundleRoot__ = bundleName;
var config = `${url}/config.${suffix}json`;
downloadJson(config, options, function (err, data) {
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions platforms/minigame/common/xmldom/dom-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,6 @@ var XMLReader = require('./sax').XMLReader;
var DOMImplementation = exports.DOMImplementation = require('./dom').DOMImplementation;
exports.XMLSerializer = require('./dom').XMLSerializer ;
exports.DOMParser = DOMParser;

window.DOMParser = DOMParser;
//}
5 changes: 4 additions & 1 deletion platforms/minigame/platforms/alipay/wrapper/builtin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2537,4 +2537,7 @@ function blur() {}
/***/ })

/******/ });
//# sourceMappingURL=my-adapter.js.map
//# sourceMappingURL=my-adapter.js.map

require('../../../common/xmldom/dom-parser');
require('./unify');
2 changes: 2 additions & 0 deletions platforms/minigame/platforms/alipay/wrapper/engine/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require('../fs-utils');
require('../../../../common/engine/index');
require('./Label');
require('./Console');
require('./AudioPlayer');
1 change: 1 addition & 0 deletions platforms/minigame/platforms/alipay/wrapper/unify.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const utils = require('./utils');

window.__globalAdapter = window.__globalAdapter || {};
if (window.__globalAdapter) {
let globalAdapter = window.__globalAdapter;
// SystemInfo
Expand Down
3 changes: 3 additions & 0 deletions platforms/minigame/platforms/baidu/wrapper/builtin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,6 @@ if (!GameGlobal.__isAdapterInjected) {
GameGlobal.__isAdapterInjected = true
inject()
}

require('../../../../common/xmldom/dom-parser');
require('../unify');
2 changes: 2 additions & 0 deletions platforms/minigame/platforms/baidu/wrapper/engine/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
require('../fs-utils');
require('../../../../common/engine/index');
require('./sprite-frame');
1 change: 1 addition & 0 deletions platforms/minigame/platforms/baidu/wrapper/unify.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const utils = require('./utils');

window.__globalAdapter = window.__globalAdapter || {};
if (window.__globalAdapter) {
let globalAdapter = window.__globalAdapter;
let isLandscape = false; // getSystemInfoSync not supported in sub context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,6 @@ if (!GameGlobal.__isAdapterInjected) {
GameGlobal.__isAdapterInjected = true
inject()
}

require('../../../../common/xmldom/dom-parser');
require('../unify');
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
require('../fs-utils');
require('../../../../common/engine/index');
require('./Label');
require('./ImageAsset');
1 change: 1 addition & 0 deletions platforms/minigame/platforms/bytedance/wrapper/unify.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const utils = require('./utils');

window.__globalAdapter = window.__globalAdapter || {};
if (window.__globalAdapter) {
let globalAdapter = window.__globalAdapter;
// SystemInfo
Expand Down
5 changes: 4 additions & 1 deletion platforms/minigame/platforms/wechat/wrapper/builtin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,7 @@ if (!GameGlobal.__isAdapterInjected) {
}

// overwrite global WebAssembly
global.WebAssembly = global.WXWebAssembly
global.WebAssembly = global.WXWebAssembly

require('../../../../common/xmldom/dom-parser');
require('../unify');
2 changes: 2 additions & 0 deletions platforms/minigame/platforms/wechat/wrapper/engine/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
require('../fs-utils');
require('../../../../common/engine/index');
require('./VideoPlayer');
require('./sprite-frame');
1 change: 1 addition & 0 deletions platforms/minigame/platforms/wechat/wrapper/unify.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const utils = require('./utils');

window.__globalAdapter = window.__globalAdapter || {};
if (window.__globalAdapter) {
let globalAdapter = window.__globalAdapter;
// SystemInfo
Expand Down
5 changes: 4 additions & 1 deletion platforms/minigame/platforms/xiaomi/wrapper/builtin.js
Original file line number Diff line number Diff line change
Expand Up @@ -1162,4 +1162,7 @@
exports.default = location;

/***/ })
/******/ ]);
/******/ ]);

require('../../../common/xmldom/dom-parser');
require('./unify');
2 changes: 2 additions & 0 deletions platforms/minigame/platforms/xiaomi/wrapper/engine/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
require('../fs-utils');
require('../../../../common/engine/index');
require('./download-ttf');
require('./Editbox');
1 change: 1 addition & 0 deletions platforms/minigame/platforms/xiaomi/wrapper/unify.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const utils = require('./utils');

window.__globalAdapter = window.__globalAdapter || {};
if (window.__globalAdapter) {
let globalAdapter = window.__globalAdapter;
// SystemInfo
Expand Down
152 changes: 152 additions & 0 deletions scripts/bundle-adapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
const ps = require('path');
const fs = require('fs');
const gulp = require('gulp');
const babelify = require("babelify");
const browserify = require('browserify');
const source = require("vinyl-source-stream");
const buffer = require('vinyl-buffer');
const chalk = require('chalk').default;
const uglify = require('gulp-uglify');
const rename = require('gulp-rename');

const engineRoot = ps.join(__dirname, '..');

(async function bundleAdapter () {
try {
console.time('Bundle Adapter');
await bundleJsbAdapter();
await bundleMinigameAdapter();
await bundleRuntimeAdapter();
console.timeEnd('Bundle Adapter');
process.exit(0);
} catch (e) {
console.error(e);
process.exit(1);
}
})();

async function bundleJsbAdapter () {
console.log(chalk.green('\nBundling jsb adapter'));
// bundle jsb-engine.js
const jsbEngineEntry = normalizePath(ps.join(engineRoot, 'platforms/native/engine/index.js'));
const jsbEngineOutput = normalizePath(ps.join(engineRoot, 'bin/adapter/native/jsb-engine.js'));
await bundle(jsbEngineEntry, jsbEngineOutput);

// bundle jsb-engine.js
const jsbBuiltinEntry = normalizePath(ps.join(engineRoot, 'platforms/native/builtin/index.js'));
const jsbBuiltinOutput = normalizePath(ps.join(engineRoot, 'bin/adapter/native/jsb-builtin.js'));
await bundle(jsbBuiltinEntry, jsbBuiltinOutput);
}

async function bundleMinigameAdapter () {
const platformsPath = ps.join(engineRoot, 'platforms/minigame/platforms');
const platforms = fs.readdirSync(platformsPath);
console.log(chalk.green(`\nBundling minigame platform adapters, including: ${platforms}`));

for (let platform of platforms) {
console.log(`handle platform: ${chalk.green(platform)}`);
// bundle engine-adapter.js
const engineEntry = normalizePath(ps.join(engineRoot, `platforms/minigame/platforms/${platform}/wrapper/engine/index.js`));
const engineOutput = normalizePath(ps.join(engineRoot, `bin/adapter/minigame/${platform}/engine-adapter.js`));
await bundle(engineEntry, engineOutput);

// bundle builtin.js
let builtinEntry = normalizePath(ps.join(engineRoot, `platforms/minigame/platforms/${platform}/wrapper/builtin/index.js`));
if (platform === 'alipay' || platform === 'xiaomi') {
builtinEntry = normalizePath(ps.join(engineRoot, `platforms/minigame/platforms/${platform}/wrapper/builtin.js`));
}
const builtinOutput = normalizePath(ps.join(engineRoot, `bin/adapter/minigame/${platform}/builtin.js`));
await bundle(builtinEntry, builtinOutput);
}
}

async function bundleRuntimeAdapter () {
const platformsPath = ps.join(engineRoot, 'platforms/runtime/platforms');
const platforms = fs.readdirSync(platformsPath);
console.log(chalk.green(`\nBundling runtime platform adapters, including: ${platforms}`));
for (let platform of platforms) {
console.log(`handle platform: ${chalk.green(platform)}`);
// bundle engine-adapter.js
const engineEntry = normalizePath(ps.join(engineRoot, `platforms/runtime/platforms/${platform}/engine/index.js`));
const engineOutput = normalizePath(ps.join(engineRoot, `bin/adapter/runtime/${platform}/engine-adapter.js`));
await bundle(engineEntry, engineOutput);
}
}


function normalizePath (path) {
return path.replace(/\\/g, '/');
}

/**
* Traverse and compare the modification time of source code and target file
* @param {string} dir
* @param {string} targetFileMtime
*/
function checkFileStat (dir, targetFileMtime) {
let files = fs.readdirSync(dir);
return files.some (file => {
let filePath = ps.join(dir, file);
let stat = fs.statSync(filePath);
if (stat.isDirectory()) {
return checkFileStat(filePath, targetFileMtime);
}
else if (stat.mtime.getTime() > targetFileMtime) {
return true;
}
});
}

/**
* Check whether the source code is updated
* @param {string} src
* @param {string} dst
*/
function hasChanged (src, dst) {
if (!fs.existsSync(dst)) {
return true;
}
let stat = fs.statSync(dst);
let dir = ps.dirname(src);
return checkFileStat(dir, stat.mtime.getTime());
}

/**
* Create bundle task
* @param {string} src
* @param {string} dst
* @param {boolean} uglify
*/
function createBundleTask (src, dst) {
let targetFileName = ps.basename(dst);
let targetFileNameMin = ps.basename(targetFileName, '.js') + '.min.js';
dst = ps.dirname(dst);
let bundler = browserify(src);
return bundler.transform(babelify, {presets: [require('@babel/preset-env')],
plugins: [
require('@babel/plugin-proposal-class-properties'),
require('@babel/plugin-proposal-export-default-from')
]})
.bundle()
.pipe(source(targetFileName))
.pipe(buffer())
.pipe(gulp.dest(dst))
.pipe(rename(targetFileNameMin)).pipe(uglify()).pipe(gulp.dest(dst));
}

/**
* Build adapters
* @param {string} entry
* @param {string} output
*/
async function bundle (entry, output) {
await new Promise((resolve) => {
console.log(`Generate bundle: ${chalk.green(ps.basename(output))}`);
// if (!hasChanged(entry, output)) {
// console.log(chalk.yellow('Use bundle cache, skip bundling'));
// resolve();
// return;
// }
createBundleTask(entry, output).on('end', resolve);
});
}
Loading

0 comments on commit 549108d

Please sign in to comment.