This repository has been archived by the owner on Mar 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
225 lines (203 loc) · 6.23 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/**
* MIT License https://opensource.org/licenses/mit-license.php
* Author Ryota Sugawara @ryo_suga
*/
"use strict";
var fs = require('fs');
var path = require('path');
var ConcatSource = require('webpack-sources').ConcatSource;
var PLUIN_IDENTIFY_STRING = '@license-banner-plugin'
function uniqueModuleFilter(module, index, source) {
return source.map(module => module.name).indexOf(module.name) === index;
}
function isArray(source) {
return Object.prototype.toString.call(source) === '[object Array]';
}
function isObject(source) {
return typeof source === 'object'
&& source !== null
&& !isArray(source);
}
var DEFAULT_LICENSE_TEMPLATE = function(pkg) {
var text = `${pkg.name}@${pkg.version}`;
if (pkg.author) text += `\n author: ${pkg.author}`;
if (pkg.license) text += `\n license: ${pkg.license}`;
if (pkg.repository) text += `\n repository: ${pkg.repository}`;
return text;
}
function getDefaultOptions() {
return {
licensePattern: null,
licenseTemplate: DEFAULT_LICENSE_TEMPLATE
};
}
class LicenseBannerWebpackPlugin {
constructor(options) {
if (arguments.length > 1)
throw new Error('LicenseBannerWebpackPlugin only takes one argument (pass an options object)');
if (!options)
options = getDefaultOptions();
if (!isObject(options))
throw new Error('LicenseBannerWebpackPlugin only takes an object argument.');
if (options.licenseDirectories && !isArray(options.licenseDirectories))
throw new Error('LicenseBannerWebpackPlugin options.licenseDirectories only takes Array.')
if (options.licenseTemplate && typeof options.licenseTemplate !== 'function')
throw new Error('LicenseBannerWebpackPlugin options.licenseTemplate only takes Function.');
this.options = Object.assign(
getDefaultOptions(),
options
);
}
apply(compiler) {
var directories = this.options.licenseDirectories || [
path.join(compiler.context, 'node_modules')
];
var optimizeChunkAssets = (compilation, chunks, callback) => {
Promise.all(
chunks.map(chunk => {
var modules = this.getModuleMap(chunk, directories);
return this.getLicenseBanner(modules)
.then(banner => {
if (banner)
chunk.files.forEach(file => {
compilation.assets[file] = new ConcatSource(
`/*\n${PLUIN_IDENTIFY_STRING}\n${banner}\n*/\n`,
compilation.assets[file]
);
});
});
})
).then(() => {
callback();
}, err => {
callback(err);
});
};
if (compiler.hooks) {
const plugin = { name: 'LicenseBannerWebpackPlugin' };
compiler.hooks.compilation.tap(plugin, compilation => {
compilation.hooks.optimizeChunkAssets.tapAsync(plugin, optimizeChunkAssets.bind(this, compilation));
});
} else {
compiler.plugin('compilation', compilation => {
compilation.plugin('optimize-chunk-assets', optimizeChunkAssets.bind(this, compilation));
});
}
}
getModuleMap(chunk, directories) {
return Array.from(chunk.modulesIterable, module => {
return module.resource || '';
}).filter(resource => {
for (var dir of directories)
if (resource.startsWith(dir)) return true;
return false;
}).map(resource => {
for (var dir of directories) {
if (resource.startsWith(dir)) {
var name = resource.replace(dir, '').split(path.sep)[1];
var directory = path.join(dir, name)
return {
name,
directory
};
}
}
}).filter(uniqueModuleFilter);
}
getLicenseBanner(modules) {
if (!modules.length) return Promise.resolve('');
return Promise.all(
modules.map(module => this.getLicensePackage(module))
).then(packages => {
return packages
.map(pkg => this.getLicenseText(pkg))
.filter(text => !!text)
.join('\n');
});
}
getLicenseText(pkg) {
var { licenseTemplate } = this.options;
return licenseTemplate(pkg);
}
getPackage(module) {
return new Promise((resolve, reject) => {
fs.readdir(module.directory, (err, files) => {
if (err) {
reject(err);
} else {
switch (true) {
case files.includes('package.json'):
resolve(this.getPackageJsonPkg(module));
break;
default:
reject('no package files exists');
}
}
});
});
}
getPackageJsonPkg(module) {
var pkg = require(path.join(module.directory, 'package.json'));
return {
name: pkg.name,
version: pkg.version,
author: this.getAuthorText(pkg.author),
license: pkg.license,
repository: this.getRepositoryText(pkg.repository)
};
}
// TODO: support bower
getBowerJsonPkg(dir) {
var pkg = require(path.join(dir, 'bower.json'));
return {
name: pkg.name,
version: pkg.version,
author: (pkg.authors ? pkg.authors : []).join(', '),
license: pkg.license,
repository: this.getRepositoryText(pkg.repository)
};
}
getLicensePackage(module) {
return this.getPackage(module)
.then(pkg => {
return {
name: pkg.name,
version: pkg.version,
author: this.getAuthorText(pkg.author),
license: pkg.license,
repository: this.getRepositoryText(pkg.repository),
};
}).catch(() => {
return {
name: module.name,
version: null,
author: null,
license: null,
repository: null
}
});
}
getRepositoryText(repository) {
if (isObject(repository) && repository.url) {
return repository.url;
} else if (typeof repository === 'string') {
return repository;
} else {
return null;
}
}
getAuthorText(author) {
if (isObject(author)) {
var text = [];
if (author.name) text.push(author.name);
if (author.email) text.push(`<${author.email}>`);
if (author.url) text.push(`(${author.url})`);
return text.join(' ');
} else if (typeof author === 'string') {
return author;
} else {
return null;
}
}
}
module.exports = LicenseBannerWebpackPlugin;