1 | const fs = require('fs');
|
2 | const path = require('path');
|
3 | const _sizeUnits = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
|
4 |
|
5 | let eeuiConfig = {};
|
6 | if (fs.existsSync(path.resolve(process.cwd(), 'eeui.config.js'))) {
|
7 | eeuiConfig = require(path.resolve(process.cwd(), 'eeui.config.js'));
|
8 | }
|
9 |
|
10 | const resolveSizeUnit = (size, i = 0) => {
|
11 | if (isNaN(size)) {
|
12 | return '';
|
13 | }
|
14 | if (size < 1000) {
|
15 | return size.toFixed(2).replace(/\.?0+$/, '') + _sizeUnits[i];
|
16 | } else {
|
17 | return resolveSizeUnit(size / 1024, i + 1);
|
18 | }
|
19 | };
|
20 |
|
21 | const loadModulePath = (moduleName, extra) => {
|
22 | try {
|
23 | const localPath = require.resolve(path.join(process.cwd(), 'node_modules', moduleName, extra || ''));
|
24 | return localPath.slice(0, localPath.lastIndexOf(moduleName) + moduleName.length);
|
25 | } catch (e) {
|
26 | try {
|
27 | const localPath = require.resolve(path.join(__dirname, '../../node_modules', moduleName, extra || ''));
|
28 | return localPath.slice(0, localPath.lastIndexOf(moduleName) + moduleName.length);
|
29 | } catch (e) {
|
30 | return moduleName;
|
31 | }
|
32 | }
|
33 | };
|
34 |
|
35 | const cssLoaders = (options) => {
|
36 | options = options || {};
|
37 |
|
38 | const cssLoader = {
|
39 | loader: loadModulePath('css-loader'),
|
40 | options: {
|
41 | sourceMap: options.sourceMap
|
42 | }
|
43 | };
|
44 |
|
45 | const postcssLoader = {
|
46 | loader: loadModulePath('postcss-loader'),
|
47 | options: {
|
48 | sourceMap: options.sourceMap
|
49 | }
|
50 | };
|
51 |
|
52 | const generateLoaders = (loader, loaderOptions) => {
|
53 | const loaders = options.useVue ? [cssLoader] : [];
|
54 | if (options.usePostCSS) {
|
55 | loaders.push(postcssLoader);
|
56 | }
|
57 | if (loader) {
|
58 | loaders.push({
|
59 | loader: loadModulePath(loader + '-loader'),
|
60 | options: Object.assign({}, loaderOptions, {
|
61 | sourceMap: !!options.sourceMap
|
62 | })
|
63 | });
|
64 |
|
65 | if (eeuiConfig.styleResources) {
|
66 | let styleResource = eeuiConfig.styleResources[loader];
|
67 | if (styleResource) {
|
68 | loaders.push({
|
69 | loader: loadModulePath('style-resources-loader'),
|
70 | options: {
|
71 | patterns: toString.call(styleResource) === "[object String]" ? [styleResource] : styleResource
|
72 | }
|
73 | });
|
74 | }
|
75 | }
|
76 | }
|
77 | if (options.useVue) {
|
78 | return [loadModulePath('vue-style-loader')].concat(loaders);
|
79 | } else {
|
80 | return loaders;
|
81 | }
|
82 | };
|
83 |
|
84 | return {
|
85 | less: generateLoaders('less'),
|
86 | sass: generateLoaders('sass', {indentedSyntax: true}),
|
87 | scss: generateLoaders('sass'),
|
88 | stylus: generateLoaders('stylus'),
|
89 | styl: generateLoaders('stylus')
|
90 | };
|
91 | };
|
92 |
|
93 | const accessSync = (path) => {
|
94 | try {
|
95 | fs.accessSync(path, fs.F_OK);
|
96 | } catch (e) {
|
97 | return false;
|
98 | }
|
99 | return true;
|
100 | };
|
101 |
|
102 | const errorServer = (res, errorCode, errorMsg) => {
|
103 | fs.readFile(__dirname + '/error.js', (err, data) => {
|
104 | if (err) {
|
105 | res.writeHead(404, {'content-type': 'text/html; charset=utf-8'});
|
106 | res.write('<h1>404错误</h1><p>你要找的页面不存在</p>');
|
107 | res.end();
|
108 | } else {
|
109 | data += "";
|
110 | if (errorCode) {
|
111 | data = data.replace('你访问的页面出错了!', '你访问的页面出错了! (' + errorCode + ')')
|
112 | }
|
113 | if (errorMsg) {
|
114 | data = data.replace('var errorMsg=decodeURIComponent("");', 'var errorMsg=decodeURIComponent("' + encodeURIComponent(errorMsg.replace(new RegExp(path.join(__dirname, '../../'), 'g'), '')) + '");')
|
115 | }
|
116 | res.writeHead(200, {'content-type': 'text/javascript; charset=utf-8'});
|
117 | res.write(data);
|
118 | res.end();
|
119 | }
|
120 | });
|
121 | };
|
122 |
|
123 | module.exports = {resolveSizeUnit, loadModulePath, cssLoaders, accessSync, errorServer};
|