// webpack.config.js const { CleanWebpackPlugin } = require('clean-webpack-plugin'); const TerserPlugin = require('terser-webpack-plugin'); const HtmlWebpackPlugin = require('html-webpack-plugin'); const MiniCssExtractPlugin = require('mini-css-extract-plugin'); const CopyPlugin = require('copy-webpack-plugin'); const path = require('path'); const argv = require('yargs').argv; const { merge } = require('webpack-merge'); const WebpackBar = require('webpackbar'); const fs = require('node:fs'); const cosmiconfigSync = require('cosmiconfig').cosmiconfigSync; const explorerSync = cosmiconfigSync('patternlab'); // @todo: wire these two ocnfigs up to use cosmicconfig! const defaultConfig = { rootDir: process.cwd(), buildDir: './dist', prod: argv.watch ? false : true, // or false for local dev sourceMaps: true, watch: argv.watch ? true : false, publicPath: './styleguide/', copy: { patterns: [ { from: '../uikit-workshop/src/images/**', to: 'images/[name][ext]' }, ], }, noViewAll: false, }; // Requiring partials // adapted from https://github.com/webpack-contrib/html-loader/issues/291#issuecomment-721909576 const INCLUDE_PATTERN = /\(?:\<\/include\>)?/gi; const processNestedHtml = (content, loaderContext) => !INCLUDE_PATTERN.test(content) ? content : content.replace(INCLUDE_PATTERN, (m, src) => processNestedHtml( fs.readFileSync(path.resolve(loaderContext.context, src), 'utf8'), loaderContext ) ); module.exports = function (apiConfig) { return new Promise(async (resolve) => { let customConfig = defaultConfig; let configToSearchFor; if (argv.patternlabrc) { configToSearchFor = await explorerSync.load(argv.patternlabrc); } else { configToSearchFor = await explorerSync.search(); } if (configToSearchFor) { if (configToSearchFor.config) { customConfig = configToSearchFor.config; } } // Allow external flags for modifying PL's prod mode, on top of the .patternlabrc config file const config = Object.assign({}, defaultConfig, customConfig, apiConfig); function getBabelConfig(isModern = false) { return { presets: [ [ '@babel/preset-env', { targets: { browsers: isModern ? [ // NOTE: I'm not using the `esmodules` target due to this issue: // https://github.com/babel/babel/issues/8809 'last 2 Chrome versions', 'last 2 Safari versions', 'last 2 iOS versions', 'last 2 Edge versions', 'Firefox ESR', ] : ['ie 11'], }, useBuiltIns: 'entry', corejs: 3, modules: false, debug: false, }, ], ], plugins: [ '@babel/plugin-proposal-optional-chaining', ['@babel/plugin-proposal-decorators', { legacy: true }], ['@babel/plugin-proposal-class-properties', { loose: true }], ['@babel/plugin-proposal-private-methods', { loose: true }], [ '@babel/plugin-proposal-private-property-in-object', { loose: true }, ], '@babel/plugin-syntax-dynamic-import', '@babel/plugin-syntax-jsx' /* [1] */, [ '@babel/plugin-transform-react-jsx' /* [1] */, { pragma: 'h', pragmaFrag: 'Fragment', throwIfNamespace: false, useBuiltIns: false, }, ], ], }; } // organize the series of plugins to run our Sass through as an external array -- this is necessary since we need to add additional loaders when compiling Sass to standalone CSS files vs compiling Sass and returning an inline-able