2020-05-28 10:23:10 +02:00
|
|
|
// eslint-disable-next-line import/no-extraneous-dependencies
|
2019-02-22 17:33:53 +01:00
|
|
|
const ncc = require('@zeit/ncc')
|
2019-03-19 00:21:18 +01:00
|
|
|
const { existsSync, readFileSync } = require('fs')
|
2020-03-29 05:41:35 +02:00
|
|
|
const { basename, dirname, extname, join } = require('path')
|
2019-02-22 17:33:53 +01:00
|
|
|
|
2020-05-18 21:24:37 +02:00
|
|
|
module.exports = function (task) {
|
2019-11-11 04:24:53 +01:00
|
|
|
// eslint-disable-next-line require-yield
|
2020-05-18 21:24:37 +02:00
|
|
|
task.plugin('ncc', {}, function* (file, options) {
|
2020-03-30 01:11:08 +02:00
|
|
|
if (options.externals && options.packageName) {
|
|
|
|
options.externals = { ...options.externals }
|
|
|
|
delete options.externals[options.packageName]
|
|
|
|
}
|
2019-02-27 15:12:40 +01:00
|
|
|
return ncc(join(__dirname, file.dir, file.base), {
|
2020-03-30 00:41:15 +02:00
|
|
|
filename: file.base,
|
2019-08-12 23:31:07 +02:00
|
|
|
minify: true,
|
2019-11-11 04:24:53 +01:00
|
|
|
...options,
|
2019-02-27 15:12:40 +01:00
|
|
|
}).then(({ code, assets }) => {
|
2020-05-18 21:24:37 +02:00
|
|
|
Object.keys(assets).forEach((key) => {
|
2020-04-28 09:58:34 +02:00
|
|
|
let data = assets[key].source
|
|
|
|
|
|
|
|
if (join(file.dir, key).endsWith('terser-webpack-plugin/worker.js')) {
|
|
|
|
data = Buffer.from(
|
|
|
|
data
|
|
|
|
.toString()
|
|
|
|
.replace(
|
|
|
|
`require('terser')`,
|
|
|
|
`require("${options.externals['terser']}")`
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-02-27 15:12:40 +01:00
|
|
|
this._.files.push({
|
2020-04-28 09:58:34 +02:00
|
|
|
data,
|
2019-02-27 15:12:40 +01:00
|
|
|
base: basename(key),
|
2020-04-28 09:58:34 +02:00
|
|
|
dir: join(file.dir, dirname(key)),
|
2019-02-27 15:12:40 +01:00
|
|
|
})
|
2020-04-28 09:58:34 +02:00
|
|
|
})
|
2019-02-27 15:12:40 +01:00
|
|
|
|
|
|
|
if (options && options.packageName) {
|
2020-03-30 00:41:15 +02:00
|
|
|
writePackageManifest.call(this, options.packageName, file.base)
|
2019-02-22 17:33:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
file.data = Buffer.from(code, 'utf8')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2019-02-27 15:12:40 +01:00
|
|
|
|
|
|
|
// 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.
|
2020-03-30 00:41:15 +02:00
|
|
|
function writePackageManifest(packageName, main) {
|
2019-02-27 15:12:40 +01:00
|
|
|
const packagePath = require.resolve(packageName + '/package.json')
|
2020-03-30 00:41:15 +02:00
|
|
|
let { name, author, license } = require(packagePath)
|
2019-02-27 15:12:40 +01:00
|
|
|
|
2020-04-07 15:42:16 +02:00
|
|
|
const compiledPackagePath = join(__dirname, `compiled/${packageName}`)
|
2019-02-27 15:12:40 +01:00
|
|
|
|
|
|
|
const potentialLicensePath = join(dirname(packagePath), './LICENSE')
|
|
|
|
if (existsSync(potentialLicensePath)) {
|
2019-03-19 00:21:18 +01:00
|
|
|
this._.files.push({
|
|
|
|
dir: compiledPackagePath,
|
|
|
|
base: 'LICENSE',
|
2019-11-11 04:24:53 +01:00
|
|
|
data: readFileSync(potentialLicensePath, 'utf8'),
|
2019-03-19 00:21:18 +01:00
|
|
|
})
|
2020-04-07 15:42:16 +02:00
|
|
|
} 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'),
|
|
|
|
})
|
|
|
|
}
|
2019-02-27 15:12:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this._.files.push({
|
|
|
|
dir: compiledPackagePath,
|
|
|
|
base: 'package.json',
|
|
|
|
data:
|
|
|
|
JSON.stringify(
|
|
|
|
Object.assign(
|
|
|
|
{},
|
|
|
|
{ name, main: `${basename(main, '.' + extname(main))}` },
|
|
|
|
author ? { author } : undefined,
|
2020-03-29 05:41:35 +02:00
|
|
|
license ? { license } : undefined
|
2019-02-27 15:12:40 +01:00
|
|
|
)
|
2019-11-11 04:24:53 +01:00
|
|
|
) + '\n',
|
2019-02-27 15:12:40 +01:00
|
|
|
})
|
|
|
|
}
|