Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const path = require('path');
- const fs = require('fs');
- const junk = require('junk');
- const DirectoryNamedWebpackPlugin = require('directory-named-webpack-plugin');
- const MiniCssExtractPlugin = require('mini-css-extract-plugin');
- const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
- const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
- const EmitExternalsPlugin = require('@trans/webpack-externals-plugin');
- const FrameConfigWebpackPlugin = require('@trans/frame-config-webpack-plugin');
- const TranslationsPlugin = require('@trans/webpack-translations-plugin');
- const startPlatform = require('@trans/start-platform');
- const getLocalIdent = require('./getLocalIdent');
- const SRC_FOLDER = 'src';
- const MODE_PRODUCTION = 'production';
- module.exports = (
- { platform, mockTranslations } = {},
- { mode = MODE_PRODUCTION } = {},
- {
- '@trans/frame-config-webpack-plugin': frameConfigOptions,
- '@trans/start-platform': startPlatformOptions,
- '@trans/webpack-externals-plugin': externalsOptions,
- '@trans/webpack-translations-plugin': translationsOptions,
- } = {}
- ) => {
- const cwd = __dirname.split(`${path.sep}node_modules${path.sep}`)[0];
- const { name, version } = require(path.join(cwd, 'package.json'));
- const absolute = src => path.resolve(cwd, src);
- const srcPath = absolute(SRC_FOLDER);
- const production = mode === MODE_PRODUCTION;
- const extensions = ['.wasm', '.js', '.jsx', '.mjs', '.ts', '.tsx', '.json'];
- const entry = ['module', 'service', 'layout'].reduce((memo, type) => {
- const dirPath = path.join(SRC_FOLDER, type);
- if (fs.existsSync(dirPath)) {
- fs.readdirSync(dirPath)
- .filter(junk.not)
- .forEach((module) => {
- const file = ['index', module]
- .flatMap(fileName => extensions.map(extension => path.join(dirPath, module, `${fileName}${extension}`)))
- .find(fs.existsSync);
- memo[[module, type].join('.')] = `./${path.relative(SRC_FOLDER, file)}`;
- });
- }
- return memo;
- }, {});
- return Object.entries(entry).map(([entryName, entryPath], index) => ({
- mode,
- context: srcPath,
- devtool: false,
- entry: {
- [entryName]: entryPath,
- },
- output: {
- path: absolute('dist'),
- publicPath: `/modules/${name}/${version}/`,
- libraryTarget: 'umd',
- filename: '[name].js',
- chunkFilename: `${entryName}_[name].js`,
- library: `${name}/${entryName}`,
- },
- optimization: {
- minimizer: [
- '...',
- new CssMinimizerPlugin(),
- ],
- },
- module: {
- rules: [
- {
- test: /\.jsx?$/,
- include: [srcPath],
- loader: 'babel-loader',
- },
- {
- test: /\.tsx?$/,
- include: [srcPath],
- loader: 'babel-loader',
- },
- {
- test: /\.json$/,
- include: [srcPath],
- resourceQuery: /labels/, // foo.json?labels
- loader: TranslationsPlugin.loader,
- },
- {
- test: /\.scss$/,
- include: [srcPath],
- use: [
- MiniCssExtractPlugin.loader,
- {
- loader: 'css-loader',
- options: {
- importLoaders: 2,
- modules: {
- getLocalIdent: getLocalIdent(name, version, entryName),
- },
- },
- },
- 'postcss-loader',
- {
- loader: 'sass-loader',
- options: {
- sassOptions: {
- precision: 8,
- },
- },
- },
- ],
- },
- {
- test: /\.svg$/,
- include: [srcPath],
- issuer: /\.m?jsx?$/,
- oneOf: [
- {
- resourceQuery: /jsx/, // foo.svg?jsx
- use: ['babel-loader', 'svg-react-loader']
- },
- {
- use: ['file-loader'],
- }
- ],
- },
- {
- test: /\.(jpe?g|a?png|gif|svg)$/,
- issuer: /\.s?css$/,
- include: [srcPath],
- use: [
- {
- loader: 'file-loader',
- options: {
- publicPath: url => url,
- },
- },
- ],
- },
- {
- test: /\.(jpe?g|a?png|gif)$/,
- include: [srcPath],
- issuer: /\.m?jsx?$/,
- use: ['file-loader'],
- },
- {
- test: /\.mp3$/,
- include: [srcPath],
- use: ['file-loader'],
- },
- ],
- },
- resolve: {
- extensions,
- plugins: [new DirectoryNamedWebpackPlugin(true)],
- alias: {
- '@': path.resolve(srcPath),
- },
- },
- plugins: [
- production && new BundleAnalyzerPlugin({
- analyzerMode: 'disabled',
- generateStatsFile: true,
- statsFilename: `../deploy/${entryName}.stats.json`,
- }),
- new MiniCssExtractPlugin({
- filename: '[name].css',
- chunkFilename: `${entryName}_[name].css`,
- }),
- new EmitExternalsPlugin({
- ...externalsOptions,
- exclude: [
- ...(externalsOptions?.exclude ?? []),
- ...production ? [] : ['prop-types'],
- ],
- }),
- new FrameConfigWebpackPlugin(frameConfigOptions),
- !index && new TranslationsPlugin(translationsOptions)
- ].filter(Boolean),
- devServer: index
- ? undefined // devServer can be started only for one (first) entry
- : startPlatform({
- ...startPlatformOptions,
- platform: platform ?? startPlatformOptions?.platform,
- mockTranslations: mockTranslations ?? startPlatformOptions?.mockTranslations,
- }),
- node: false,
- }));
- };
Advertisement
Add Comment
Please, Sign In to add comment