Browse Source

Split app and tests JS files, and simplified dependencies bundle

Michaël Lemaire 6 months ago
parent
commit
436e568a0e

+ 4 - 2
.gitignore

@@ -1,10 +1,12 @@
 .venv
 coverage
 /node_modules
-/out/vendor
 /out/assets
-/out/build.*
+/out/app.*
+/out/tests.*
+/out/dependencies.js
 /graphics/**/*.blend?*
 /graphics/**/output.png
 /typings/
 *.log
+*.tsbuildinfo

+ 2 - 2
.vscode/settings.json

@@ -10,7 +10,7 @@
         "**/CVS": true,
         "**/.DS_Store": true,
         "node_modules": true,
-        ".env": true,
-        "out": true
+        ".venv": true,
+        "**/*.tsbuildinfo": true,
     }
 }

+ 13 - 0
config/app.json

@@ -0,0 +1,13 @@
+{
+    "extends": "./common.json",
+    "compilerOptions": {
+        "outFile": "../out/app.js",
+        "rootDir": ".."
+    },
+    "include": [
+        "../src/**/*.ts"
+    ],
+    "exclude": [
+        "../src/**/*.spec.ts"
+    ]
+}

+ 21 - 0
config/common.json

@@ -0,0 +1,21 @@
+{
+    "compilerOptions": {
+        "module": "system",
+        "composite": true,
+        "declaration": true,
+        "declarationMap": true,
+        "forceConsistentCasingInFileNames": true,
+        "noFallthroughCasesInSwitch": true,
+        "noImplicitReturns": true,
+        "removeComments": false,
+        "preserveConstEnums": true,
+        "strict": true,
+        "sourceMap": true,
+        "lib": [
+            "dom",
+            "es6",
+            "scripthost"
+        ],
+        "target": "es6"
+    }
+}

+ 15 - 0
config/tests.json

@@ -0,0 +1,15 @@
+{
+    "extends": "./common.json",
+    "compilerOptions": {
+        "outFile": "../out/tests.js",
+        "rootDir": ".."
+    },
+    "include": [
+        "../src/**/*.spec.ts"
+    ],
+    "references": [
+        {
+            "path": "./app.json"
+        }
+    ]
+}

+ 1 - 1
out/aiworker.js

@@ -8,7 +8,7 @@ var Phaser = new Proxy(function () { }, handler);
 //var debug = console.log;
 var debug = function () { };
 
