@oriflame/config-webpack
Version:
Reusable Webpack configs.
267 lines (235 loc) • 7.68 kB
JavaScript
;
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
Object.defineProperty(exports, '__esModule', {
value: true
});
const module$1 = require('@boost/module');
const lumosCommon = require('@oriflame/lumos-common');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const fs = require('fs');
const path = require('path');
const TerserPlugin = require('terser-webpack-plugin');
const webpackMerge = require('webpack-merge');
const constants = require('./constants2.js');
const helpers = require('./helpers2.js');
const _interopDefault = e => e && e.__esModule ? e : {
default: e
};
const CssMinimizerPlugin__default = /*#__PURE__*/_interopDefault(CssMinimizerPlugin);
const fs__default = /*#__PURE__*/_interopDefault(fs);
const path__default = /*#__PURE__*/_interopDefault(path);
const TerserPlugin__default = /*#__PURE__*/_interopDefault(TerserPlugin);
const customConfigPathTs = path__default.default.join(process.cwd(), '.config', 'lumos', 'webpack.ts');
const customConfigPathJs = path__default.default.join(process.cwd(), '.config', 'lumos', 'webpack.js');
function getConfig({
analyzeBundle = false,
buildFolder = 'build',
port = helpers.PORT,
react = false,
sourceMaps = true,
parallel = true,
root = helpers.WEBPACK_ROOT,
publicPath = 'auto',
srcFolder,
entryPoint,
host,
devServerContentBase = 'public',
moduleFederationConfig,
enableSharedModules = false,
sharedModulesManifestPath = constants.DEFAULT_MANIFEST_PATH,
sharedModulesPackage = constants.DEFAULT_MANIFEST_PACKAGE,
monorepoRoot
}) {
const srcPath = path__default.default.join(root, srcFolder);
const internalPath = path__default.default.join(root, buildFolder);
const contentBase = path__default.default.join(root, devServerContentBase);
const entry = {
index: [srcPath]
};
const POSTCSS_SETTING = _objectSpread({}, constants.POSTCSS_SETTING);
if (monorepoRoot) {
try {
process.chdir(monorepoRoot);
} catch (error) {
console.log(error);
}
}
POSTCSS_SETTING.options.sourceMap = sourceMaps;
const output = {
path: internalPath,
publicPath,
filename: '[name].js',
chunkFilename: helpers.PROD ? '[id].chunk.js' : '[id].js',
sourceMapFilename: '[file].map',
uniqueName: helpers.PROD ? helpers.getUniqueName() : undefined
};
const plugins = helpers.getPlugins({
analyzeBundle,
buildFolder,
port,
react,
root,
sourceMaps,
entryPoint,
srcFolder,
moduleFederationConfig,
enableSharedModules,
sharedModulesManifestPath,
sharedModulesPackage
});
if (entryPoint) {
entry.index = path__default.default.join(srcPath, entryPoint);
}
if (entryPoint && helpers.PROD) {
output.filename = 'index.js';
}
const baseConfig = {
mode: helpers.PROD ? 'production' : 'development',
bail: helpers.PROD,
entry,
context: root,
plugins,
stats: 'errors-warnings',
module: {
rules: [{
test: lumosCommon.TJSX_EXT_PATTERN,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
cacheDirectory: true,
configFile: true
}
}
}, {
test: lumosCommon.CSS_MODULE_EXT_PATTERN,
use: ['style-loader', {
loader: 'css-loader',
options: {
sourceMap: sourceMaps,
importLoaders: 1,
modules: {
localIdentName: '[local]_[hash:base64:5]'
}
}
}, POSTCSS_SETTING],
sideEffects: true
}, {
test: lumosCommon.CSS_EXT_PATTERN,
exclude: lumosCommon.CSS_MODULE_EXT_PATTERN,
use: ['style-loader', {
loader: 'css-loader',
options: {
sourceMap: sourceMaps
}
}, POSTCSS_SETTING],
sideEffects: true
}, {
test: lumosCommon.ASSET_EXT_PATTERN,
use: {
loader: 'url-loader',
options: {
limit: 1000,
name: 'assets/[name].[ext]?[hash:7]',
publicPath,
esModule: false
}
}
}, {
test: lumosCommon.GQL_EXT_PATTERN,
exclude: /node_modules/,
use: {
loader: 'graphql-tag/loader'
}
}]
},
resolve: {
alias: {
[`${lumosCommon.ALIAS_PATTERN}`]: path__default.default.join(root, srcFolder, '/')
},
fallback: {
applicationinsights: false,
assert: false,
async_hooks: false,
buffer: false,
child_process: false,
console: false,
constants: false,
crypto: false,
dns: false,
domain: false,
events: false,
fs: false,
http: false,
https: false,
module: false,
net: false,
os: false,
path: false,
process: false,
punycode: false,
querystring: false,
stream: false,
string_decoder: false,
sys: false,
timers: false,
tty: false,
url: false,
util: false,
vm: false,
zlib: false
},
mainFields: ['browser', 'module', 'main'],
extensions: ['.wasm', '.mjs', ...lumosCommon.EXTS]
},
output,
devtool: helpers.PROD ? sourceMaps ? 'source-map' : false : 'eval-source-map',
// @ts-expect-error -- something wrong
devServer: {
static: {
directory: contentBase,
watch: true
},
allowedHosts: 'all',
client: {
logging: 'info'
},
headers: {
'Service-Worker-Allowed': '/'
},
historyApiFallback: true,
hot: true,
port,
// This can be a unix socket path so a string is valid
host
},
optimization: {
chunkIds: helpers.PROD ? undefined : 'named',
minimize: helpers.PROD,
minimizer: [new TerserPlugin__default.default({
parallel: helpers.getParallelValue(parallel),
terserOptions: {
sourceMap: sourceMaps
}
}), new CssMinimizerPlugin__default.default({
parallel: helpers.getParallelValue(parallel)
})]
},
performance: false
};
let config;
if (fs__default.default.existsSync(customConfigPathTs)) {
config = module$1.requireModule(customConfigPathTs).default;
}
if (fs__default.default.existsSync(customConfigPathJs)) {
config = module$1.requireModule(customConfigPathJs).default;
}
if (config) {
return webpackMerge.merge(baseConfig, config);
}
return baseConfig;
}
exports.getConfig = getConfig;
//# sourceMappingURL=index2.js.map