UNPKG

accelerator-build-engine

Version:
245 lines (203 loc) 8.7 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _path = require('path'); var _path2 = _interopRequireDefault(_path); var _forEach = require('lodash/forEach'); var _forEach2 = _interopRequireDefault(_forEach); var _isEmpty = require('lodash/isEmpty'); var _isEmpty2 = _interopRequireDefault(_isEmpty); var _isArray = require('lodash/isArray'); var _isArray2 = _interopRequireDefault(_isArray); var _isString = require('lodash/isString'); var _isString2 = _interopRequireDefault(_isString); var _isPlainObject = require('lodash/isPlainObject'); var _isPlainObject2 = _interopRequireDefault(_isPlainObject); var _map = require('lodash/map'); var _map2 = _interopRequireDefault(_map); var _webpack = require('webpack'); var _webpack2 = _interopRequireDefault(_webpack); var _htmlWebpackPlugin = require('html-webpack-plugin'); var _htmlWebpackPlugin2 = _interopRequireDefault(_htmlWebpackPlugin); var _extractTextWebpackPlugin = require('extract-text-webpack-plugin'); var _extractTextWebpackPlugin2 = _interopRequireDefault(_extractTextWebpackPlugin); var _webpackMerge = require('webpack-merge'); var _webpackMerge2 = _interopRequireDefault(_webpackMerge); var _constants = require('../constants'); var _common = require('./common'); var _helpers = require('../helpers'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } const getStyleValue = (styleEntry = {}, brand = '') => { if ((0, _isEmpty2['default'])(styleEntry)) { return _constants.DEFAULT_VALUE.STYLESHEET; } if ((0, _isString2['default'])(styleEntry) || (0, _isArray2['default'])(styleEntry)) { return styleEntry; } if ((0, _isPlainObject2['default'])(styleEntry)) { return styleEntry[brand] || _constants.DEFAULT_VALUE.STYLESHEET; } return _constants.DEFAULT_VALUE.STYLESHEET; }; const getEntryValue = (env = _constants.ENV.DEV, ref, brand = '', journey = '') => { if ((0, _isEmpty2['default'])(ref)) { return ''; } const journeyConfig = (0, _common.getJourneyConfig)(env, journey); const entryProps = journeyConfig.entry || {}; if ((0, _isEmpty2['default'])(journeyConfig) || (0, _isEmpty2['default'])(entryProps)) { return ''; } const entryValue = entryProps[ref]; return ref === 'styles' ? getStyleValue(entryValue, brand) : entryValue; }; const getDefinePlugin = (env = _constants.ENV.DEV, journey = '', brand = '') => { const configs = (0, _common.getEnvConfig)(env); const journeyConfig = (0, _common.getJourneyConfig)(env, journey); const journeyBrandPath = (0, _common.getJourneyBrandPath)((0, _common.isJourneyActive)(env), journey, brand); if ((0, _isEmpty2['default'])(journeyConfig)) { return {}; } const currentProperties = configs.brandProperties[brand] || configs.brandProperties['default']; const properties = (0, _common.updateProperties)(currentProperties, { basePath: env === _constants.ENV.DEV ? `${journeyBrandPath}/` : '/' }); return new _webpack2['default'].DefinePlugin({ webpack: JSON.stringify(properties), 'process.env': { BRAND: `"${brand}"`, NODE_ENV: JSON.stringify(process.env.NODE_ENV || env) } }); }; const getScoutTemplateConfig = (indexTemplateConfig = {}, basePath = '') => { const props = _extends({}, indexTemplateConfig.props, { scoutpath: `${_constants.CLI_PATH.RELEASE_PATH}` }); const rootPath = (0, _isEmpty2['default'])(basePath) ? '' : `${basePath}/`; return { props, favicon: indexTemplateConfig.favicon, filename: `${rootPath}${indexTemplateConfig.filename || 'index.html'}`, template: indexTemplateConfig.template }; }; const getTemplateConfig = (env = _constants.ENV.DEV, journey = '', brand = '') => { const journeyConfig = (0, _common.getJourneyConfig)(env, journey); if ((0, _isEmpty2['default'])(journeyConfig)) { return {}; } const configs = (0, _common.getEnvConfig)(env); const journeyBrandPath = (0, _common.getJourneyBrandPath)((0, _common.isJourneyActive)(env), journey, brand); const currentProperties = configs.brandProperties[brand] || configs.brandProperties['default']; const properties = (0, _common.updateProperties)(currentProperties, { basePath: env === _constants.ENV.DEV ? `${journeyBrandPath}/` : '/' }); return { favicon: _path2['default'].resolve(process.cwd(), configs.favIcon), filename: journeyConfig.htmlOutput.fileName, template: _path2['default'].resolve(process.cwd(), journeyConfig.htmlOutput.template), props: properties }; }; const getExtractTextSass = (env = _constants.ENV.DEV, journey = '') => { if ((0, _isEmpty2['default'])(journey) || env === _constants.ENV.DEV) { return []; } const configs = (0, _common.getEnvConfig)(env); const styleOutputPath = (0, _common.getStyleOutputPath)((0, _common.getFolderFilePath)(env, journey))[0]; if ((0, _isEmpty2['default'])(styleOutputPath)) { return []; } const sName = env === _constants.ENV.PROD && !configs.noHash ? `${styleOutputPath.path}.[chunkhash]` : `${styleOutputPath.path}`; return [new _extractTextWebpackPlugin2['default']({ filename: `${sName}.css`, disable: false, allChunks: true })]; }; const getOutput = (env = _constants.ENV.DEV, journey = '', brand = '') => { const configs = (0, _common.getEnvConfig)(env); const fName = env === _constants.ENV.PROD && !configs.noHash ? '[name].[chunkhash]' : '[name]'; const cName = env === _constants.ENV.PROD && !configs.noHash ? '[id].[chunkhash]' : '[id]'; return { filename: `${fName}.js`, chunkFilename: `${cName}.js`, publicPath: configs.publicPath, path: `${(0, _common.getBaseOutputPath)(env, journey, brand)}/${(0, _common.getReleaseVersionPath)(env)}` }; }; const getEntry = (env = _constants.ENV.DEV, journey = '', brand = '') => { const paths = (0, _common.getOutputPath)((0, _common.getFolderFilePath)(env, journey)); const entries = {}; (0, _forEach2['default'])(paths, (prop, idx) => { let value = getEntryValue(env, prop.ref, brand, journey); value = (0, _isString2['default'])(value) ? [value] : value; if (!idx) { value = [...value]; } entries[prop.path] = value; }); return entries; }; const getVersionPlugins = (env = _constants.ENV.DEV, journey = '', brand = '', templateConfig = {}) => { if (env === _constants.ENV.PROD) { const configs = (0, _common.getEnvConfig)(env); const basePath = (0, _common.getBaseOutputPath)(env, journey, brand); const versionFilename = (0, _common.getVersionFilename)(env, basePath); const scoutTemplateConfig = getScoutTemplateConfig(templateConfig, basePath); return [new _htmlWebpackPlugin2['default'](scoutTemplateConfig), new _helpers.plugins.VersionTemplatePlugin({ filePath: versionFilename, noHash: configs.noHash, journey, brand })]; } return []; }; const getWebpackConfigs = (env = _constants.ENV.DEV, journey = '', brand = '') => { if ((0, _isEmpty2['default'])(journey)) { return {}; } const configs = (0, _common.getEnvWebpack)(env); const templateConfig = getTemplateConfig(env, journey, brand); return _webpackMerge2['default'].smart({ entry: getEntry(env, journey, brand), output: getOutput(env, journey, brand), module: { rules: [..._helpers.rules[env]] }, plugins: [..._helpers.plugins[env], ...getExtractTextSass(env, journey), new _htmlWebpackPlugin2['default'](templateConfig), ...getVersionPlugins(env, journey, brand, templateConfig), getDefinePlugin(env, journey, brand)] }, configs); }; const getBrandConfigs = (env = _constants.ENV.DEV, journey = '') => { if ((0, _isEmpty2['default'])(journey)) { return {}; } const brands = (0, _common.getBrands)(env); const journeyConfig = (0, _common.getJourneyConfig)(env, journey); if ((0, _isEmpty2['default'])(journeyConfig)) { return {}; } if (!(0, _isEmpty2['default'])(brands) && !journeyConfig.noBrands) { return (0, _map2['default'])(brands, brand => getWebpackConfigs(env, journey, brand)); } return getWebpackConfigs(env, journey, ''); }; exports['default'] = { getStyleValue, getEntryValue, getDefinePlugin, getTemplateConfig, getExtractTextSass, getOutput, getEntry, getWebpackConfigs, getBrandConfigs, getScoutTemplateConfig, getVersionPlugins }; module.exports = exports['default']; //# sourceMappingURL=branding.js.map