-importScripts("build.js");
+importScripts("app.js");
 
 onmessage = function (e) {
     debug("[AI Worker] Received", e.data.length);

+ 2 - 3
out/index.html

@@ -41,9 +41,8 @@
     <div id="-space-tac" class="game"></div>
     <div class=".fontLoader">.</div>
 
-    <script src="vendor/parse/parse.min.js"></script>
-    <script src="vendor/phaser/phaser.js"></script>
-    <script src="build.js"></script>
+    <script src="dependencies.js"></script>
+    <script src="app.js"></script>
 
     <script>
         window.onload = function () {

+ 1 - 1
out/sim.js

@@ -2,7 +2,7 @@ const Pool = require('process-pool').default;
 
 const pool = new Pool({ processLimit: 8 });
 const work = pool.prepare(function () {
-    const App = require("./build").TK.SpaceTac;
+    const App = require("./app").TK.SpaceTac;
 
     async function doOneBattle(i) {
         let ai1 = new App.TacticalAI();

+ 8 - 7
out/tests.html

@@ -5,7 +5,7 @@
     <meta charset="utf-8" />
     <title>SpaceTac - Unit tests</title>
 
-    <link rel="stylesheet" href="vendor/jasmine/jasmine.css">
+    <link rel="stylesheet" href="/jasmine/jasmine.css">
     <style>
         canvas {
             display: none;
@@ -22,12 +22,13 @@
         <div id="-space-tac" class="game"></div>
     </div>
 
-    <script src="vendor/jasmine/jasmine.js"></script>
-    <script src="vendor/jasmine/jasmine-html.js"></script>
-    <script src="vendor/jasmine/boot.js"></script>
-    <script src="vendor/parse/parse.min.js"></script>
-    <script src="vendor/phaser/phaser.min.js"></script>
-    <script src="build.js"></script>
+    <script src="/jasmine/jasmine.js"></script>
+    <script src="/jasmine/jasmine-html.js"></script>
+    <script src="/jasmine/boot.js"></script>
+
+    <script src="dependencies.js"></script>
+    <script src="app.js"></script>
+    <script src="tests.js"></script>
 </body>
 
 </html>

+ 13 - 4
package-lock.json

@@ -2499,7 +2499,8 @@
     "jasmine-core": {
       "version": "3.4.0",
       "resolved": "https://registry.npmjs.org/jasmine-core/-/jasmine-core-3.4.0.tgz",
-      "integrity": "sha512-HU/YxV4i6GcmiH4duATwAbJQMlE0MsDIR5XmSVxURxKHn3aGAdbY1/ZJFmVRbKtnLwIxxMJD7gYaPsypcbYimg=="
+      "integrity": "sha512-HU/YxV4i6GcmiH4duATwAbJQMlE0MsDIR5XmSVxURxKHn3aGAdbY1/ZJFmVRbKtnLwIxxMJD7gYaPsypcbYimg==",
+      "dev": true
     },
     "js-yaml": {
       "version": "3.13.1",
@@ -3418,6 +3419,13 @@
         "jsdoc": "^3.6.1",
         "path": "^0.12.7",
         "typescript": "^3.4.5"
+      },
+      "dependencies": {
+        "typescript": {
+          "version": "3.4.5",
+          "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.4.5.tgz",
+          "integrity": "sha512-YycBxUb49UUhdNMU5aJ7z5Ej2XGmaIBL0x34vZ82fn3hGvD+bgrMrVDpatgz2f7YxUMJxMkbWxJZeAvDxVe7Vw=="
+        }
       }
     },
     "pify": {
@@ -4473,9 +4481,10 @@
       }
     },
     "typescript": {
-      "version": "3.4.5",
-      "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.4.5.tgz",
-      "integrity": "sha512-YycBxUb49UUhdNMU5aJ7z5Ej2XGmaIBL0x34vZ82fn3hGvD+bgrMrVDpatgz2f7YxUMJxMkbWxJZeAvDxVe7Vw=="
+      "version": "3.5.0-dev.20190516",
+      "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.5.0-dev.20190516.tgz",
+      "integrity": "sha512-J+1tISjR4yCmZ3F2wWZDEMoocF4wdXsHf68ln6cb0gjm8VmSDUYSPOVbCJUJoLqjWaCEPs05O7eNrbTulo/4ew==",
+      "dev": true
     },
     "uc.micro": {
       "version": "1.0.6",

+ 7 - 3
package.json

@@ -21,6 +21,7 @@
     "glob": "^7.1.4",
     "glob-watcher": "^5.0.3",
     "jasmine": "^3.4.0",
+    "jasmine-core": "^3.4.0",
     "karma": "^4.1.0",
     "karma-chrome-launcher": "^2.2.0",
     "karma-coverage": "^1.1.2",
@@ -31,12 +32,15 @@
     "remap-istanbul": "^0.13.0",
     "runjs": "^4.4.2",
     "shelljs": "^0.8.3",
-    "typescript": "^3.4.5",
+    "typescript": "^3.5.0-dev.20190516",
     "uglify-js": "^3.5.12"
   },
   "dependencies": {
-    "jasmine-core": "^3.4.0",
     "parse": "^2.4.0",
     "phaser": "^3.17.0"
+  },
+  "dependenciesMap": {
+    "parse": "dist/parse.min.js",
+    "phaser": "dist/phaser.min.js"
   }
-}
+}

+ 26 - 24
runfile.js

@@ -35,16 +35,16 @@ async function exec(command) {
 /**
  * Build app from typescript sources
  */
-async function ts(dist = false) {
+async function ts() {
     console.log("Building app...");
-    await exec(`tsc --project ${dist ? "./tsconfig.dist.json" : "."}`);
+    await exec(`tsc --build config/app.json config/tests.json`);
 }
 
 /**
  * Start watching for typescript changes
  */
 async function watch_ts() {
-    await exec(`tsc --project . --watch --preserveWatchOutput`);
+    await exec(`tsc --build config/app.json config/tests.json --watch --preserveWatchOutput`);
 }
 
 /**
@@ -135,33 +135,32 @@ async function watch_data() {
 }
 
 /**
- * Copy the vendors from node_modules to dist directory
+ * Copy and concatenate the vendors from node_modules to out/dependencies.js
  */
-async function vendors() {
-    console.log("Copying vendors...");
-    shell.rm('-rf', 'out/vendor');
-    shell.mkdir('-p', 'out/vendor');
-    shell.cp('-R', 'node_modules/phaser/dist', 'out/vendor/phaser');
-    shell.cp('-R', 'node_modules/parse/dist', 'out/vendor/parse');
-    shell.cp('-R', 'node_modules/jasmine-core/lib/jasmine-core', 'out/vendor/jasmine');
+async function dependencies() {
+    console.log("Bundling dependencies...");
+    const self = JSON.parse(fs.readFileSync("package.json"));
+    const deps = Object.keys(self["dependenciesMap"]).map(dependency => `node_modules/${dependency}/${self["dependenciesMap"][dependency]}`);
+    const bundle = shell.cat(deps);
+    fs.writeFileSync("out/dependencies.js", bundle);
 }
 
 /**
- * Start watching for vendors changes
+ * Start watching for dependencies changes
  */
-async function watch_vendors() {
-    watch(['package.json'], () => vendors());
+async function watch_dependencies() {
+    watch(['package.json'], () => dependencies());
     await forever();
 }
 
 /**
  * Trigger a single build
  */
-async function build(dist = false) {
+async function build() {
     await Promise.all([
-        ts(dist),
+        ts(),
         data(),
-        vendors()
+        dependencies()
     ]);
 }
 
@@ -170,14 +169,14 @@ async function build(dist = false) {
  */
 async function optimize() {
     // TODO do not overwrite dev build
-    await exec("uglifyjs out/build.dist.js --source-map --ecma 6 --mangle --keep-classnames --compress --output out/build.js");
+    await exec("uglifyjs out/app.js --source-map --ecma 6 --mangle --keep-classnames --compress --output out/app.js");
 }
 
 /**
  * Deploy to production
  */
 async function deploy(task, experimental = false) {
-    await build(true);
+    await build();
     await optimize();
     await exec(`rsync -avz --delete ./out/ hosting.thunderk.net:/srv/website/spacetac${experimental ? "x" : ""}/`);
 }
@@ -208,7 +207,7 @@ async function test(task) {
  * Run tests in karma when the build changes
  */
 async function watch_test(task) {
-    watch(["out/*.js", "out/*.html"], () => karma());
+    watch(["out/*.js", "out/*.html", "spec/support/*"], () => karma());
     await forever();
 }
 
@@ -216,7 +215,7 @@ async function watch_test(task) {
  * Run tests in karma, using freshly built app (for continuous integration)
  */
 async function ci(task) {
-    await Promise.all([ts(), vendors()]);
+    await Promise.all([ts(), dependencies()]);
     await karma();
     await exec("remap-istanbul -i out/coverage/coverage.json -o out/coverage -t html");
 }
@@ -230,6 +229,9 @@ async function serve() {
         port: 8012,
         root: 'out',
         ignore: 'out/coverage',
+        mount: [
+            ['/jasmine', './node_modules/jasmine-core/lib/jasmine-core']
+        ],
         wait: 500
     });
     await new Promise(() => null);
@@ -249,7 +251,7 @@ async function continuous() {
         serve(),
         watch_ts(),
         watch_data(),
-        watch_vendors(),
+        watch_dependencies(),
         watch_test(),
     ]);
 }
@@ -281,8 +283,8 @@ module.exports = {
     watch_ts: command(watch_ts),
     data: command(data),
     watch_data: command(watch_data),
-    vendors: command(vendors),
-    watch_vendors: command(watch_vendors),
+    dependencies: command(dependencies),
+    watch_dependencies: command(watch_dependencies),
     build: command(build),
     test: command(test),
     watch_test: command(watch_test),

+ 4 - 4
spec/support/karma.conf.js

@@ -14,7 +14,7 @@ module.exports = function (config) {
     },
 
     preprocessors: {
-      'build.js': ['coverage']
+      'app.js': ['coverage']
     },
 
     coverageReporter: {
@@ -31,9 +31,9 @@ module.exports = function (config) {
     },
 
     files: [
-      'vendor/phaser/phaser.js',
-      'vendor/parse/parse.min.js',
-      'build.js'
+      'dependencies.js',
+      'app.js',
+      'tests.js',
     ]
   })
 }

+ 1 - 1
src/MainUI.spec.ts

@@ -1,4 +1,4 @@
-/// <reference path="ui/TestGame.ts" />
+/// <reference path="../node_modules/phaser/types/phaser.d.ts"/>
 
 module TK.SpaceTac.UI.Specs {
     class FakeStorage {

+ 0 - 2
src/common/DiffLog.spec.ts

@@ -1,5 +1,3 @@
-/// <reference path="DiffLog.ts" />
-
 module TK.Specs {
     class TestState {
         counter = 0

+ 0 - 2
src/common/RObject.spec.ts

@@ -1,5 +1,3 @@
-/// <reference path="RObject.ts" />
-
 module TK.Specs {
     export class TestRObject extends RObject {
         x: number

+ 1 - 1
src/common/Tools.ts

@@ -477,7 +477,7 @@ module TK {
     /**
      * Copy an object (only a shallow copy of immediate properties)
      */
-    export function copy<T>(object: T): T {
+    export function copy<T extends Object>(object: T): T {
         let objectCopy = <T>Object.create(object.constructor.prototype);
         copyfields(object, objectCopy);
         return objectCopy;

+ 0 - 2
src/core/Drone.spec.ts

@@ -1,5 +1,3 @@
-/// <reference path="effects/BaseEffect.ts" />
-
 module TK.SpaceTac {
     testing("Drone", test => {
         test.case("applies area effects when deployed", check => {

+ 0 - 2
src/core/PersonalityReactions.spec.ts

@@ -1,5 +1,3 @@
-/// <reference path="PersonalityReactions.ts" />
-
 module TK.SpaceTac.Specs {
     testing("PersonalityReactions", test => {
         function apply(pool: ReactionPool): PersonalityReaction | null {

+ 0 - 2
src/core/ai/TacticalAI.spec.ts

@@ -1,5 +1,3 @@
-/// <reference path="Maneuver.ts" />
-
 module TK.SpaceTac.Specs {
     testing("TacticalAI", test => {
         class FixedManeuver extends Maneuver {

+ 0 - 2
src/core/diffs/ShipAttributeDiff.spec.ts

@@ -1,5 +1,3 @@
-/// <reference path="../../common/Testing.ts" />
-
 module TK.SpaceTac.Specs {
     testing("ShipAttributeDiff", test => {
         test.case("applies and reverts", check => {

+ 0 - 2
src/core/diffs/ShipDeathDiff.spec.ts

@@ -1,5 +1,3 @@
-/// <reference path="../../common/Testing.ts" />
-
 module TK.SpaceTac.Specs {
     testing("ShipDeathDiff", test => {
         test.case("applies and reverts", check => {

+ 0 - 2
src/core/diffs/ShipValueDiff.spec.ts

@@ -1,5 +1,3 @@
-/// <reference path="../../common/Testing.ts" />
-
 module TK.SpaceTac.Specs {
     testing("ShipValueDiff", test => {
         test.case("applies and reverts", check => {

+ 0 - 2
src/ui/AssetLoading.spec.ts

@@ -1,5 +1,3 @@
-/// <reference path="TestGame.ts" />
-
 module TK.SpaceTac.UI.Specs {
     testing("AssetLoading", test => {
         let testgame = setupSingleView(test, () => [new AssetLoading({}), []]);

+ 0 - 2
src/ui/BaseView.spec.ts

@@ -1,5 +1,3 @@
-/// <reference path="TestGame.ts" />
-
 module TK.SpaceTac.UI.Specs {
     testing("BaseView", test => {
         let testgame = setupEmptyView(test);

+ 0 - 3
src/ui/Router.spec.ts

@@ -1,6 +1,3 @@
-/// <reference path="TestGame.ts" />
-/// <reference path="Router.ts" />
-
 module TK.SpaceTac.UI.Specs {
     testing("Router", test => {
         let testgame = setupSingleView(test, () => [new Router({}), {}]);

+ 0 - 2
src/ui/battle/ActionBar.spec.ts

@@ -1,5 +1,3 @@
-/// <reference path="../TestGame.ts"/>
-
 module TK.SpaceTac.UI.Specs {
     testing("ActionBar", test => {
         let testgame = setupBattleview(test);

+ 0 - 2
src/ui/battle/ActionIcon.spec.ts

@@ -1,5 +1,3 @@
-/// <reference path="../TestGame.ts"/>
-
 module TK.SpaceTac.UI.Specs {
     testing("ActionIcon", test => {
         let testgame = setupBattleview(test);

+ 0 - 2
src/ui/battle/ActionTooltip.spec.ts

@@ -1,5 +1,3 @@
-/// <reference path="../TestGame.ts"/>
-
 module TK.SpaceTac.UI.Specs {
     testing("ActionTooltip", test => {
         let testgame = setupEmptyView(test);

+ 0 - 2
src/ui/battle/ArenaShip.spec.ts

@@ -1,5 +1,3 @@
-/// <reference path="../TestGame.ts"/>
-
 module TK.SpaceTac.UI.Specs {
     testing("ArenaShip", test => {
         let testgame = setupBattleview(test);

+ 0 - 2
src/ui/battle/BattleView.spec.ts

@@ -1,5 +1,3 @@
-/// <reference path="../TestGame.ts"/>
-
 module TK.SpaceTac.UI.Specs {
     testing("BattleView", test => {
         let testgame = setupBattleview(test);

+ 0 - 2
src/ui/battle/ShipList.spec.ts

@@ -1,5 +1,3 @@
-/// <reference path="../TestGame.ts"/>
-
 module TK.SpaceTac.UI.Specs {
     testing("ShipList", test => {
         let testgame = setupEmptyView(test);

+ 0 - 2
src/ui/character/CharacterUpgrade.spec.ts

@@ -1,5 +1,3 @@
-/// <reference path="../TestGame.ts"/>
-
 module TK.SpaceTac.UI.Specs {
     testing("CharacterUpgrade", test => {
         let testgame = setupEmptyView(test);

+ 0 - 2
src/ui/character/FleetCreationView.spec.ts

@@ -1,5 +1,3 @@
-/// <reference path="../TestGame.ts"/>
-
 module TK.SpaceTac.UI.Specs {
     testing("FleetCreationView", test => {
         let testgame = setupSingleView(test, () => [new FleetCreationView({}), []]);

+ 0 - 3
src/ui/menu/InputInviteCode.spec.ts

@@ -1,6 +1,3 @@
-/// <reference path="../TestGame.ts" />
-/// <reference path="MainMenu.ts" />
-
 module TK.SpaceTac.UI.Specs {
     testing("InputInviteCode", test => {
         let testgame = setupEmptyView(test);

+ 0 - 10
tsconfig.dist.json

@@ -1,10 +0,0 @@
-{
-    "extends": "./tsconfig",
-    "compilerOptions": {
-        "outFile": "out/build.dist.js",
-        "removeComments": true
-    },
-    "exclude": [
-        "src/**/*.spec.ts"
-    ]
-}

+ 1 - 17
tsconfig.json

@@ -1,21 +1,5 @@
 {
-    "compilerOptions": {
-        "module": "system",
-        "forceConsistentCasingInFileNames": true,
-        "noFallthroughCasesInSwitch": true,
-        "noImplicitReturns": true,
-        "removeComments": false,
-        "preserveConstEnums": true,
-        "outFile": "out/build.js",
-        "strict": true,
-        "sourceMap": true,
-        "lib": [
-            "dom",
-            "es6",
-            "scripthost"
-        ],
-        "target": "es6"
-    },
+    "extends": "./config/common.json",
     "include": [
         "src/**/*.ts"
     ]