main.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // This file has been automatically migrated to valid ESM format by Storybook.
  2. import { fileURLToPath } from 'node:url'
  3. import { createRequire } from 'node:module'
  4. import { defineMain } from '@storybook/react-webpack5/node'
  5. import path, { dirname } from 'node:path'
  6. import MiniCssExtractPlugin from 'mini-css-extract-plugin'
  7. const __filename = fileURLToPath(import.meta.url)
  8. const __dirname = dirname(__filename)
  9. const require = createRequire(import.meta.url)
  10. const rootDir = path.resolve(__dirname, '..')
  11. // NOTE: must be set before webpack config is imported
  12. process.env.OVERLEAF_CONFIG = path.join(rootDir, 'config/settings.webpack.js')
  13. function getAbsolutePath(value: string): any {
  14. return path.dirname(require.resolve(path.join(value, 'package.json')))
  15. }
  16. // Make sure that babel-macros are re-evaluated after changing the modules config
  17. // Import this after setting process.env.OVERLEAF_CONFIG
  18. const invalidateBabelCacheIfNeeded = require('../frontend/macros/invalidate-babel-cache-if-needed')
  19. invalidateBabelCacheIfNeeded()
  20. export default defineMain({
  21. core: {
  22. disableTelemetry: true,
  23. },
  24. staticDirs: [path.join(rootDir, 'public')],
  25. stories: [
  26. path.join(rootDir, 'frontend/stories/**/*.stories.{js,jsx,ts,tsx}'),
  27. path.join(rootDir, 'modules/**/stories/**/*.stories.{js,jsx,ts,tsx}'),
  28. path.join(rootDir, 'frontend/stories/**/*.mdx'),
  29. path.join(rootDir, 'modules/**/stories/**/*.mdx'),
  30. ],
  31. addons: [
  32. getAbsolutePath('@storybook/addon-links'),
  33. getAbsolutePath('@storybook/addon-a11y'),
  34. getAbsolutePath('@storybook/addon-designs'),
  35. getAbsolutePath('@storybook/addon-webpack5-compiler-babel'),
  36. {
  37. name: getAbsolutePath('@storybook/addon-styling-webpack'),
  38. options: {
  39. rules: [
  40. {
  41. test: /\.css$/,
  42. use: [
  43. { loader: MiniCssExtractPlugin.loader },
  44. { loader: 'css-loader' },
  45. ],
  46. },
  47. {
  48. // Pass Sass files through sass-loader/css-loader/mini-css-extract-
  49. // plugin (note: run in reverse order)
  50. test: /\.s[ac]ss$/,
  51. use: [
  52. // Allows the CSS to be extracted to a separate .css file
  53. { loader: MiniCssExtractPlugin.loader },
  54. // Resolves any CSS dependencies (e.g. url())
  55. { loader: 'css-loader' },
  56. // Resolve relative paths sensibly in SASS
  57. { loader: 'resolve-url-loader' },
  58. {
  59. // Runs autoprefixer on CSS via postcss
  60. loader: 'postcss-loader',
  61. options: {
  62. postcssOptions: {
  63. plugins: ['autoprefixer'],
  64. },
  65. },
  66. },
  67. // Compiles Sass to CSS
  68. {
  69. loader: 'sass-loader',
  70. options: { sourceMap: true }, // sourceMap: true is required for resolve-url-loader
  71. },
  72. ],
  73. },
  74. ],
  75. plugins: [new MiniCssExtractPlugin()],
  76. },
  77. },
  78. getAbsolutePath('@storybook/addon-docs'),
  79. ],
  80. framework: {
  81. name: getAbsolutePath('@storybook/react-webpack5'),
  82. options: {},
  83. },
  84. babel: (options: Record<string, any>) => {
  85. return {
  86. ...options,
  87. plugins: [
  88. ['@babel/plugin-proposal-decorators', { legacy: true }],
  89. // ensure that TSX files are transformed before other plugins run
  90. ['@babel/plugin-transform-typescript', { isTSX: true }],
  91. ...(options.plugins ?? []),
  92. ],
  93. }
  94. },
  95. webpackFinal: storybookConfig => {
  96. return {
  97. ...storybookConfig,
  98. resolve: {
  99. ...storybookConfig.resolve,
  100. fallback: {
  101. ...storybookConfig.resolve?.fallback,
  102. fs: false,
  103. os: false,
  104. module: false,
  105. tty: require.resolve('tty-browserify'),
  106. },
  107. extensions: ['.js', '.jsx', '.mjs', '.ts', '.tsx', '.json'],
  108. alias: {
  109. ...storybookConfig.resolve?.alias,
  110. // custom prefixes for import paths
  111. '@': path.join(rootDir, 'frontend/js/'),
  112. '@ol-types': path.join(rootDir, 'types/'),
  113. '@ol-storybook': path.join(rootDir, '.storybook/'),
  114. '@wf': path.join(
  115. rootDir,
  116. 'modules/writefull/frontend/js/integration/src/'
  117. ),
  118. },
  119. },
  120. module: {
  121. ...storybookConfig.module,
  122. rules: (storybookConfig.module?.rules ?? []).concat(
  123. {
  124. test: /\.wasm$/,
  125. type: 'asset/resource',
  126. generator: {
  127. filename: 'js/[name]-[contenthash][ext]',
  128. },
  129. },
  130. {
  131. // Disable webpack's `new URL()` processing for pdfjs-dist worker
  132. // files, so that webpack does not try to resolve qcms_bg.wasm and
  133. // openjpeg.wasm (which live in pdfjs-dist/wasm/, not build/).
  134. // The main webpack build relies on a `/* webpackIgnore: true */`
  135. // comment (via yarn patch) to achieve the same effect, but
  136. // storybook's babel pipeline may strip comments from the 1.9 MB
  137. // worker file (babel `compact: "auto"` removes comments for files
  138. // > 500 KB), so we disable URL parsing for these files instead.
  139. test: /pdfjs-dist.*pdf\.worker.*\.m?js$/,
  140. parser: { javascript: { url: false } },
  141. }
  142. ),
  143. },
  144. }
  145. },
  146. })