bundle.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import webpack = require('webpack');
  2. import { getWebpackConfig } from '../../../config/webpack.plugin.config';
  3. import formatWebpackMessages = require('react-dev-utils/formatWebpackMessages');
  4. import clearConsole = require('react-dev-utils/clearConsole');
  5. export interface PluginBundleOptions {
  6. watch: boolean;
  7. production?: boolean;
  8. yarnlink?: boolean;
  9. }
  10. // export const bundlePlugin = useSpinner<PluginBundleOptions>('Bundle plugin', ({ watch }) => {
  11. export const bundlePlugin = async ({ watch, production }: PluginBundleOptions) => {
  12. const compiler = webpack(
  13. getWebpackConfig({
  14. watch,
  15. production,
  16. })
  17. );
  18. const webpackPromise = new Promise<void>((resolve, reject) => {
  19. if (watch) {
  20. console.log('Started watching plugin for changes...');
  21. compiler.watch({}, (err, stats) => {});
  22. compiler.hooks.invalid.tap('invalid', () => {
  23. clearConsole();
  24. console.log('Compiling...');
  25. });
  26. compiler.hooks.done.tap('done', stats => {
  27. clearConsole();
  28. const output = formatWebpackMessages(stats.toJson());
  29. if (!output.errors.length && !output.warnings.length) {
  30. console.log('Compiled successfully!\n');
  31. console.log(stats.toString({ colors: true }));
  32. }
  33. if (output.errors.length) {
  34. console.log('Compilation failed!');
  35. output.errors.forEach(e => console.log(e));
  36. if (output.warnings.length) {
  37. console.log('Warnings:');
  38. output.warnings.forEach(w => console.log(w));
  39. }
  40. }
  41. if (output.errors.length === 0 && output.warnings.length) {
  42. console.log('Compiled with warnings!');
  43. output.warnings.forEach(w => console.log(w));
  44. }
  45. });
  46. } else {
  47. compiler.run((err: Error, stats: webpack.Stats) => {
  48. if (err) {
  49. reject(err.message);
  50. }
  51. if (stats.hasErrors()) {
  52. stats.compilation.errors.forEach(e => {
  53. console.log(e.message);
  54. });
  55. reject('Build failed');
  56. }
  57. console.log('\n', stats.toString({ colors: true }), '\n');
  58. resolve();
  59. });
  60. }
  61. });
  62. return webpackPromise;
  63. };