plugins.test.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. "use strict";
  2. var __assign = (this && this.__assign) || function () {
  3. __assign = Object.assign || function(t) {
  4. for (var s, i = 1, n = arguments.length; i < n; i++) {
  5. s = arguments[i];
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  7. t[p] = s[p];
  8. }
  9. return t;
  10. };
  11. return __assign.apply(this, arguments);
  12. };
  13. Object.defineProperty(exports, "__esModule", { value: true });
  14. var webpack = require("webpack");
  15. var path = require("path");
  16. var plugin_1 = require("../plugin");
  17. describe("TsconfigPathsPlugin", function () {
  18. var SETTINGS = {
  19. mode: "development",
  20. context: path.resolve(__dirname, "src"),
  21. entry: __dirname + "/../../examples/example/src/index.ts",
  22. output: {
  23. path: path.join(__dirname, "../../temp"),
  24. filename: "bundle.js",
  25. },
  26. module: {
  27. rules: [
  28. {
  29. test: /\\.tsx?$/,
  30. exclude: /^node_modules/,
  31. loader: "ts-loader",
  32. options: {
  33. configFile: "./example/tsconfig.json",
  34. },
  35. },
  36. ],
  37. },
  38. resolve: {
  39. extensions: [".ts", ".tsx", ".js"],
  40. },
  41. };
  42. it("Can initialize the plugin", function (done) {
  43. var testPlugin = new plugin_1.TsconfigPathsPlugin({
  44. configFile: __dirname + "/../../examples/example/tsconfig.json",
  45. logLevel: "INFO",
  46. extensions: [".ts", ".tsx"],
  47. mainFields: ["browser", "main"],
  48. });
  49. expect(testPlugin).toBeInstanceOf(plugin_1.TsconfigPathsPlugin);
  50. var testSettings = __assign(__assign({}, SETTINGS), { resolve: {
  51. extensions: [".ts", ".tsx", ".js"],
  52. plugins: [testPlugin],
  53. } });
  54. var compiler = webpack(testSettings);
  55. compiler.run(function (err, stats) {
  56. if (err) {
  57. done(err);
  58. return;
  59. }
  60. expect(stats).toBeDefined();
  61. var details = stats === null || stats === void 0 ? void 0 : stats.toJson();
  62. expect(details === null || details === void 0 ? void 0 : details.errorsCount).toEqual(0);
  63. // TODO There should probably be a test that verifies the stats match what is expected
  64. done();
  65. });
  66. });
  67. it("Test to ensure Apply exists and is working", function (done) {
  68. var _a, _b, _c;
  69. var webpackSettings = {
  70. entry: __dirname + "/../../examples/example/src/index.ts",
  71. target: "web",
  72. output: {
  73. path: path.join(__dirname, "../../temp"),
  74. filename: "[name].js",
  75. },
  76. mode: "development",
  77. resolve: {
  78. extensions: [
  79. ".ts",
  80. ".tsx",
  81. ".js",
  82. ".jsx",
  83. "ttf",
  84. "eot",
  85. "otf",
  86. "svg",
  87. "png",
  88. "woff",
  89. "woff2",
  90. ],
  91. plugins: [
  92. new plugin_1.TsconfigPathsPlugin({
  93. configFile: __dirname + "/../../examples/example/tsconfig.json",
  94. }),
  95. ],
  96. },
  97. module: {
  98. rules: [],
  99. },
  100. };
  101. // Build compiler
  102. var compiler = webpack(webpackSettings);
  103. var pluginInstance = (_c = (_b = (_a = compiler === null || compiler === void 0 ? void 0 : compiler.options) === null || _a === void 0 ? void 0 : _a.resolve) === null || _b === void 0 ? void 0 : _b.plugins) === null || _c === void 0 ? void 0 : _c.find(function (plugin) { return plugin instanceof plugin_1.TsconfigPathsPlugin; });
  104. if (!pluginInstance) {
  105. return done("TsconfigPathsPlugin not loaded in webpack settings");
  106. }
  107. expect(pluginInstance instanceof plugin_1.TsconfigPathsPlugin).toBeTruthy();
  108. expect(pluginInstance.apply).toBeDefined();
  109. // Run compiler
  110. compiler.run(function (err, stats) {
  111. if (err) {
  112. done(err);
  113. return;
  114. }
  115. expect(stats).toBeDefined();
  116. var details = stats === null || stats === void 0 ? void 0 : stats.toJson();
  117. expect(details === null || details === void 0 ? void 0 : details.errorsCount).toEqual(0);
  118. done();
  119. });
  120. });
  121. it("Resolves project references", function (done) {
  122. var testPlugin = new plugin_1.TsconfigPathsPlugin({
  123. configFile: __dirname + "/../../examples/referenceExample/tsconfig.json",
  124. logLevel: "INFO",
  125. extensions: [".ts", ".tsx"],
  126. mainFields: ["browser", "main"],
  127. references: [__dirname + "/../../examples/example/tsconfig.json"],
  128. });
  129. expect(testPlugin).toBeInstanceOf(plugin_1.TsconfigPathsPlugin);
  130. var testSettings = __assign(__assign({}, SETTINGS), { resolve: {
  131. extensions: [".ts", ".tsx", ".js"],
  132. plugins: [testPlugin],
  133. } });
  134. var compiler = webpack(testSettings);
  135. compiler.run(function (err, stats) {
  136. if (err) {
  137. done(err);
  138. return;
  139. }
  140. expect(stats).toBeDefined();
  141. var details = stats === null || stats === void 0 ? void 0 : stats.toJson();
  142. expect(details === null || details === void 0 ? void 0 : details.errorsCount).toEqual(0);
  143. // TODO There should probably be a test that verifies the stats match what is expected
  144. done();
  145. });
  146. });
  147. });
  148. //# sourceMappingURL=plugins.test.js.map