Here's the method I ended up using. This process will create a file that can be linked with a <script src="myModuleName.min.js"></script>
tag on an HTML page for use in a browser.
- Install rollup and plugins (many plugins listed here are optional but this config should cover any way in which you use modules)
npm install rollup-plugin-buble rollup-plugin-commonjs rollup-plugin-node-resolve rollup-plugin-typescript rollup-plugin-uglify typescript --save-dev
- This example rollup configuration file shows how to prevent rollup from also bundling your dependencies. In this case, I'm using jQuery and Angular, but I don't want to include that in the package I'm providing to users--so I list them as external, and they both happen to have a global variable--this example file shows how to deal with that scenario:
rollup.config.js
'use strict';
import 'rollup';
import typescript from 'rollup-plugin-typescript';
import buble from 'rollup-plugin-buble';
import commonjs from 'rollup-plugin-commonjs';
import nodeResolve from 'rollup-plugin-node-resolve';
import uglify from 'rollup-plugin-uglify';
import {minify} from 'uglify-js';
/**
* Default/development Build
*/
const config = {
entry: 'src/index.ts',
exports: 'auto',
globals: {
'jquery': '$',
'angular': 'angular'
},
external: ['angular', 'jquery'],
targets: [{dest: 'dist/myModuleName.js', format: 'umd', moduleName: 'myModuleName', sourceMap: true}],
plugins: [
typescript({
typescript: require('typescript')
}),
buble(),
nodeResolve({
jsnext: true,
main: true
}),
commonjs({
namedExports: {
'node_modules/jquery/dist/jquery.min.js': ['jquery'],
'node_modules/angular/angular.min.js': ['angular']
}
})
]
}
// Minified JS Build
if (process.env.BUILD === 'minify') {
config.targets = [{dest: 'dist/myModuleName.min.js', format: 'umd', moduleName: 'myModuleName', sourceMap: false}];
config.plugins.push(
uglify({}, minify)
);
}
// Report destination paths on console
console.info(`\u001b[36m\[Rollup ${process.env.BUILD} build\]\u001b[97m \nConverting Typescript from ${
config.entry} to javascript, exporting to: ${config.targets[0].dest}`);
export default config
- Add scripts to package.json
"scripts": {
"build": "rollup -c rollup.config.js --no-conflict --environment BUILD:development",
"minify": "rollup -c rollup.config.js --environment INCLUDE_DEPS,BUILD:minify"
}
- Provide a file for rollup to consume:
src/index.ts
`export * from './myModule';`
- Optionally use a file to collect your modules for export if you're writing a library, these are the functions you intend to be publically available.
src/myModule.ts
export {myOtherClass, myFunction} from './myUtils/myFile';
export * from "./myUtils/myOtherFile";
- run
npm run build
or npm run build && npm run minify
to also get the minified version.
"module": "amd"
, then you will need AMD loader instead of SystemJS. – Stinger