run_tests.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * Copyright 2018 Google Inc. All Rights Reserved.
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. * Unless required by applicable law or agreed to in writing, software
  8. * distributed under the License is distributed on an "AS IS" BASIS,
  9. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. * See the License for the specific language governing permissions and
  11. * limitations under the License.
  12. */
  13. const rollup = require("rollup");
  14. const path = require("path");
  15. const omt = require(".");
  16. const fs = require("fs");
  17. const chalk = require("chalk");
  18. const karma = require("karma");
  19. const myKarmaConfig = require("./karma.conf.js");
  20. async function fileExists(file) {
  21. try {
  22. const stat = await fs.promises.stat(file);
  23. return stat.isFile();
  24. } catch (e) {
  25. return false;
  26. }
  27. }
  28. async function init() {
  29. await Promise.all(
  30. [
  31. "./tests/fixtures/simple-bundle/entry.js",
  32. "./tests/fixtures/import-meta/entry.js",
  33. "./tests/fixtures/dynamic-import/entry.js",
  34. "./tests/fixtures/public-path/entry.js",
  35. "./tests/fixtures/worker/entry.js",
  36. "./tests/fixtures/module-worker/entry.js",
  37. "./tests/fixtures/more-workers/entry.js",
  38. "./tests/fixtures/amd-function-name/entry.js",
  39. "./tests/fixtures/single-default/entry.js",
  40. "./tests/fixtures/import-worker-url/entry.js",
  41. "./tests/fixtures/import-meta-worker/entry.js",
  42. "./tests/fixtures/import-worker-url-custom-scheme/entry.js",
  43. "./tests/fixtures/assets-in-worker/entry.js",
  44. "./tests/fixtures/url-import-meta-worker/entry.js"
  45. ].map(async input => {
  46. const pathName = path.dirname(input);
  47. const outputOptions = {
  48. dir: path.join(pathName, "build"),
  49. format: "amd"
  50. };
  51. let rollupConfig = {
  52. input,
  53. strictDeprecations: true,
  54. // Copied / adapted from default `onwarn` in Rollup CLI.
  55. onwarn: warning => {
  56. console.warn(`⚠️ ${chalk.bold(warning.message)}`);
  57. if (warning.url) {
  58. console.warn(chalk.cyan(warning.url));
  59. }
  60. if (warning.loc) {
  61. console.warn(
  62. `${warning.loc.file} (${warning.loc.line}:${warning.loc.column})`
  63. );
  64. }
  65. if (warning.frame) {
  66. console.warn(chalk.dim(warning.frame));
  67. }
  68. console.warn("");
  69. }
  70. };
  71. const rollupConfigPath = "./" + path.join(pathName, "rollup.config.js");
  72. const configPath = "./" + path.join(pathName, "config.json");
  73. if (await fileExists(rollupConfigPath)) {
  74. require(rollupConfigPath)(rollupConfig, outputOptions, omt);
  75. } else if (await fileExists(configPath)) {
  76. rollupConfig.plugins = [omt(require(configPath))];
  77. } else {
  78. rollupConfig.plugins = [omt()];
  79. }
  80. const bundle = await rollup.rollup(rollupConfig);
  81. await bundle.write(outputOptions);
  82. })
  83. );
  84. const karmaConfig = { port: 9876 };
  85. myKarmaConfig({
  86. set(config) {
  87. Object.assign(karmaConfig, config);
  88. }
  89. });
  90. const server = new karma.Server(karmaConfig, code => {
  91. console.log(`Karma exited with code ${code}`);
  92. process.exit(code);
  93. });
  94. server.start();
  95. }
  96. init();