builder.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import remarkGfm from "remark-gfm";
  2. import remarkTwemoji from "remark-twemoji";
  3. import remarkCodeSandbox from "remark-codesandbox";
  4. import remarkHint from "remark-hint";
  5. import remarkHtml from "remark-html";
  6. import remarkHighlight from "./plugins/remark-highlight.js";
  7. import { defaultSchema } from "hast-util-sanitize";
  8. import merge from "deepmerge";
  9. // Import the highlighter
  10. import prism from "prismjs";
  11. import "prismjs/components/prism-pug.js";
  12. import "prismjs/components/prism-javascript.js";
  13. import "prismjs/components/prism-sass.js";
  14. import "prismjs/components/prism-typescript.js";
  15. import "prismjs/components/prism-bash.js";
  16. import webpack from "webpack";
  17. // Preserve className attributes when sanitizing the HTML
  18. // This is necessary for syntax highlighting
  19. const schema = merge(defaultSchema, {
  20. attributes: {
  21. span: ["className"],
  22. code: ["className"]
  23. }
  24. });
  25. /**
  26. * @see hints: https://github.com/sergioramos/remark-hint
  27. */
  28. const config = {
  29. entry: process.cwd() + "/src/index.js",
  30. output: {
  31. path: process.cwd() + "/dist",
  32. filename: "index.js",
  33. publicPath: "./"
  34. },
  35. cache: false,
  36. mode: "development",
  37. devtool: "source-map",
  38. module: {
  39. rules: [
  40. {
  41. test: /\.pupper$/,
  42. use: ["@pupperjs/webpack-loader"]
  43. },
  44. {
  45. test: /\.js$/,
  46. enforce: "pre",
  47. use: ["source-map-loader"],
  48. },
  49. {
  50. test: /\.md$/,
  51. use: [
  52. {
  53. loader: "html-loader",
  54. },
  55. {
  56. loader: "remark-loader",
  57. options: {
  58. remarkOptions: {
  59. plugins: [
  60. remarkTwemoji,
  61. remarkGfm,
  62. remarkCodeSandbox,
  63. remarkHint,
  64. [remarkHtml, {
  65. sanitize: false
  66. }],
  67. [remarkHighlight, {
  68. highlight: (code, language) => {
  69. const grammar = prism.languages[language];
  70. if (grammar) {
  71. code = prism.highlight(code, grammar);
  72. }
  73. return code;
  74. }
  75. }]
  76. ],
  77. },
  78. },
  79. },
  80. ],
  81. },
  82. ]
  83. }
  84. };
  85. const compiler = webpack(config);
  86. compiler.watch({
  87. aggregateTimeout: 300,
  88. poll: undefined
  89. }, (err, stats) => {
  90. if (err) {
  91. console.error(err.stack || err);
  92. if (err.details) {
  93. console.error(err.details);
  94. }
  95. return;
  96. }
  97. console.log(stats.toString({
  98. colors: true
  99. }));
  100. });