1.0.0 • Published 12 months ago

@oat-sa/grunt-tao-bundle v1.0.0

Weekly downloads
41
License
GPL-2.0
Repository
github
Last release
12 months ago

grunt-tao-bundle

Grunt task to bundle assets from a TAO Extension.

Concept

The TAO software architecture is based on atomic components called extensions. Each extension provides it's own source code (client and server side code). TAO extensions can be dependent on other extensions.

In order to create bundles that doesn't impact the extension dependency model, we bundle the code for a given extension only, no matter if an asset is dependent of an asset in another extension.

This task produce bundles sandboxed to a given extension.

The task is made of 2 steps : 1. The bundling, based on the configuration, we aggregate the assets of the extension, into a single file. Since the TAO client source code is managed using AMD modules and use some requirejs internals, we use the r.js optimizer to parse the dependency tree and bundle the assets. We don't use it to minimify the resources. 2. The transform step is used to either transpile the code, using Babel 7 (still experimental) or UglifyJs to compress and minimify it.

tao bundler concept

Getting Started

This plugin requires node.js >= 8.6.0 and to be installed along with Grunt. To add install it :

npm install @oat-sa/grunt-tao-bundle --save-dev

The "tao-bundle" task

Overview

In your project's Gruntfile, add a section named bundle to the data object passed into grunt.initConfig() or grunt.config.merge().

grunt.initConfig({
  bundle: {
    options: {
        amd : require('config/amd.json')
    },
    taoce : {
        options : {
            extension : 'taoCe',
            extensionPath : root + '/taoCe/views/js',
            outputDir : 'loader',
            bundles : [{
                name : 'taoCe',
                default : true,
                babel: true
            }]
        }
    }
  },
})

Options

OptionTypeDescriptionScopeExample value
amdObjectThe AMD/requirejs configurationAll extensions, all bundles
amd.baseUrlStringRelative path to resolve the AMD modulesAll extensions, all bundles../js
amd.pathsObjectThe list of AMD paths/aliasAll extensions, all bundles
amd.shimObjectthe AMD shim configurationAll extensions, all bundle
amd.vendorString[]A list of vendor modulesAll extensions, all bundle['lodash', 'core/**/*']
amd.excludeString[]A list of modules to always excludeAll extensions, all bundle['mathJax', 'ckeditor']
amd.bootstrapString[]A list of module to include to bootstrap a bundleAll extensions, all bundle['loader/bootrstrap']
amd.defaultString[]A list of modules to include for default bundles. It's the default pattern for TAO extension source code.All extensions, all bundle['controller/**/*', 'component/**/*', 'provider/**/*']
pathsObjectAdditional list of paths/aliasThe current extension
workDirStringThe temporary working directory to copy the sources and generates the bundlesThe current extensionoutput
outputDirStringThe directory (inside the workDir) where the bundles will be generatedThe current extensionloader
extensionStringThe name of the current extensionThe current extensiontaoQtiTest
rootExtensionStringThe name of the root extension (the one with no prefix in AMD and that contains the libs and SDK)All extensionstao
dependenciesString[]The list of extension dependencies, the code from a dependency is excluded from the bundle.The current extensions['taoItems', 'taoTests', 'taoQtiItem']
allowExternalString[]Allow external dependency into the bundle. It's not a good practice, but this can be used for aliases. Allow them to be included without the bundler to warn you about forbidden dependency.The current extensions['qtiInfoControlContext']
getExtensionPathFunctionA function that resolves the path to the JS files from an extensionAll extensionsextension => root + '/' + extension + '/views/js'
getExtensionCssPathFunctionA function that resolves the path to the CSS files from an extensionAll extensionsextension => root + '/' + extension + '/views/css'
bundlesObject[]The bundles configurationThe current extensions
bundle.nameStringThe bundle name without the file extensionThe current bundlevendor or taoQtiTest
bundle.vendorBooleanWhen true, the bundle will include ONLY the libraries from amd.vendor (Default : false)The current bundle
bundle.standaloneBooleanWhen true, the bundle will include ALL dependencies so that it can be run as a standalone (Default : false)The current bundle
bundle.bootstrapBooleanWhen true, the bundle will include the modules from amd.bootstrap (Default : false)The current bundle
bundle.entryPointStringDefine an entryPoint for the bundle. Useful to create a separate bundle for a given entryPointThe current bundle'controller/login'
bundle.defaultBooleanWhen true, the bundle will include the modules from amd.default (Default : false)The current bundle
bundle.includeString[]A list of additional module to add to the bundle, especially when modules are not in the correct folder.The current bundle['taoQtiItem/qtiCommonRenderer/**/*']
bundle.excludeString[]A list of additional module to exclude from the bundle, but if module has nested dependencies that are part of the built fileThe current bundle['taoQtiItem/qtiItem/test/**/*']
bundle.excludeNestedString[]A list of module to exclude from the bundle. Excludes all nested dependencies from the built fileThe current bundle['taoQtiItem/qtiItem/test/**/*']
bundle.dependenciesString[]Override the extension dependency loading : if set, loads the exact list of moduleThe current bundle['taoQtiItem/loader/taoQtiItemRunner.min']
bundle.babelBooleanExperimental When true, the bundle will use Babel to transpile the code (Default : false)The current bundle
bundle.targetsObject,StringExperimental A specific target for transpiling the code when using Babel (Default : 'extends @oat-sa/browserslist-config-tao')The current bundle{ ie: '11' }
bundle.uglifyBooleanWhen true, the bundle will be minimified (Default : true)The current bundle
bundle.packageObject[]Extends packages to the bundle (Default : [])The current bundle[{ name: 'codemirror', location: '../../../tao/views/node_modules/codemirror', main: 'lib/codemirror'}]

