Skip to content

Commit

Permalink
update for better use as module (#66)
Browse files Browse the repository at this point in the history
* fix name

* add version

* update build system

- export to `hacks.<hack_name>` instead of stripping exports with plugin
- split build script into:
  - `build`: accepts filenames and outputs built hacks
  - `cli`: builds hacks from repo source
  - `index`: exports the build function
- add optional command line args to build script

* update dependencies for testing

* update lockfile

* update version

to account for new export behaviour

all minor, except corrupt which is major due to a complication in how hackOptions is being used (pretty sure that one's broken atm anyway)

* run build
  • Loading branch information
seleb committed Feb 24, 2019
1 parent 7cd1de1 commit 3000226
Show file tree
Hide file tree
Showing 85 changed files with 2,923 additions and 2,248 deletions.
11 changes: 8 additions & 3 deletions .babelrc
@@ -1,5 +1,10 @@
{
"presets": [
"env"
]
}
"@babel/env"
],
"env": {
"test": {
"plugins": ["@babel/plugin-transform-runtime"]
}
}
}
21 changes: 0 additions & 21 deletions StripExportsPlugin.mjs

This file was deleted.

54 changes: 17 additions & 37 deletions build.mjs
@@ -1,57 +1,37 @@
import { readdirSync } from "fs";
import {
basename
} from "path";

import rollup from "rollup";
import clear from "rollup-plugin-clear";
import eslint from "rollup-plugin-eslint";
import nodeResolve from "rollup-plugin-node-resolve";
import commonjs from "rollup-plugin-commonjs";

import headerComment from "./HeaderCommentPlugin";
import topLevelOptions from "./TopLevelOptionsPlugin";
import readme from "./ReadmePlugin";
import stripExportsPlugin from "./StripExportsPlugin";

const inputDir = "./src/";
const outputDir = "./dist/";

function build(src) {
export async function buildOne(src = '', plugins = []) {
const inputOptions = {
input: `${inputDir}${src}`,
input: src,
external: [
'bitsy'
],
plugins: [
clear({
targets: [outputDir]
}),
nodeResolve(),
commonjs(),
stripExportsPlugin(),
readme.plugin(),
headerComment(),
topLevelOptions(),
eslint({})
]
commonjs()
].concat(plugins)
};

const outputOptions = {
file: `${outputDir}${src.replace(/\s/g, '-')}`,
format: "iife",
globals: {
bitsy: 'window'
}
},
name: `hacks.${basename(src, '.js').replace(/\s/g,'_')}`,
};

return rollup.rollup(inputOptions)
.then(bundle => {
return bundle.write(outputOptions);
});
const bundle = await rollup.rollup(inputOptions)
const output = await bundle.generate(outputOptions);
return output.code;
}

Promise.all(
readdirSync(inputDir)
.filter(file => file.match(/^.*?(?<!\.test)\.js$/))
.map(build)
).then(() => {
readme.parse();
readme.write();
});
export async function build(hacks = [], plugins) {
const output = await Promise.all(hacks.map(hack => buildOne(hack, plugins)));
return output;
}
58 changes: 58 additions & 0 deletions cli.mjs
@@ -0,0 +1,58 @@
import fs from 'fs';
import {
basename
} from "path";
import eslint from "rollup-plugin-eslint";

import headerComment from "./HeaderCommentPlugin";
import topLevelOptions from "./TopLevelOptionsPlugin";
import readme from "./ReadmePlugin";

import build from './index';

const fsp = fs.promises;

const inputDir = "./src/";
const outputDir = "./dist/";

async function buildHacks(hacks) {
const outputFiles = hacks.map(name => `${outputDir}${basename(name).replace(/\s/g, '-')}`);

// make dist
!fs.existsSync(outputDir) && await fsp.mkdir(outputDir);

// cleanup old dist
await Promise.all(outputFiles.map(file => fs.existsSync(file) && fsp.unlink(file)));

// build
const output = await build(hacks, [
eslint({}),
readme.plugin(),
headerComment(),
topLevelOptions()
]);

// write to dist
await Promise.all(output.map((file, idx) => fsp.writeFile(outputFiles[idx], file)));

readme.parse();
readme.write();
}

async function getArgs() {
// use command-line args if present
if (process.argv.length > 2) {
return process.argv.slice(2);
}

// use source files otherwise
const srcFiles = await fsp.readdir(inputDir);
return srcFiles
.filter(file => file.match(/^.*?(?<!\.test)\.js$/))
.map(file => `${inputDir}${file}`);
}

