Compare commits

..

2 Commits

Author SHA1 Message Date
483582842a [README]: Added sections Usage and Images 2020-04-21 11:24:24 +02:00
8ebac5da24 [script]: Added watch script 2020-04-21 11:23:18 +02:00
6 changed files with 5052 additions and 1252 deletions

View File

@ -4,12 +4,17 @@ module.exports = {
plugins: [ plugins: [
'@typescript-eslint', '@typescript-eslint',
], ],
env: {
"node": true,
"browser": true
},
extends: [ extends: [
'eslint:recommended', 'eslint:recommended',
'plugin:@typescript-eslint/eslint-recommended', 'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended', 'plugin:@typescript-eslint/recommended',
], ],
rules: { rules: {
"@typescript-eslint/no-empty-function": "off" "@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/explicit-function-return-type": "off"
} }
}; };

2
.gitignore vendored
View File

@ -1,2 +1,4 @@
dist dist
node_modules node_modules
._.*
.DS_Store

View File

@ -1,6 +1,8 @@
# asdf-browser-template # asdf-browser-template
This is a template for creating games in the browser with the [asdf-games](https://gitea.arnweb.nl/arne/asdf-games) framework. It works with gulp (and plugins), browserify, tsify, babelify and eslint. These packages allow you to create browser games with asdf in typescript without having to worry about browser compatibility (babel). This is a template for creating games in the browser with the [asdf-games](https://gitea.arnweb.nl/arne/asdf-games) framework. It works with gulp, browserify, tsify, babelify, eslint and more other plugins. These packages allow you to create browser games with asdf in typescript without having to worry about browser compatibility (babel) or refreshing your page (live-server & gulp).
## 🏗️ Installation
To get started, clone the repository and run npm: To get started, clone the repository and run npm:
```bash ```bash
@ -8,4 +10,12 @@ git clone https://gitea.arnweb.nl/corner/asdf-browser-template.git && cd asdf-br
npm i npm i
``` ```
## 💻 Usage
You are now ready to rock! Start coding some great games! If you want to transpile your project into valid JS and HTML run `npm run build`. This will create a output.js, output.min.js and an index.html. Open up the index.html in your browser, and there is your game, ready to be distributed. You are now ready to rock! Start coding some great games! If you want to transpile your project into valid JS and HTML run `npm run build`. This will create a output.js, output.min.js and an index.html. Open up the index.html in your browser, and there is your game, ready to be distributed.
If you want to get the best developer experience with hot reload, run `npm run watch`. This will launch a browser window with your game. If you change anything in your `src` folder, gulp will re-transpile the project and live-server will then reload the browser page.
## 🖼️ Images
If you want to make use of images in your project, create the folder `src/res`, and place your images in there. The `res` folder will also be transpiled (imagemin minifies the files) and the files are, then, placed in the `dist` folder. The pathnames will also stay the same.

View File

@ -1,60 +1,86 @@
var gulp = require('gulp') /* eslint-disable @typescript-eslint/no-var-requires */
const tsify = require('tsify'); const gulp = require("gulp");
var sourcemaps = require('gulp-sourcemaps'); const tsify = require("tsify");
const rename = require('gulp-rename'); const sourcemaps = require("gulp-sourcemaps");
const browserify = require('browserify'); const rename = require("gulp-rename");
const source = require('vinyl-source-stream'); const browserify = require("browserify");
const buffer = require('vinyl-buffer'); const source = require("vinyl-source-stream");
const terser = require('gulp-terser'); const buffer = require("vinyl-buffer");
const eslint = require('gulp-eslint'); const terser = require("gulp-terser");
const babelify = require('babelify'); const eslint = require("gulp-eslint");
const htmlmin = require('gulp-html-minifier-terser'); const babelify = require("babelify");
const htmlvalidator = require('gulp-html'); const htmlmin = require("gulp-html-minifier-terser");
const htmlvalidator = require("gulp-html");
const imagemin = require('gulp-imagemin');
const gulpif = require('gulp-if');
const clean = require('gulp-dest-clean');
const html = () => { const html = () => {
return gulp.src('src/index.html') return gulp.src("src/index.html")
.pipe(htmlvalidator()) .pipe(htmlvalidator())
.pipe(htmlmin({ .pipe(htmlmin({
removeComments: true, removeComments: true,
collapseWhitespace: true, collapseWhitespace: true,
collapseBooleanAttributes: true, collapseBooleanAttributes: true,
collapseInlineTagWhitespace: true, collapseInlineTagWhitespace: true,
removeRedundantAttributes: true removeRedundantAttributes: true,
minifyCSS: true
})) }))
.pipe(gulp.dest('dist')); .pipe(gulp.dest("dist"));
} };
const js = () => { const js = () => {
gulp.src(['src/**/*.ts', '!node_modules/**']) gulp.src(["src/**/*.ts", "!node_modules/**"])
.pipe(eslint({quiet: true})) .pipe(eslint({quiet: true}))
.pipe(eslint.results(results => { .pipe(eslint.results(results => {
// Called once for all ESLint results. // Called once for all ESLint results.
console.log('ESLint results:\n'); console.log("ESLint results:\n");
console.log(`Total Errors: ${results.errorCount}`); console.log(`Total Errors: ${results.errorCount}`);
let output = ''; let output = "";
results.forEach(file => { results.forEach(file => {
output += file.filePath + '\n'; output += file.filePath + "\n";
file.messages.forEach(message => { file.messages.forEach(message => {
output += ' - ' + message.message + ' at ' + message.line + ':' + message.column + '\n'; output += " - " + message.message + " at " + message.line + ":" + message.column + "\n";
}); });
output += '\n'; output += "\n";
}); });
console.error(output); console.error(output);
})) }))
.pipe(eslint.failAfterError()); .pipe(eslint.failAfterError());
return browserify('src/index.ts') return browserify("src/index.ts")
.plugin(tsify, { noImplicitAny: true, target: 'es6' }) .plugin(tsify, { noImplicitAny: true, target: "es6" })
.transform(babelify, { extensions: [ '.tsx', '.ts' ] }) .transform(babelify, { extensions: [ ".tsx", ".ts" ] })
.bundle().on('error', (e) => console.error(e)) .bundle().on("error", (e) => console.error(e))
.pipe(source('output.js')) .pipe(source("output.js"))
.pipe(gulp.dest('dist')) .pipe(gulp.dest("dist"))
.pipe(buffer()) .pipe(buffer())
.pipe(sourcemaps.init()) .pipe(sourcemaps.init())
.pipe(terser()) .pipe(terser())
.pipe(rename({ extname: '.min.js' })) .pipe(rename({ extname: ".min.js" }))
.pipe(sourcemaps.write()) // Now the sourcemaps are added to the .js file .pipe(sourcemaps.write()) // Now the sourcemaps are added to the .js file
.pipe(gulp.dest('dist')); .pipe(gulp.dest("dist"));
}; };
exports.default = gulp.parallel(js, html); const res = () => {
return gulp.src("src/res/*")
.pipe(clean("dist/res"))
.pipe(gulpif(
file => file.basename.match(/\.png|jpg|jpeg|gif|svg/gmi) !== [],
imagemin()
))
.pipe(gulp.dest("dist/res"));
};
exports.default = gulp.parallel(js, html, res);
exports.watch = () => {
console.log("Transpiling current files...");
js();
html();
res();
console.log("Watching for changes!");
gulp.watch(["src/**/*.js", "src/**/*.ts"], js);
gulp.watch("src/*.html", html);
gulp.watch("src/res/*", res);
};

6165
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -4,7 +4,8 @@
"description": "A template for creating games for the browser with asdf-games.", "description": "A template for creating games for the browser with asdf-games.",
"main": "src/index.ts", "main": "src/index.ts",
"scripts": { "scripts": {
"build": "gulp" "build": "gulp",
"watch": "concurrently \"live-server dist\" \"gulp watch\""
}, },
"keywords": [ "keywords": [
"asdf", "asdf",
@ -23,14 +24,19 @@
"@typescript-eslint/parser": "^2.28.0", "@typescript-eslint/parser": "^2.28.0",
"babelify": "^10.0.0", "babelify": "^10.0.0",
"browserify": "^16.5.1", "browserify": "^16.5.1",
"concurrently": "^5.1.0",
"eslint": "^6.8.0", "eslint": "^6.8.0",
"gulp": "^4.0.2", "gulp": "^4.0.2",
"gulp-dest-clean": "^0.5.0",
"gulp-eslint": "^6.0.0", "gulp-eslint": "^6.0.0",
"gulp-html": "^2.0.0", "gulp-html": "^2.0.0",
"gulp-html-minifier-terser": "^6.0.0", "gulp-html-minifier-terser": "^6.0.0",
"gulp-if": "^3.0.0",
"gulp-imagemin": "^7.1.0",
"gulp-rename": "^2.0.0", "gulp-rename": "^2.0.0",
"gulp-sourcemaps": "^2.6.5", "gulp-sourcemaps": "^2.6.5",
"gulp-terser": "^1.2.0", "gulp-terser": "^1.2.0",
"live-server": "^1.2.1",
"tsify": "^4.0.1", "tsify": "^4.0.1",
"typescript": "^3.8.3", "typescript": "^3.8.3",
"vinyl-buffer": "^1.0.1", "vinyl-buffer": "^1.0.1",