8221c180a5
This upgrades to ncc@0.25.0 and fixes the previous bugs including: * ncc not referenced correctly in build * Babel type errors * node-fetch, etag, chalk and raw-body dependencies not building with ncc - these have been "un-ncc'd" for now. As they are relatively small dependencies, this doesn't seem too much of an issue and we can follow up in the tracking ncc issue at https://github.com/vercel/ncc/issues/612. * `yarn dev` issues Took a lot of bisecting, but the overall diff isn't too bad here in the end.
100 lines
2.9 KiB
JavaScript
100 lines
2.9 KiB
JavaScript
// eslint-disable-next-line import/no-extraneous-dependencies
|
|
const ncc = require('@vercel/ncc')
|
|
const { existsSync, readFileSync } = require('fs')
|
|
const { basename, dirname, extname, join } = require('path')
|
|
|
|
module.exports = function (task) {
|
|
// eslint-disable-next-line require-yield
|
|
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: true,
|
|
...options,
|
|
}).then(({ code, assets }) => {
|
|
Object.keys(assets).forEach((key) => {
|
|
let data = assets[key].source
|
|
|
|
if (
|
|
join(file.dir, key).endsWith('terser-webpack-plugin/dist/minify.js')
|
|
) {
|
|
data = Buffer.from(
|
|
data
|
|
.toString()
|
|
.replace(
|
|
`require('terser')`,
|
|
`require("${options.externals['terser']}")`
|
|
)
|
|
)
|
|
}
|
|
|
|
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')
|
|
})
|
|
})
|
|
}
|
|
|
|
// 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) {
|
|
const packagePath = require.resolve(packageName + '/package.json')
|
|
let { name, author, license } = require(packagePath)
|
|
|
|
const compiledPackagePath = join(
|
|
__dirname,
|
|
`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',
|
|
})
|
|
}
|