Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support moduleMainPaths #491

Merged
merged 8 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# ChangeLog

## 3.18.0
* @akashic/[email protected] に追従
* `moduleMainPaths` をサポート

## 3.17.1
* `g.Game#asset: AssetAccessor` を追加
* アセットIDからアクセッサパスを逆引きするメソッド `AssetAccessor#pathOf()` を追加
Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "@akashic/akashic-engine",
"version": "3.17.1",
"version": "3.18.0",
"description": "The core library of Akashic Engine",
"main": "index.js",
"dependencies": {
"@akashic/game-configuration": "~2.1.0",
"@akashic/game-configuration": "~2.2.0",
"@akashic/pdi-types": "^1.13.0",
"@akashic/playlog": "~3.3.0",
"@akashic/trigger": "~2.1.0"
Expand Down
13 changes: 11 additions & 2 deletions src/AssetManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import type {
AudioAssetConfigurationBase,
VideoAssetConfigurationBase,
VectorImageAssetConfigurationBase,
BinaryAssetConfigurationBase
BinaryAssetConfigurationBase,
ModuleMainPathsMap
} from "@akashic/game-configuration";
import type {
Asset,
Expand Down Expand Up @@ -215,6 +216,12 @@ export class AssetManager implements AssetLoadHandler {
*/
_moduleMainScripts: ModuleMainScriptsMap;

/**
* package.json のパスをキーに、その main フィールドの内容を値に持つテーブル
* @private
*/
_moduleMainPaths: ModuleMainPathsMap;

/**
* 各アセットに対する参照の数。
* 参照は requestAssets() で増え、unrefAssets() で減る。
Expand Down Expand Up @@ -266,7 +273,8 @@ export class AssetManager implements AssetLoadHandler {
gameParams: AssetManagerParameterGameLike,
conf?: AssetConfigurationMap,
audioSystemConfMap?: AudioSystemConfigurationMap,
moduleMainScripts?: ModuleMainScriptsMap
moduleMainScripts?: ModuleMainScriptsMap,
moduleMainPaths?: ModuleMainPathsMap
) {
this._resourceFactory = gameParams.resourceFactory;
this._audioSystemManager = gameParams.audio;
Expand All @@ -278,6 +286,7 @@ export class AssetManager implements AssetLoadHandler {
this._liveAssetVirtualPathTable = {};
this._liveAssetPathTable = {};
this._moduleMainScripts = moduleMainScripts ? moduleMainScripts : {};
this._moduleMainPaths = moduleMainPaths ?? {};
this._refCounts = {};
this._loadings = {};
this._generatedAssetCount = 0;
Expand Down
8 changes: 7 additions & 1 deletion src/Game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,13 @@ export class Game {
if (Array.isArray(gameConfiguration.assets)) {
throw new Error("Game#constructor: array type of configuration.assets is not yet supported");
}
this._assetManager = new AssetManager(this, gameConfiguration.assets, gameConfiguration.audio, gameConfiguration.moduleMainScripts);
this._assetManager = new AssetManager(
this,
gameConfiguration.assets,
gameConfiguration.audio,
gameConfiguration.moduleMainScripts,
gameConfiguration.moduleMainPaths
);
this._moduleManager = undefined!;
this.asset = new AssetAccessor(this._assetManager);

Expand Down
12 changes: 10 additions & 2 deletions src/ModuleManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export class ModuleManager {
let resolvedPath: string | undefined;
const liveAssetVirtualPathTable = this._assetManager._liveAssetVirtualPathTable;
const moduleMainScripts = this._assetManager._moduleMainScripts;
const moduleMainPaths = this._assetManager._moduleMainPaths;

// 0. アセットIDらしい場合はまず当該アセットを探す
if (path.indexOf("/") === -1) {
Expand All @@ -95,7 +96,8 @@ export class ModuleManager {
}

// akashic-engine独自仕様: 対象の `path` が `moduleMainScripts` に指定されていたらそちらを参照する
if (moduleMainScripts[path]) {
// moduleMainScripts は将来的に非推奨となるため、moduleMainPaths が存在しない場合だけ参照する
if (Object.keys(moduleMainPaths).length === 0 && moduleMainScripts[path]) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

存在チェックのため Object.keys で判定しています。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_moduleMainPaths: ModuleMainPathsMap | null にすればなくせませんか? 頻発する処理でもないですが、処理としてですし記述もコンパクトになりそうです。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

型に null を追加し修正しました。

targetScriptAsset = liveAssetVirtualPathTable[resolvedPath];
} else {
targetScriptAsset = this._findAssetByPathAsFile(resolvedPath, liveAssetVirtualPathTable);
Expand Down Expand Up @@ -145,6 +147,7 @@ export class ModuleManager {
let resolvedPath: string | null = null;
const liveAssetVirtualPathTable = this._assetManager._liveAssetVirtualPathTable;
const moduleMainScripts = this._assetManager._moduleMainScripts;
const moduleMainPaths = this._assetManager._moduleMainPaths;

// require(X) from module at path Y
// 1. If X is a core module,
Expand Down Expand Up @@ -180,7 +183,8 @@ export class ModuleManager {
// 3. LOAD_NODE_MODULES(X, dirname(Y))

// akashic-engine独自仕様: 対象の `path` が `moduleMainScripts` に指定されていたらそちらを返す
if (moduleMainScripts[path]) {
// moduleMainScripts は将来的に非推奨となるため、moduleMainPaths が存在しない場合だけ参照する
if (Object.keys(moduleMainPaths).length === 0 && moduleMainScripts[path]) {
return moduleMainScripts[path];
}

Expand Down Expand Up @@ -250,6 +254,10 @@ export class ModuleManager {
_resolveAbsolutePathAsDirectory(resolvedPath: string, liveAssetPathTable: { [key: string]: OneOfAsset }): string | null {
let path = resolvedPath + "/package.json";
const asset = liveAssetPathTable[path];
const moduleMainPaths = this._assetManager._moduleMainPaths;
if (moduleMainPaths[path]) {
return moduleMainPaths[path];
}
// liveAssetPathTable[path] != null だけではpathと同名のprototypeプロパティがある場合trueになってしまうので hasOwnProperty() を利用
if (liveAssetPathTable.hasOwnProperty(path) && asset.type === "text") {
const pkg = JSON.parse(asset.data);
Expand Down
105 changes: 105 additions & 0 deletions src/__tests__/ModuleSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,111 @@ describe("test Module", () => {
game._startLoadingGlobalAssets();
});

it("require - use moduleMainPaths", done => {
const moduleMainScripts = gameConfiguration.moduleMainScripts;
delete gameConfiguration.moduleMainScripts;
gameConfiguration.moduleMainPaths = {
"node_modules/noPackageJsonModule/package.json": "node_modules/noPackageJsonModule/hoge.js",
"node_modules/externalResolvedModule/package.json": "node_modules/externalResolvedModule/index.js"
};

const game = new Game(gameConfiguration, "./");
const manager = game._moduleManager;
const path = "/script/dummypath.js";
game.resourceFactory.scriptContents = scriptContents;
game._onLoad.add(() => {
const module = new Module({
id: "dummymod",
path,
virtualPath: game._assetManager._liveAssetPathTable[path],
runtimeValueBase: game._runtimeValueBase,
requireFunc: (path: string, currentModule?: Module) => manager._require(path, currentModule),
resolveFunc: (path: string, currentModule?: Module) => manager._resolvePath(path, currentModule)
});

let mod = module.require("./foo");
expect(mod.me).toBe("script-foo");
expect(mod.thisModule instanceof Module).toBe(true);
expect(mod.thisModule.filename).toBe("/script/foo.js");
expect(mod.thisModule.parent).toBe(module);
expect(mod.thisModule.children).toEqual([]);
expect(mod.thisModule.loaded).toBe(true);

mod = module.require("noPackageJson");
expect(mod.me).toBe("noPackageJson-index");
expect(mod.thisModule instanceof Module).toBe(true);
expect(mod.thisModule.filename).toBe("/node_modules/noPackageJson/index.js");
expect(mod.thisModule.parent).toBe(module);
expect(mod.thisModule.children).toEqual([]);
expect(mod.thisModule.loaded).toBe(true);

mod = module.require("noDefaultIndex");
expect(mod.me).toBe("noDefaultIndex-root");
expect(mod.thisModule instanceof Module).toBe(true);
expect(mod.thisModule.filename).toBe("/node_modules/noDefaultIndex/root.js");
expect(mod.thisModule.parent).toBe(module);
expect(mod.thisModule.children).toEqual([]);
expect(mod.thisModule.loaded).toBe(true);

mod = module.require("wrongPackageJsonMain");
expect(mod.me).toBe("wrongPackageJsonMain-index");
expect(mod.thisModule instanceof Module).toBe(true);
expect(mod.thisModule.filename).toBe("/node_modules/wrongPackageJsonMain/index.js");
expect(mod.thisModule.parent).toBe(module);
expect(mod.thisModule.children).toEqual([]);
expect(mod.thisModule.loaded).toBe(true);

mod = module.require("aGlobalAssetFoo");
expect(mod.me).toBe("script-foo");
expect(mod.thisModule instanceof Module).toBe(true);
expect(mod.thisModule.filename).toBe("/script/foo.js");
expect(mod.thisModule.parent).toBe(module);
expect(mod.thisModule.children).toEqual([]);
expect(mod.thisModule.loaded).toBe(true);

mod = module.require("noPackageJsonModule");
expect(mod.me).toBe("noPackageJsonModule");
expect(mod.thisModule instanceof Module).toBe(true);
expect(mod.thisModule.filename).toBe("/node_modules/noPackageJsonModule/real_hoge.js");
expect(mod.thisModule.parent).toBe(module);
expect(mod.thisModule.children).toEqual([]);
expect(mod.thisModule.loaded).toBe(true);

expect(() => {
module.require("aNonGlobalAssetBar");
}).toThrowError("AssertionError");
const scene = new Scene({
game: game,
assetIds: ["aNonGlobalAssetBar"]
});
scene.onLoad.add(() => {
let mod = module.require("aNonGlobalAssetBar");
expect(mod.me).toBe("script-bar");
expect(mod.thisModule instanceof Module).toBe(true);
expect(mod.thisModule.filename).toBe("/script/bar.js");
expect(mod.thisModule.parent).toBe(module);
expect(mod.thisModule.children).toEqual([]);
expect(mod.thisModule.loaded).toBe(true);

mod = module.require("./bar");
expect(mod.me).toBe("script-bar");

game.popScene();
game._flushPostTickTasks();
expect(() => {
module.require("aNonGlobalAssetBar");
}).toThrowError("AssertionError");

gameConfiguration.moduleMainScripts = moduleMainScripts;
delete gameConfiguration.moduleMainPaths;
done();
});
game.pushScene(scene);
game._flushPostTickTasks();
});
game._startLoadingGlobalAssets();
});

it("_resolvePath", done => {
const game = new Game(gameConfiguration, "/");
const manager = game._moduleManager;
Expand Down