/
index.js
176 lines (158 loc) · 6.35 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* A wrap of webpack
*
* @author wujohns
* @date 17/8/7
*/
'use strict';
const _ = require('lodash');
const path = require('path');
const fs = require('fs');
const async = require('async');
const webpack = require('webpack');
const stringHash = require('string-hash');
class Webpack2B {
/**
* Get the lib path which can be import
* @param {String} libSrc - original path
* @param {String} saveDir - target file saving path
* @returns {String} - the path after transform
* @static
*/
static _getResolvePath (libSrc, saveDir) {
if (/^(\.\/|\/|\.\.\/)/.test(libSrc)) {
// only transform path of ('/', './', '../')
const absolutePath = path.join(process.cwd(), libSrc);
const relativePath = path.relative(saveDir, absolutePath);
if (/^\./.test(relativePath)) {
return relativePath.split(path.sep).join('/');
}
return `./${ relativePath.split(path.sep).join('/') }`;
}
return libSrc;
}
/**
* libs packing
* @param {Object} libsConfig - libs' packing config
* @param {Array} libsConfig.libs - the config array, each item is the config of a lib
* @param {String} libsConfig.[libs].src - the source path of the lib
* @param {String} libsConfig.[libs].expose - the custom name of the lib
* @param {String} libsConfig.savePath - the saving directory of the packed file
* @param {String} libsConfig.globalField - global variable(defaults: 'WEBPACK_2B', whick means window.WEBPACK_2B)
* @param {Object} webpackConfig - custom webpack config except entry and output
* @param {Function} callback - callback funcion
* @static
*/
static libsPack (libsConfig, webpackConfig, callback) {
if (typeof webpackConfig === 'function') {
callback = webpackConfig;
webpackConfig = {};
}
const libs = _.get(libsConfig, 'libs', []);
const globalField = _.get(libsConfig, 'globalField', 'WEBPACK_2B');
let savePath = libsConfig.savePath;
if (!path.isAbsolute(savePath)) {
savePath = path.join(process.cwd(), savePath);
}
const saveDir = path.dirname(savePath);
const saveExtname = path.extname(savePath);
const saveBasename = path.basename(savePath, saveExtname);
const tempPath = path.format({
dir: saveDir,
name: `${ saveBasename }.temp`,
ext: saveExtname
});
async.auto({
// create temp lib file
tempLib: (callback) => {
let tempFileContent = `
'use strict';
window.${ globalField } = window.${ globalField } || {};
`;
// import packages and concat the content
_.forEach(libs, (lib) => {
// the path in temp file
const libSrc = Webpack2B._getResolvePath(lib.src, saveDir);
const libVarName = `${ globalField }_${ stringHash(lib.expose).toString(36) }`;
tempFileContent = `
${ tempFileContent }
import ${ libVarName } from '${ libSrc }';
import * as _${ libVarName } from '${ libSrc }';
window.${ globalField }['${ lib.expose }'] = ${ libVarName } || _${ libVarName };
`;
});
// create temp file by using fs
fs.writeFile(tempPath, tempFileContent, {
encoding: 'utf8',
flags: 'w',
mode: 0o666
}, callback);
},
// create the target libs file by using webpack
build: ['tempLib', (results, callback) => {
const entry = {};
entry[saveBasename] = [tempPath];
webpack(_.defaults({
entry: entry,
output: {
path: saveDir,
filename: `${ saveBasename }${ saveExtname }`
},
externals: libsConfig.externals
}, webpackConfig), callback);
}],
// clear temp file
clear: ['build', (results, callback) => fs.unlink(tempPath, callback)]
}, (err) => callback(err));
}
/**
* Get the externals config by libsConfig
* @param {Object} libsConfig - libs' packing config
* @return {[Object]} - the externals
* @static
*/
static getExternals (libsConfig) {
const libs = _.get(libsConfig, 'libs', []);
const globalField = _.get(libsConfig, 'globalField', 'WEBPACK_2B');
const externals = {};
_.forEach(libs, (lib) => {
externals[lib.expose] = `${ globalField }['${ lib.expose }']`;
});
return externals;
}
/**
* @param {Object} pagesConfig - pages' packing config
* @param {Array} pagesConfig.pages - the config array, each item is the config of a page
* @param {Array} pagesConfig.[pages].src - the source path of the page
* @param {String} pagesConfig.[pages].name - the file name of the page
* @param {Object} pagesConfig.externals - the externals lib which will not be packed into the dest file
* @param {String} pagesConfig.destDir - the save directory of the page
* @param {Object} webpackConfig - custom webpack config except entry, output and externals
* @param {Function} callback - callback function
*/
static pagesPack (pagesConfig, webpackConfig, callback) {
if (typeof webpackConfig === 'function') {
callback = webpackConfig;
webpackConfig = {};
}
let destDir = pagesConfig.destDir;
if (!path.isAbsolute(destDir)) {
destDir = path.join(process.cwd(), pagesConfig.destDir);
}
const pages = pagesConfig.pages;
const externals = pagesConfig.externals;
const entry = {};
_.forEach(pages, (page, callback) => {
entry[page.name] = page.src;
});
webpack(_.defaults({
entry: entry,
externals: externals,
output: {
path: destDir,
filename: '[name].js'
}
}, webpackConfig), callback);
}
}
module.exports = Webpack2B;