複数のプレーンJavaScript出力ファイルを構築するために、共通の依存関係を持つTypeScriptプロジェクトを構成します

アンドリューマオ

私は現在ボットランド用のスクリプトいくつか書いています。ボットランドは、マウスとキーボードでユニットを制御する代わりに、APIを介してボットを制御するコードを記述し、ボットが他のボットと戦うリアルタイム戦略ゲームです。SC2のユニットに精通している場合は、まばたきストーカー、攻城戦車、医療、ウルトラリスクに似たボットを作成できます。(ソフトウェアエンジニアにとっては非常に楽しいゲームですが、それはこの質問の範囲外です。)

ボットランド

Bot control has three levels of increasing complexity: a default AI, a Scratch-like programming language, and a reduced set of JavaScript called BotLandScript. Although the built-in editor for BotLandScript is reasonable, you have to upload all your code as one single file with global top-level functions everywhere. Naturally, this starts getting painful after a while if your code starts to get long and different bots share the same functions.

プログラミング環境

To facilitate writing code for multiple bots, reduce the chance for unintentional errors when coding in bare JS, and increase my chances of beating other players, I set up the above TypeScript project to provide a common library as well as code for each of my bots. The current directory structure looks like approximately like the following:

lib/ 
  bot.land.d.ts
  common.ts
BlinkStalker/
  BlinkStalker.ts
  tsconfig.json
Artillery/
  Artillery.ts
  tsconfig.json
SmartMelee/
  SmartMelee.ts
  tsconfig.json

lib is the common code that is shared among bots, and provides TypeScript definitions for the (non-TS) Bot Land API. Each bot then gets its own folder, with one file containing the bot code and the other a boilerplate tsconfig.json:

{
  "compilerOptions": {
    "target": "es3",
    "module": "none",
    "sourceMap": false,
    "outFile": "bot.js"
  },
  "files": [
    "MissileKite.ts"
  ],
  "include": [
    "../lib/**/*"
  ]
}

When each tsconfig.json is built, it creates a corresponding bot.js that contains transpiled code from the bot itself as well as all the code in common.js. This setup is suboptimal for a few reasons, among others: it requires a lot of duplicate boilerplate, makes it hard to add new bots, includes a lot of unnecessary code for each bot, and requires each bot to be built separately.

However, based on my research so far, it doesn't seem like there's an easy way to do what I want. In particular, using the new tsc -b option and references does not work, because that requires the code to be modularized and Bot Land requires a single file with all functions defined at the top level.

What's the best way to achieve as many of the following as possible?

  • No new boilerplate required to add a new bot (e.g. no tsconfig.json per bot)
  • Use import for common functions to avoid outputting unused code, but then...
  • Still output all functions as one single file in Bot Land's specific format
  • A single build step that produces multiple output files, one for each bot
  • Bonus: integrating the build process with VS Code. There is a currently correspondingly boilerplate tasks.json for building each sub-project.

I vaguely surmise the answer probably involves something like Grunt in addition to tsc, but I don't know enough about that to be sure.

PopGoesTheWza

Here is my attempt to answer your requirements.

Notable files:

  • src/tsconfig-botland.json holds settings for any bot.land script (including your custom declarations which I moved to types/bot-land/index.d.ts). You may which to change the strict settings I used.
  • src/tsconfig.json holds references to all your bots. This is the file to edit whenever you want to add another bot script

A bot script is at least two files: a minimalist tsconfig.json and one or more .ts script files.

For example src/AggroMiner/tsconfig.json:

{
    "extends": "../tsconfig-botland",
    "compilerOptions": {
        "outFile": "../../build/AggroMiner.js"
    },
    "files": ["index.ts"],
    "include": ["**/*.ts", "../lib/**/*.ts"]
}

In most cases, to start a new bot script you should:

  1. copy any bot folder (i.e. src/AggroMiner) to a new folder under src
  2. edit the src/<newBotFolder>/tsconfig.json to edit the outFile with the name of your bot
  3. edit src/tsconfig.json and add a reference to src/<newBotFolder>

The following npm/yarn script have been set:

  • build to build all bots
  • build-clean which clear the build folder before running a build
  • format to run Prettier on all .ts files under src
  • lint to run a tslint check on all bot scripts

Now running down your requirements:

  • No new boilerplate required to add a new bot (e.g. no tsconfig.json per bot)

To achieve this would require creating some script which would enumerate your bots folder/scripts... and setup the relevant per bot tsconfig.json and run tsc. Unless it is strictly necessary, a minimal setup (describe above) might be sufficient.

  • Use import for common functions to avoid outputting unused code, but then...

まず、モジュールexport/importステートメントの使用を開始する場合、単一のファイル出力を実現するには、パック/ツリーシェイクするために追加のサードパーティが必要になることに注意してくださいBot.landから収集できたものから、スクリプトはサーバー上で実行されています。デッドコードがボットのパフォーマンスに影響を与えない限り、私はあまり気にしません。

  • それでも、ボットランドの特定の形式ですべての機能を単一のファイルとして出力します

完了。

  • ボットごとに1つずつ、複数の出力ファイルを生成する単一のビルドステップ

完了。

  • ボーナス:ビルドプロセスをVSCodeと統合します。現在、各サブプロジェクトを構築するための定型的なtasks.jsonがあります。

npmスクリプトは、このようにすること(少なくとも、彼らは鉱山で行う)VSCのタスクリストに表示されますtasks.json不要に。

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