Examples

The configuration from the tao extension :

grunt.config.merge({
    bundle : {
        //define the global options for all extensions' tasks
        options: {
            rootExtension        : 'tao',
            getExtensionPath     : extension => `${root}/${extension}/views/js`,
            getExtensionCssPath  : extension => `${root}/${extension}/views/css`,
            amd                  : require('../config/requirejs.build.json'),
            workDir              : 'output',
            outputDir            : 'loader'
        },

        tao : {
            options : {
                extension : 'tao',
                bundles : [{
                    //bundles sources from amd.vendor
                    name   : 'vendor',
                    vendor : true
                }, {
                    //bundles sources from controller/login with amd.bootstrap
                    name      : 'login',
                    bootstrap : true,
                    default   : false,
                    entryPoint: 'controller/login'
                }, {
                    //bundles sources from amd/default, amd.boostrap and the includes
                    name      : 'tao',
                    bootstrap : true,
                    default   : true,
                    include   : ['layout/**/*', 'form/**/*', 'lock', 'report', 'users']
                }]
            }
        }
    }
});

The configuration from the taoQtiItem extension :

grunt.config.merge({
    bundle : {
        taoqtiitem : {
            options : {
                extension : 'taoQtiItem',
                dependencies : ['taoItems']

                //the source code of this extension relies on custom alias, so we can add them
                paths : {
                    'qtiCustomInteractionContext' : `${root}/taoQtiItem/views/js/runtime/qtiCustomInteractionContext`,
                    'qtiInfoControlContext' : `${root}taoQtiItem/views/js/runtime/qtiInfoControlContext`
                },

                bundles : [{
                    name : 'taoQtiItem',
                    default : true,

                    //we need to list the dependencies manually, since the
                    //sources contains tests in subfoldesr
                    include : [
                        'taoQtiItem/mathRenderer/mathRenderer',
                        'taoQtiItem/portableElementRegistry/**/*',
                        'taoQtiItem/qtiCommonRenderer/helpers/**/*',
                        'taoQtiItem/qtiCommonRenderer/renderers/**/*',
                        'taoQtiItem/qtiCreator/**/*',
                        'taoQtiItem/qtiItem/**/*',
                        'taoQtiItem/qtiRunner/**/*',
                        'taoQtiItem/qtiXmlRenderer/**/*',
                        'qtiCustomInteractionContext',
                        'qtiInfoControlContext'
                    ]
                }]
            }
        }
    }
});

Test

npm test

Release History

  • 0.8.0 Add options for standalone bundles and specific Babel targets
  • 0.7.0 Update browserslist version (remove IE11 support)
  • 0.6.1 Enable babel updates
  • 0.6.0 Exclude nested modules from bundle
  • 0.5.0 Extend optimizer with packages option
  • 0.4.1 Fix paths for the embedded source-maps
  • 0.4.0 Add Jenkins file
  • 0.2.0 Babel implementation
  • 0.1.0 Initial release

License

See GPL-2.0

1.0.0

12 months ago

0.8.1

1 year ago

0.8.0

1 year ago

0.7.0

2 years ago

0.6.1

2 years ago

0.6.0

4 years ago

0.5.0

4 years ago

0.4.2

4 years ago

0.4.1

4 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.0

5 years ago

0.1.0

6 years ago