browserify.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. * Bundle the application.
  3. */
  4. // Parse argument
  5. const requiredTargets = 'Must be one of "app", "tests:unit" or "tests:ui".';
  6. const target = process.argv[2];
  7. if (target === undefined) {
  8. console.error('Missing target. ' + requiredTargets);
  9. process.exit(1);
  10. }
  11. // Import libs
  12. const fs = require('fs');
  13. const browserify = require('browserify');
  14. const babelifyConfig = require('./babelify-config.js');
  15. // Choose target entry point
  16. let b;
  17. switch (target) {
  18. case 'app':
  19. b = browserify('./src/app.ts');
  20. break;
  21. case 'test:unit':
  22. b = browserify('./tests/ts/main.ts');
  23. break;
  24. case 'test:ui':
  25. b = browserify('./tests/ui/main.ts');
  26. break;
  27. default:
  28. console.error(`Invalid target: ${target}. ${requiredTargets}`);
  29. process.exit(2);
  30. return;
  31. }
  32. // Convert TypeScript
  33. b.plugin('tsify');
  34. // Babelify
  35. b.transform('babelify', babelifyConfig);
  36. // Add header comment
  37. if (target === 'app') {
  38. b.plugin('browserify-header', {
  39. 'file': 'header.js',
  40. })
  41. }
  42. // Generate bundle
  43. switch (target) {
  44. case 'app':
  45. b.bundle().pipe(fs.createWriteStream('dist/app.js'));
  46. break;
  47. case 'test:unit':
  48. b.bundle().pipe(fs.createWriteStream('dist/ts-tests.js'));
  49. break;
  50. case 'test:ui':
  51. b.bundle().pipe(fs.createWriteStream('dist/ui-tests.js'));
  52. break;
  53. }