build-js-loader.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* eslint-disable no-console */
  2. import fs from 'fs';
  3. import {minify} from 'terser';
  4. import * as ts from 'typescript';
  5. /**
  6. * This script is used to generate the loader script templates used by Django.
  7. * It takes the base loader script (which has to be valid ES5 JS!)
  8. * and generates both an unminified and minified version of it, with Django template tags.
  9. *
  10. * The generated Django template tags have to be checked in normally, and are what's actually used.
  11. * The base `.ts` file is only the blueprint used to generate the template files off.
  12. *
  13. * Run this script whenever you change the base loader script,
  14. * then verify the build output of both generated `js.tmpl` files and check all three files in.
  15. */
  16. const header = `{% load sentry_helpers %}`;
  17. const loaderScriptPath = './src/sentry/templates/sentry/js-sdk-loader.ts';
  18. const loaderTmplPath = './src/sentry/templates/sentry/js-sdk-loader.js.tmpl';
  19. const loaderMinTmplPath = './src/sentry/templates/sentry/js-sdk-loader.min.js.tmpl';
  20. async function run() {
  21. const baseTs = fs.readFileSync(loaderScriptPath, 'utf-8');
  22. const {outputText: base} = ts.transpileModule(baseTs, {
  23. compilerOptions: {
  24. noEmitOnError: true,
  25. target: ts.ScriptTarget.ES5,
  26. module: ts.ModuleKind.CommonJS,
  27. },
  28. });
  29. if (!base) {
  30. throw new Error('Could not transpile loader script!');
  31. }
  32. const unminifiedLoader = `${header}${replacePlaceholders(base)}`;
  33. const {code: minifiedBase} = await minify(base, {
  34. ecma: 5,
  35. mangle: {
  36. reserved: ['onLoad', 'forceLoad', 'sentryOnLoad'],
  37. },
  38. format: {
  39. ecma: 5,
  40. },
  41. });
  42. if (!minifiedBase) {
  43. throw new Error('Could not minify loader script!');
  44. }
  45. const minifiedLoader = `${header}${replacePlaceholders(minifiedBase)}\n`;
  46. fs.writeFileSync(loaderTmplPath, unminifiedLoader, 'utf-8');
  47. console.log(`Updated loader script template at ${loaderTmplPath}`);
  48. fs.writeFileSync(loaderMinTmplPath, minifiedLoader, 'utf-8');
  49. console.log(`Updated loader min. script template at ${loaderMinTmplPath}`);
  50. }
  51. /**
  52. * We replace some placeholders in the loader script with Django template tags.
  53. * This is done so the base template is actually valid JS (so we can minify it, have autocomplete, linting, etc.).
  54. * The placeholders are replaced with the actual values by Django.
  55. */
  56. function replacePlaceholders(str: string): string {
  57. return str
  58. .replace('__LOADER__PUBLIC_KEY__', "'{{ publicKey|safe }}'")
  59. .replace('__LOADER_SDK_URL__', "'{{ jsSdkUrl|safe }}'")
  60. .replace('__LOADER__CONFIG__', '{{ config|to_json|safe }}')
  61. .replace('__LOADER__IS_LAZY__', '{{ isLazy|safe|lower }}');
  62. }
  63. run();