UNPKG

3.98 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.dllConfig = exports.serverRenderConfig = exports.config = exports.callback = exports.callbackOnlyError = void 0;
4const process = require("process");
5const _ = require("lodash");
6const config_1 = require("./config");
7const server_1 = require("./server");
8const dll_1 = require("./dll");
9const configFile_1 = require("./utils/configFile");
10/* 基础配置 */
11const sweetOptions = {
12 basicPath: process.cwd(),
13 environment: 'client' // 当前环境
14};
15/* 获取配置 */
16function getConfig(environment, sweetConfig) {
17 if (typeof sweetConfig === 'string') {
18 const config = configFile_1.default(sweetOptions, sweetConfig);
19 return typeof config === 'function' ? config({ environment }) : config;
20 }
21 else if (_.isPlainObject(sweetConfig)) {
22 return sweetConfig;
23 }
24 else {
25 const config = configFile_1.default(sweetOptions);
26 return typeof config === 'function' ? config({ environment }) : config;
27 }
28}
29/* webpack的回调函数,只显示错误 */
30function callbackOnlyError(err, stats) {
31 if (err) {
32 console.error(err);
33 }
34 else {
35 console.log(stats.toString({
36 colors: true,
37 assets: false,
38 entrypoints: false,
39 builtAt: false,
40 hash: false,
41 modules: false,
42 version: false,
43 timings: false
44 }));
45 }
46}
47exports.callbackOnlyError = callbackOnlyError;
48/* webpack的回调函数 */
49function callback(err, stats) {
50 if (err) {
51 console.error(err);
52 }
53 else {
54 console.log(stats.toString({
55 colors: true
56 }));
57 }
58}
59exports.callback = callback;
60/**
61 * webpack配置
62 * @param { SweetConfig | string | null | undefined } args.sweetConfig: webpack配置,覆盖文件,优先级最高
63 * @param { string } args.mode: 开发环境,覆盖配置的开发环境
64 * @param { string } args.webpackLog: 覆盖日志的显示
65 * @param { boolean } args.hot: 添加webpack.HotModuleReplacementPlugin插件,开启热更新功能
66 */
67function config(args = {}) {
68 const { sweetConfig, mode, webpackLog, hot } = args;
69 const config = getConfig('client', sweetConfig);
70 if (config) {
71 if (mode) {
72 config.mode = mode;
73 }
74 if (webpackLog) {
75 config.webpackLog = webpackLog;
76 }
77 if (hot) {
78 config.hot = hot;
79 }
80 }
81 return config_1.default(config, sweetOptions);
82}
83exports.config = config;
84/**
85 * 服务器端渲染的webpack配置
86 * @param { SweetConfig | string | null | undefined } args.sweetConfig: webpack配置,覆盖文件,优先级最高
87 * @param { string } args.mode: 开发环境,覆盖配置的开发环境
88 * @param { string } args.webpackLog: 覆盖日志的显示
89 */
90function serverRenderConfig(args = {}) {
91 const { sweetConfig, mode, webpackLog } = args;
92 const config = getConfig('server', sweetConfig);
93 sweetOptions.environment = 'server';
94 if (config) {
95 if (mode) {
96 config.mode = mode;
97 }
98 if (webpackLog) {
99 config.webpackLog = webpackLog;
100 }
101 }
102 return server_1.default(config, sweetOptions);
103}
104exports.serverRenderConfig = serverRenderConfig;
105/**
106 * webpack的dll文件配置
107 * @param { SweetConfig | string | null | undefined } args.sweetConfig: webpack配置,覆盖文件,优先级最高
108 * @param { string } args.webpackLog: 覆盖日志的显示
109 */
110function dllConfig(args = {}) {
111 const { sweetConfig, webpackLog } = args;
112 const config = getConfig('dll', sweetConfig);
113 sweetOptions.environment = 'dll';
114 if (config && webpackLog) {
115 config.webpackLog = webpackLog;
116 }
117 return dll_1.default(config, sweetOptions);
118}
119exports.dllConfig = dllConfig;
120exports.default = {
121 callbackOnlyError,
122 callback,
123 config,
124 serverRenderConfig,
125 dllConfig
126};