szymski

Untitled

Apr 12th, 2022
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.04 KB | None | 0 0
  1. const path = require('path');
  2. const fs = require('fs');
  3. const junk = require('junk');
  4.  
  5. const DirectoryNamedWebpackPlugin = require('directory-named-webpack-plugin');
  6. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  7. const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
  8. const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
  9. const EmitExternalsPlugin = require('@trans/webpack-externals-plugin');
  10. const FrameConfigWebpackPlugin = require('@trans/frame-config-webpack-plugin');
  11. const TranslationsPlugin = require('@trans/webpack-translations-plugin');
  12. const startPlatform = require('@trans/start-platform');
  13.  
  14. const getLocalIdent = require('./getLocalIdent');
  15.  
  16. const SRC_FOLDER = 'src';
  17. const MODE_PRODUCTION = 'production';
  18.  
  19. module.exports = (
  20. { platform, mockTranslations } = {},
  21. { mode = MODE_PRODUCTION } = {},
  22. {
  23. '@trans/frame-config-webpack-plugin': frameConfigOptions,
  24. '@trans/start-platform': startPlatformOptions,
  25. '@trans/webpack-externals-plugin': externalsOptions,
  26. '@trans/webpack-translations-plugin': translationsOptions,
  27. } = {}
  28. ) => {
  29. const cwd = __dirname.split(`${path.sep}node_modules${path.sep}`)[0];
  30. const { name, version } = require(path.join(cwd, 'package.json'));
  31.  
  32. const absolute = src => path.resolve(cwd, src);
  33. const srcPath = absolute(SRC_FOLDER);
  34. const production = mode === MODE_PRODUCTION;
  35.  
  36. const extensions = ['.wasm', '.js', '.jsx', '.mjs', '.ts', '.tsx', '.json'];
  37.  
  38. const entry = ['module', 'service', 'layout'].reduce((memo, type) => {
  39. const dirPath = path.join(SRC_FOLDER, type);
  40. if (fs.existsSync(dirPath)) {
  41. fs.readdirSync(dirPath)
  42. .filter(junk.not)
  43. .forEach((module) => {
  44. const file = ['index', module]
  45. .flatMap(fileName => extensions.map(extension => path.join(dirPath, module, `${fileName}${extension}`)))
  46. .find(fs.existsSync);
  47.  
  48. memo[[module, type].join('.')] = `./${path.relative(SRC_FOLDER, file)}`;
  49. });
  50. }
  51. return memo;
  52. }, {});
  53.  
  54. return Object.entries(entry).map(([entryName, entryPath], index) => ({
  55. mode,
  56. context: srcPath,
  57. devtool: false,
  58. entry: {
  59. [entryName]: entryPath,
  60. },
  61. output: {
  62. path: absolute('dist'),
  63. publicPath: `/modules/${name}/${version}/`,
  64. libraryTarget: 'umd',
  65. filename: '[name].js',
  66. chunkFilename: `${entryName}_[name].js`,
  67. library: `${name}/${entryName}`,
  68. },
  69. optimization: {
  70. minimizer: [
  71. '...',
  72. new CssMinimizerPlugin(),
  73. ],
  74. },
  75. module: {
  76. rules: [
  77. {
  78. test: /\.jsx?$/,
  79. include: [srcPath],
  80. loader: 'babel-loader',
  81. },
  82. {
  83. test: /\.tsx?$/,
  84. include: [srcPath],
  85. loader: 'babel-loader',
  86. },
  87. {
  88. test: /\.json$/,
  89. include: [srcPath],
  90. resourceQuery: /labels/, // foo.json?labels
  91. loader: TranslationsPlugin.loader,
  92. },
  93. {
  94. test: /\.scss$/,
  95. include: [srcPath],
  96. use: [
  97. MiniCssExtractPlugin.loader,
  98. {
  99. loader: 'css-loader',
  100. options: {
  101. importLoaders: 2,
  102. modules: {
  103. getLocalIdent: getLocalIdent(name, version, entryName),
  104. },
  105. },
  106. },
  107. 'postcss-loader',
  108. {
  109. loader: 'sass-loader',
  110. options: {
  111. sassOptions: {
  112. precision: 8,
  113. },
  114. },
  115. },
  116. ],
  117. },
  118. {
  119. test: /\.svg$/,
  120. include: [srcPath],
  121. issuer: /\.m?jsx?$/,
  122. oneOf: [
  123. {
  124. resourceQuery: /jsx/, // foo.svg?jsx
  125. use: ['babel-loader', 'svg-react-loader']
  126. },
  127. {
  128. use: ['file-loader'],
  129. }
  130. ],
  131. },
  132. {
  133. test: /\.(jpe?g|a?png|gif|svg)$/,
  134. issuer: /\.s?css$/,
  135. include: [srcPath],
  136. use: [
  137. {
  138. loader: 'file-loader',
  139. options: {
  140. publicPath: url => url,
  141. },
  142. },
  143. ],
  144. },
  145. {
  146. test: /\.(jpe?g|a?png|gif)$/,
  147. include: [srcPath],
  148. issuer: /\.m?jsx?$/,
  149. use: ['file-loader'],
  150. },
  151. {
  152. test: /\.mp3$/,
  153. include: [srcPath],
  154. use: ['file-loader'],
  155. },
  156. ],
  157. },
  158. resolve: {
  159. extensions,
  160. plugins: [new DirectoryNamedWebpackPlugin(true)],
  161. alias: {
  162. '@': path.resolve(srcPath),
  163. },
  164. },
  165. plugins: [
  166. production && new BundleAnalyzerPlugin({
  167. analyzerMode: 'disabled',
  168. generateStatsFile: true,
  169. statsFilename: `../deploy/${entryName}.stats.json`,
  170. }),
  171. new MiniCssExtractPlugin({
  172. filename: '[name].css',
  173. chunkFilename: `${entryName}_[name].css`,
  174. }),
  175. new EmitExternalsPlugin({
  176. ...externalsOptions,
  177. exclude: [
  178. ...(externalsOptions?.exclude ?? []),
  179. ...production ? [] : ['prop-types'],
  180. ],
  181. }),
  182. new FrameConfigWebpackPlugin(frameConfigOptions),
  183. !index && new TranslationsPlugin(translationsOptions)
  184. ].filter(Boolean),
  185. devServer: index
  186. ? undefined // devServer can be started only for one (first) entry
  187. : startPlatform({
  188. ...startPlatformOptions,
  189. platform: platform ?? startPlatformOptions?.platform,
  190. mockTranslations: mockTranslations ?? startPlatformOptions?.mockTranslations,
  191. }),
  192. node: false,
  193. }));
  194. };
  195.  
Advertisement
Add Comment
Please, Sign In to add comment