-
Notifications
You must be signed in to change notification settings - Fork 43
/
taskfile-plugins.js
121 lines (108 loc) · 3.71 KB
/
taskfile-plugins.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
'use strict';
const findUp = require('find-up');
const ncc = require('@vercel/ncc');
const { existsSync, readFileSync } = require('fs');
const { basename, dirname, extname, join, resolve } = require('path');
const { Module } = require('module');
// See taskfile.js bundleContext definition for explanation
const m = new Module(resolve(__dirname, 'bundles', '_'));
m.filename = m.id;
m.paths = Module._nodeModulePaths(m.id);
const bundleRequire = m.require;
bundleRequire.resolve = (request, options) => Module._resolveFilename(request, m, false, options);
module.exports = function (task, _utils) {
// eslint-disable-next-line require-yield -- taskr plugin
task.plugin('ncc', {}, function *(file, options) {
if (options.externals && options.packageName) {
options.externals = { ...options.externals };
delete options.externals[options.packageName];
}
return ncc(join(__dirname, file.dir, file.base), {
filename: file.base,
minify: options.minify !== false,
assetBuilds: true,
esm: false,
target: 'es2019',
...options
}).then(({ code, assets }) => {
Object.keys(assets).forEach((key) => {
const data = assets[key].source;
this._.files.push({
data,
base: basename(key),
dir: join(file.dir, dirname(key))
});
});
if (options && options.packageName) {
writePackageManifest.call(
this,
options.packageName,
file.base,
options.bundleName
);
}
file.data = Buffer.from(code, 'utf8');
});
});
// eslint-disable-next-line require-yield -- taskr plugin
task.plugin('dts', {}, function *(file, options) {
const code = options.packages
.map(packageName => `declare module '@/compiled/${packageName}' {\n import * as p from '${packageName}';\n export = p;\n}\n`)
.join('\n');
return Promise.resolve().then(() => {
file.data = Buffer.from(code, 'utf8');
});
});
};
// This function writes a minimal `package.json` file for a compiled package.
// It defines `name`, `main`, `author`, and `license`. It also defines `types`.
// n.b. types intended for development usage only.
function writePackageManifest(packageName, main, bundleName) {
// some newer packages fail to include package.json in the exports
// so we can't reliably use require.resolve here
let packagePath;
try {
packagePath = bundleRequire.resolve(`${packageName}/package.json`);
} catch {
packagePath = findUp.sync('package.json', {
cwd: dirname(bundleRequire.resolve(packageName))
});
}
const { name, author, license } = require(packagePath);
const compiledPackagePath = join(
__dirname,
`src/compiled/${bundleName || packageName}`
);
const potentialLicensePath = join(dirname(packagePath), './LICENSE');
if (existsSync(potentialLicensePath)) {
this._.files.push({
dir: compiledPackagePath,
base: 'LICENSE',
data: readFileSync(potentialLicensePath, 'utf8')
});
} else {
// license might be lower case and not able to be found on case-sensitive
// file systems (ubuntu)
const otherPotentialLicensePath = join(dirname(packagePath), './license');
if (existsSync(otherPotentialLicensePath)) {
this._.files.push({
dir: compiledPackagePath,
base: 'LICENSE',
data: readFileSync(otherPotentialLicensePath, 'utf8')
});
}
}
this._.files.push({
dir: compiledPackagePath,
base: 'package.json',
data:
`${JSON.stringify(
Object.assign(
{},
{ name, main: basename(main, `.${extname(main)}`) },
author ? { author } : undefined,
license ? { license } : undefined
)
)}\n`
});
}