getArgs()
.then(buildHacks)
.then(() => console.log('👍'))
.catch(err => console.error('👎\n', err));
11 changes: 8 additions & 3 deletions dist/avatar-by-room.js
Expand Up @@ -3,7 +3,7 @@
@file avatar by room
@summary change the avatar in certain rooms
@license MIT
@version 1.0
@version 1.1.0
@requires 5.3
@author Sean S. LeBlanc
Expand All @@ -17,7 +17,8 @@ HOW TO USE:
By default, the avatar will reset to the default if you enter a room without a sprite defined.
This can also be changed in the hackOptions below to instead apply avatar changes permanently.
*/
(function (bitsy) {
this.hacks = this.hacks || {};
this.hacks.avatar_by_room = (function (exports,bitsy) {
'use strict';
var hackOptions = {
permanent: false, // If true, avatar changes will persist across rooms without sprites defined
Expand Down Expand Up @@ -293,4 +294,8 @@ before('update', function () {
}
});

}(window));
exports.hackOptions = hackOptions;

return exports;

}({},window));
11 changes: 8 additions & 3 deletions dist/basic-sfx.js
Expand Up @@ -3,7 +3,7 @@
@file basic sfx
@summary "walk" and "talk" sound effect support
@license MIT
@version 1.0.0
@version 1.1.0
@author Sean S. LeBlanc
@description
Expand All @@ -22,7 +22,8 @@ HOW TO USE:
Additional sounds can be added by by including more <audio> tags with different ids and calling `sounds.<sound id>()` as needed.
*/
(function (bitsy) {
this.hacks = this.hacks || {};
this.hacks.basic_sfx = (function (exports,bitsy) {
'use strict';
var hackOptions = {
beNiceToEars: true // if `true`, reduces volume of recently played sound effects
Expand Down Expand Up @@ -95,4 +96,8 @@ bitsy.dialogBuffer.FlipPage = function () {
sounds.talk();
};

}(window));
exports.hackOptions = hackOptions;

return exports;

}({},window));
11 changes: 8 additions & 3 deletions dist/bitsymuse.js
Expand Up @@ -3,7 +3,7 @@
@file bitsymuse
@summary A variety of Bitsy sound and music handlers
@license MIT
@version 2.1.2
@version 2.2.0
@requires 4.8, 4.9
@author David Mowatt
Expand Down Expand Up @@ -33,7 +33,8 @@ You can also use a special ID ("S" by default) to Silence the music.
By default, music tracks automatically restart from the beginning if you go back to a previous track.
This can also be changed in the hackOptions below.
*/
(function (bitsy) {
this.hacks = this.hacks || {};
this.hacks.bitsymuse = (function (exports,bitsy) {
'use strict';
var hackOptions = {
// You need to put an entry in this list for every room ID or name that is accessible by the player,
Expand Down Expand Up @@ -443,4 +444,8 @@ addDialogTag('soundeffect', function (environment, parameters, onReturn) {
});
// End of (music) dialog function mod

}(window));
exports.hackOptions = hackOptions;

return exports;

}({},window));
11 changes: 8 additions & 3 deletions dist/canvas-replacement.js
Expand Up @@ -3,7 +3,7 @@
@file canvas replacement
@summary WebGLazy bitsy integration (this one's mostly just for me)
@license MIT
@version 2.0.0
@version 2.1.0
@author Sean S. LeBlanc
@description
Expand Down Expand Up @@ -35,7 +35,8 @@ e.g.
}
(closing script tag omitted in comment to avoid confusing browser)
*/
(function (bitsy) {
this.hacks = this.hacks || {};
this.hacks.canvas_replacement = (function (exports,bitsy) {
'use strict';
var hackOptions = {
glazyOptions: {
Expand Down Expand Up @@ -252,4 +253,8 @@ after('update', function () {
hackOptions.update(glazy);
});

}(window));
exports.hackOptions = hackOptions;

return exports;

}({},window));
3 changes: 2 additions & 1 deletion dist/character-portraits.js
Expand Up @@ -3,7 +3,7 @@
@file character portraits
@summary high quality anime jpegs (or pngs i guess)
@license MIT
@version 1.0.0
@version 1.1.0
@requires Bitsy Version: 5.3
@author Sean S. LeBlanc
Expand Down Expand Up @@ -31,6 +31,7 @@ Note: The hack is called "character portraits", but this can easily be used to s
HOW TO USE:
Copy-paste into a script tag after the bitsy source
*/
this.hacks = this.hacks || {};
(function (bitsy) {
'use strict';
var hackOptions = {
Expand Down
3 changes: 2 additions & 1 deletion dist/close-on-ending.js
Expand Up @@ -3,7 +3,7 @@
@file close on ending
@summary Prevents from playing past an ending
@license MIT
@version 1.0.0
@version 1.1.0
@author Sean S. LeBlanc
@description
Expand All @@ -19,6 +19,7 @@ but players will still be able to manually refresh or close/re-open the page to
HOW TO USE:
Copy-paste this script into a script tag after the bitsy source
*/
this.hacks = this.hacks || {};
(function (bitsy) {
'use strict';

Expand Down

0 comments on commit 3000226

Please sign in to comment.