Skip to content

Commit

Permalink
Modern ES2015+ syntax and ESModules
Browse files Browse the repository at this point in the history
This is an effort towards making this a Typescript library
* added Babel.js in the process (merged with browserify)
* preserved the compilation process
* replaced legacy JSHint with JSLint
* ensured that tests passes in QUnit
* extracted error reporting functions into a separate error.js file
* converted files into ESModules (the compilation target is still UMD though)
* preserved the way that sax is included in "imsc.all.*" build and excluded from "imsc.*" builds

--

Todos:
* extract IMSC objects classes into separate file
* move to the Typescript compiler (google-closure-compiler can still be used for minification)
* extract proper TS interfaces
* export types and extract to *.d.ts definition file
* fix potentially buggy code (replacing string with object in array, etc.)
* use ValueObject pattern (immutable IMSC classes with parameter injection via constructor, instead of "initFromNode")
* cloning process improvement
  • Loading branch information
BasBastian committed Mar 6, 2022
1 parent a6ed5f4 commit 22d7d2d
Show file tree
Hide file tree
Showing 13 changed files with 12,145 additions and 4,001 deletions.
33 changes: 33 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module.exports = {
"env": {
"browser": true,
"commonjs": true,
"es2021": true,
"node": true
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": "latest"
},
globals: {
'sax' : "writable"
},
"rules": {
"indent": [
"error",
4
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
]
}
};
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ dist/
node_modules/
nbproject/private
ga.js
.vscode/
.vscode/
.idea/
196 changes: 114 additions & 82 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,96 +1,124 @@
const babelConfig = require('./babel.config');

