forked from pombredanne/webpack-licenses-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack-licenses-plugin.js
90 lines (70 loc) · 2.6 KB
/
webpack-licenses-plugin.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
'use strict';
const path = require('path')
function validateConfig(conf) {
return Object.assign({
acceptable: /.*/,
unacceptable: /unlicensed/i,
selected: {},
title: 'Licenses:',
filename: 'LICENSES.txt',
}, conf)
}
function canUseLicense(license, options) {
return license && license.match(options.acceptable) && !license.match(options.unacceptable)
}
function selectLicenseForPackage(info, options) {
let license
if (options.selected && options.selected[info.name]) {
license = options.selected[info.name]
} else if (!info.license && info.licenses) {
const l = info.licenses.find(t => canUseLicense(t.type, options))
license = l ? l.type : null
} else {
license = info.license
}
if (typeof license !== 'string') {
throw new Error(`Missing or unrecognized license for ${info.name}\n`)
} else if (!canUseLicense(license, options)) {
throw new Error(`Forbidden license [${license}] for ${info.name}\n`)
}
return license
}
function generateTextFile(title, licenses, extension) {
let header
let list
if (extension === 'markdown' || extension === 'md' || extension === 'mdown') {
header = `# ${title}`
list = licenses.map(l => `[${l.name}](${l.link}) licensed under ${l.license}`)
} else {
header = title
list = licenses.map(l => `${l.name} licensed under ${l.license} (${l.link})`)
}
return `${[header].concat(list).join('\n\n')}\n`
}
function WebpackLicensesPlugin(conf) {
this.options = validateConfig(conf)
}
WebpackLicensesPlugin.prototype.apply = function apply(compiler) {
/* eslint global-require: 0 */
const vendors = require(path.join(compiler.context, 'package.json')).dependencies
const licenses = Object.keys(vendors).map(name => {
const info = require(path.join(compiler.context, 'node_modules', name, 'package.json'))
const license = selectLicenseForPackage(info, this.options)
const link = info.homepage || (info.repository && info.repository.url)
return { name, license, link: link || `https://www.npmjs.com/package/${name}` }
})
compiler.plugin('emit', (compilation, cb) => {
const result = generateTextFile(this.options.title, licenses, this.options.filename)
compilation.assets[this.options.filename] = { /* eslint no-param-reassign: 0 */
source: () => result,
size: () => result.length,
}
cb()
})
}
if (process.env.NODE_ENV === 'test') {
WebpackLicensesPlugin.selectLicenseForPackage = selectLicenseForPackage
WebpackLicensesPlugin.generateTextFile = generateTextFile
WebpackLicensesPlugin.validateConfig = validateConfig
}
module.exports = WebpackLicensesPlugin