Skip to content

Commit

Permalink
Merge pull request #11 from regal/feat/5-ts-option
Browse files Browse the repository at this point in the history
feat(bundle): Implement bundler.input.ts option
  • Loading branch information
jcowman2 committed Dec 20, 2018
2 parents c770338 + 131f61d commit 180fe25
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 20 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
"[typescript]": {
"editor.formatOnSave": true,
},
"[javascript]": {
"editor.formatOnSave": true,
},
"tslint.autoFixOnSave": true
}
11 changes: 5 additions & 6 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
collectCoverageFrom: [
"src/**"
]
};
preset: "ts-jest",
testEnvironment: "node",
collectCoverageFrom: ["src/**"],
setupTestFrameworkScriptFile: "<rootDir>/test/jest-setup.js"
};
42 changes: 31 additions & 11 deletions src/bundle.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GameMetadata } from "regal";
import * as rollup from "rollup";
import * as _commonjs from "rollup-plugin-commonjs";
import * as insert from "rollup-plugin-insert";
import * as _json from "rollup-plugin-json";
import * as _resolve from "rollup-plugin-node-resolve";
Expand All @@ -13,32 +13,52 @@ import { BundlerOptions, RecursivePartial } from "./interfaces-public";
const json = _json;
const resolve = _resolve;
const typescript = _typescript;
const commonjs = _commonjs;

export const bundleFooter = (md: GameMetadata) => `
const esFooter = (metadata: string) => `
import { Game } from "regal";
/** Initialize game **/
Game.init(${JSON.stringify(md, undefined, 2)});
Game.init(${metadata});
export { Game as default };
`;

const cjsFooter = (metadata: string) => `
const Game = require("regal").Game;
/** Initialize game **/
Game.init(${metadata});
module.exports = Game;
`;

export const bundleFooter = (config: LoadedConfiguration) => {
const footer = config.bundler.input.ts ? esFooter : cjsFooter;
return footer(JSON.stringify(config.game, undefined, 2));
};

export const bundleHeader = () => "/** BUNDLED GAME */";

export const getPlugins = (config: LoadedConfiguration): rollup.Plugin[] => {
const plugins: rollup.Plugin[] = [];

plugins.push(
(typescript as any)({
tsconfigOverride: {
compilerOptions: { module: "es2015" }
}
insert.append(bundleFooter(config), {
include: config.bundler.input.file
}),
resolve(),
json({ exclude: "node_modules/**" }),
insert.append(bundleFooter(config.game), {
include: config.bundler.input.file
})
json({ exclude: "node_modules/**" })
);

if (config.bundler.input.ts) {
plugins.unshift(
(typescript as any)({
tsconfigOverride: {
compilerOptions: { module: "es2015" }
}
})
);
} else {
plugins.push(commonjs());
}

if (config.bundler.output.minify) {
plugins.push(terser({ output: { preamble: bundleHeader() } }));
}
Expand Down
8 changes: 5 additions & 3 deletions src/get-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ export const loadUserConfig = async (
const searchResult = await explorer.search(configLocation);

if (searchResult === null) {
config = {
game: {}
};
config = {};
} else {
config = searchResult.config;
}

if (config.game === undefined) {
config.game = {};
}

const pkgPath = path.join(configLocation, "package.json");
const pkg = await import(pkgPath);

Expand Down
38 changes: 38 additions & 0 deletions test/cases/js-basic/js-basic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { GameApi } from "regal";
import { lines } from "../../test-utils";

// @ts-ignore: import will be resolved
import { bundle } from "../../../dist/regal-bundler.cjs.js";

describe("Case: JS Basic", () => {
beforeAll(async () => {
await bundle({ configLocation: __dirname });
});

it("Creates a functional bundle without typescript", async () => {
// @ts-ignore: import will be resolved
const Game: GameApi = await import("./js-basic.regal.js");

let response = Game.postStartCommand();
expect(response.output.wasSuccessful).toBe(true);
expect(lines(response)).toEqual(["Game initialized to zero."]);

response = Game.postPlayerCommand(response.instance, "inc");
expect(response.output.wasSuccessful).toBe(true);
expect(lines(response)).toEqual([
"Game state incremented from 0 to 1."
]);

for (let i = 0; i < 5; i++) {
response = Game.postPlayerCommand(response.instance, "dec");
}
expect(response.output.wasSuccessful).toBe(true);
expect(lines(response)).toEqual([
"Game state decremented from -3 to -4."
]);

response = Game.postPlayerCommand(response.instance, "woof");
expect(response.output.wasSuccessful).toBe(true);
expect(lines(response)).toEqual(["Command not recognized: 'woof'."]);
});
});
20 changes: 20 additions & 0 deletions test/cases/js-basic/package-lock.json

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

14 changes: 14 additions & 0 deletions test/cases/js-basic/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "js-basic",
"author": "author",
"regal": {
"bundler": {
"input": {
"ts": false
}
}
},
"dependencies": {
"regal": "^0.7.1"
}
}
29 changes: 29 additions & 0 deletions test/cases/js-basic/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const regal = require("regal");

const init = regal.on("INIT", game => {
game.output.write("Game initialized to zero.");
game.state.num = 0;
});

const incr = regal.on("INCR", game =>
game.output.write(
`Game state incremented from ${game.state.num} to ${++game.state.num}.`
)
);

const decr = regal.on("DECR", game =>
game.output.write(
`Game state decremented from ${game.state.num} to ${--game.state.num}.`
)
);

regal.onStartCommand(init);
regal.onPlayerCommand(cmd => game => {
if (cmd.startsWith("i")) {
return incr;
} else if (cmd.startsWith("d")) {
return decr;
}
game.output.write(`Command not recognized: '${cmd}'.`);
return regal.noop;
});
1 change: 1 addition & 0 deletions test/jest-setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
jest.setTimeout(10000);
16 changes: 16 additions & 0 deletions test/unit/bundle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,21 @@ describe("Bundle", () => {
const plugins = getPlugins(config);
expect(plugins.find(p => p.name === "terser")).toBeUndefined();
});

it("Includes typescript plugin if input.ts is true", () => {
const config = sampleConfig();
config.bundler.input.ts = true;

const plugins = getPlugins(config);
expect(plugins.find(p => p.name === "rpt2")).not.toBeUndefined();
});

it("Does not include typescript plugin if input.ts is false", () => {
const config = sampleConfig();
config.bundler.input.ts = false;

const plugins = getPlugins(config);
expect(plugins.find(p => p.name === "rpt2")).toBeUndefined();
});
});
});

0 comments on commit 180fe25

Please sign in to comment.