module.exports = function (grunt) {

grunt.initConfig({

properties: grunt.file.readJSON('properties.json'),

pkg: grunt.file.readJSON('package.json'),

clean: ['<%= properties.webappBuildDir %>', '<%= properties.umdBuildDir %>'],

sync: {
all: {
files:
[
// copy tests
{ expand: true, cwd: '<%= properties.webappTestDir %>', src: '**', dest: '<%= properties.webappBuildDir %>' },

// copy tests resources
{ expand: true, cwd: '<%= properties.unitTestsResourcesDir %>', src: 'imsc-tests/imsc1/ttml/**', dest: '<%= properties.webappBuildDir %>' },
{ expand: true, cwd: '<%= properties.unitTestsResourcesDir %>', src: 'imsc-tests/imsc1/tests.json', dest: '<%= properties.webappBuildDir %>' },
{ expand: true, cwd: '<%= properties.unitTestsResourcesDir %>', src: 'imsc-tests/imsc1_1/ttml/**', dest: '<%= properties.webappBuildDir %>' },
{ expand: true, cwd: '<%= properties.unitTestsResourcesDir %>', src: 'imsc-tests/imsc1_1/tests.json', dest: '<%= properties.webappBuildDir %>' },
{ expand: true, cwd: '<%= properties.unitTestsResourcesDir %>', src: 'unit-tests/**', dest: '<%= properties.webappBuildDir %>' }
]
},

release: {
src: '<%= properties.umdBuildDir %>/<%= properties.umdMinName %>',
dest: '<%= properties.webappBuildDir %>/libs/imsc.js'
properties: grunt.file.readJSON('properties.json'),

pkg: grunt.file.readJSON('package.json'),

clean: ['<%= properties.webappBuildDir %>', '<%= properties.umdBuildDir %>'],

sync: {
all: {
files:
[
// copy tests
{
expand: true,
cwd: '<%= properties.webappTestDir %>',
src: '**',
dest: '<%= properties.webappBuildDir %>'
},

// copy tests resources
{
expand: true,
cwd: '<%= properties.unitTestsResourcesDir %>',
src: 'imsc-tests/imsc1/ttml/**',
dest: '<%= properties.webappBuildDir %>'
},
{
expand: true,
cwd: '<%= properties.unitTestsResourcesDir %>',
src: 'imsc-tests/imsc1/tests.json',
dest: '<%= properties.webappBuildDir %>'
},
{
expand: true,
cwd: '<%= properties.unitTestsResourcesDir %>',
src: 'imsc-tests/imsc1_1/ttml/**',
dest: '<%= properties.webappBuildDir %>'
},
{
expand: true,
cwd: '<%= properties.unitTestsResourcesDir %>',
src: 'imsc-tests/imsc1_1/tests.json',
dest: '<%= properties.webappBuildDir %>'
},
{
expand: true,
cwd: '<%= properties.unitTestsResourcesDir %>',
src: 'unit-tests/**',
dest: '<%= properties.webappBuildDir %>'
}
]
},

release: {
src: '<%= properties.umdBuildDir %>/<%= properties.umdMinName %>',
dest: '<%= properties.webappBuildDir %>/libs/imsc.js'
},

debug: {
src: '<%= properties.umdBuildDir %>/<%= properties.umdDebugName %>',
dest: '<%= properties.webappBuildDir %>/libs/imsc.js'
}
},

debug: {
src: '<%= properties.umdBuildDir %>/<%= properties.umdDebugName %>',
dest: '<%= properties.webappBuildDir %>/libs/imsc.js'
}
},

npmcopy: {
default: {
files: {
'<%= properties.webappBuildDir %>/libs/': [
'sax:main',
'qunit-assert-close:main',
'qunitjs:main',
'filesaver.js-npm:main',
'jszip/dist/jszip.js'
]
}
}
},

browserify: [
{
src: "<%= pkg.main %>",
dest: "<%= properties.umdBuildDir %>/<%= properties.umdDebugName %>",
options: {
exclude: ["sax"],
browserifyOptions: {
standalone: 'imsc'
npmcopy: {
default: {
files: {
'<%= properties.webappBuildDir %>/libs/': [
'sax:main',
'qunit-assert-close:main',
'qunitjs:main',
'filesaver.js-npm:main',
'jszip/dist/jszip.js'
]
}
}
},
{
src: "<%= pkg.main %>",
dest: "<%= properties.umdBuildDir %>/<%= properties.umdAllDebugName %>",
options: {
browserifyOptions: {
standalone: 'imsc'

browserify: [
{
src: "<%= pkg.main %>",
dest: "<%= properties.umdBuildDir %>/<%= properties.umdDebugName %>",
options: {
transform: [["babelify", babelConfig]],
exclude: ["sax"],
browserifyOptions: {
standalone: 'imsc'
}
}
},
{
src: "<%= pkg.main %>",
dest: "<%= properties.umdBuildDir %>/<%= properties.umdAllDebugName %>",
options: {
transform: [["babelify", babelConfig]],
browserifyOptions: {
standalone: 'imsc'
}
}
}
}
],
],

jshint: {
'default': {
src: "src/main/js",
options: {
"-W032": true
}
}
},

exec: {
minify:
{
cmd: [
"npx google-closure-compiler --js=<%= properties.umdBuildDir %>/<%= properties.umdAllDebugName %> --js_output_file=<%= properties.umdBuildDir %>/<%= properties.umdAllMinName %>",
"npx google-closure-compiler --js=<%= properties.umdBuildDir %>/<%= properties.umdDebugName %> --js_output_file=<%= properties.umdBuildDir %>/<%= properties.umdMinName %>"
].join("&&")
eslint: {
target: "src/main",
},

exec: {
minify:
{
cmd: [
"npx google-closure-compiler --js=<%= properties.umdBuildDir %>/<%= properties.umdAllDebugName %> --js_output_file=<%= properties.umdBuildDir %>/<%= properties.umdAllMinName %>",
"npx google-closure-compiler --js=<%= properties.umdBuildDir %>/<%= properties.umdDebugName %> --js_output_file=<%= properties.umdBuildDir %>/<%= properties.umdMinName %>"
].join("&&")
}
}
}
}

);

grunt.loadNpmTasks('grunt-contrib-clean');
Expand All @@ -99,15 +127,19 @@ module.exports = function (grunt) {

grunt.loadNpmTasks('grunt-browserify');

grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-babel');

grunt.loadNpmTasks('grunt-eslint');

grunt.loadNpmTasks('grunt-sync');

grunt.loadNpmTasks('grunt-exec');

grunt.registerTask('build:release', ['jshint', 'browserify', 'exec:minify', 'sync:all', 'sync:release', 'npmcopy']);
// Register tasks

grunt.registerTask('build:release', ['eslint', 'browserify', 'exec:minify', 'sync:all', 'sync:release', 'npmcopy']);

grunt.registerTask('build:debug', ['jshint', 'browserify', 'exec:minify', 'sync:all', 'sync:debug', 'npmcopy']);
grunt.registerTask('build:debug', ['eslint', 'browserify', 'exec:minify', 'sync:all', 'sync:debug', 'npmcopy']);

grunt.registerTask('build', ['build:debug']);

Expand Down
9 changes: 9 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
"presets": ["@babel/preset-env"],
"plugins": ["@babel/plugin-transform-modules-umd", {
}],
"include" : [
"@babel/plugin-transform-classes"
],
"moduleId": "imsc"
};
Loading

0 comments on commit 22d7d2d

Please sign in to comment.