Skip to content

Commit b613863

Browse files
committed
fix(file): 添加之前全局忽略的Config.js
1 parent 3df5352 commit b613863

File tree

1 file changed

+165
-0
lines changed

1 file changed

+165
-0
lines changed

lib/models/Config.js

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
'use strict';
2+
3+
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
4+
5+
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
6+
7+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
8+
9+
var ExtractTextPlugin = require("extract-text-webpack-plugin");
10+
11+
var Config = function () {
12+
function Config(cwd) {
13+
_classCallCheck(this, Config);
14+
15+
this._config = {
16+
cwd: cwd,
17+
context: sysPath.join(cwd, 'src'),
18+
entry: {},
19+
entryGroup: {},
20+
output: {
21+
dev: {
22+
path: "./dev",
23+
filename: "[name][ext]"
24+
},
25+
prd: {
26+
path: "./prd",
27+
filename: "[name].min[ext]"
28+
}
29+
},
30+
module: {
31+
preLoaders: [],
32+
loaders: [{
33+
test: /\.json$/,
34+
exclude: /node_modules/,
35+
loader: require.resolve('json-loader')
36+
}, {
37+
test: /\.(html|string|tpl)$/,
38+
loader: require.resolve('html-loader')
39+
}, {
40+
test: /\.css$/,
41+
loader: ExtractTextPlugin.extract(require.resolve('style-loader'), require.resolve('css-loader'))
42+
}],
43+
postLoaders: []
44+
},
45+
plugins: [require('../plugins/extTemplatedPathPlugin.js'), require('../plugins/requireModulePlugin.js'), require('../plugins/ProgressBarPlugin.js'), require('../plugins/compileInfoPlugin.js')],
46+
resolve: {
47+
root: [],
48+
extensions: ['', '.js', '.css', '.json', '.string', '.tpl'],
49+
alias: {}
50+
},
51+
entryExtNames: {
52+
css: ['.css'],
53+
js: ['.js']
54+
},
55+
requireRules: ['node_modules|package.json:main|index.js'],
56+
devtool: 'cheap-source-map'
57+
};
58+
}
59+
60+
_createClass(Config, [{
61+
key: 'setExports',
62+
value: function setExports(files) {
63+
var _this = this;
64+
65+
[].concat(files).forEach(function (file) {
66+
var entryFile = Array.isArray(file) ? file[file.length - 1] : file;
67+
68+
var name = entryFile.replace(/\.\w+$/g, '');
69+
if (name.indexOf('./') == 0) {
70+
name = name.substring(2);
71+
} else if (name[0] == '/') {
72+
name = name.substring(1);
73+
}
74+
_this._config.entry[name] = Array.isArray(file) ? file : [file];
75+
});
76+
return this;
77+
}
78+
}, {
79+
key: 'setGroupExports',
80+
value: function setGroupExports(group, exportsArr) {
81+
var exportGroup = this._config.entryGroup;
82+
exportGroup[group] = exportGroup[group] ? exportGroup[group].concat(exportsArr) : exportsArr;
83+
84+
this.setExports(exportsArr);
85+
}
86+
}, {
87+
key: 'setOutput',
88+
value: function setOutput(output) {
89+
extend(this._config.output, output);
90+
return this;
91+
}
92+
}, {
93+
key: 'setSync',
94+
value: function setSync(syncConfig) {
95+
if (syncConfig) {
96+
if ((typeof syncConfig === 'undefined' ? 'undefined' : _typeof(syncConfig)) === 'object') {
97+
this._config.sync = syncConfig;
98+
} else if (typeof syncConfig === 'function') {
99+
this._config.sync = syncConfig();
100+
}
101+
}
102+
}
103+
}, {
104+
key: 'setCompiler',
105+
value: function setCompiler(compileConfig) {
106+
var _this2 = this;
107+
108+
if (compileConfig) {
109+
(function () {
110+
var nextConfig = {};
111+
112+
// 获取用户定义的compile配置
113+
if ((typeof compileConfig === 'undefined' ? 'undefined' : _typeof(compileConfig)) === 'object') {
114+
nextConfig = compileConfig;
115+
} else if (typeof compileConfig === 'function') {
116+
nextConfig = compileConfig(Object.assign({}, _this2._config)) || {};
117+
}
118+
119+
// 处理context
120+
if (nextConfig.context && !sysPath.isAbsolute(nextConfig.context)) {
121+
nextConfig.context = sysPath.resolve(_this2._config.cwd, nextConfig.context);
122+
}
123+
124+
// 处理loaders => loader
125+
if (nextConfig.module && nextConfig.module.loaders) {
126+
nextConfig.module.loaders.map(function (loader, i) {
127+
if (loader.loaders && !loader.loader) {
128+
loader.loader = loader.loaders.join("!");
129+
}
130+
return loader;
131+
});
132+
}
133+
134+
// 处理alias
135+
var context = nextConfig.context || _this2._config.context;
136+
var relativeContext = sysPath.relative(_this2._config.cwd, context);
137+
if (nextConfig.resolve && nextConfig.resolve.alias) {
138+
(function () {
139+
var alias = nextConfig.resolve.alias;
140+
Object.keys(alias).map(function (key, i) {
141+
alias[key] = sysPath.relative(relativeContext, alias[key]);
142+
});
143+
extend(true, _this2._config.resolve.alias, alias);
144+
})();
145+
}
146+
147+
_this2._config.resolve.root.push(context);
148+
149+
extend(true, _this2._config, nextConfig);
150+
})();
151+
}
152+
}
153+
}, {
154+
key: 'getConfig',
155+
value: function getConfig() {
156+
return this._config;
157+
}
158+
}]);
159+
160+
return Config;
161+
}();
162+
163+
;
164+
165+
module.exports = Config;

0 commit comments

Comments
 (0)