Files
next.js/packages/next/taskfile-ncc.js
Arian Tron 61f56f997c
Some checks failed
Test examples / Test Examples (20) (push) Has been cancelled
Test examples / Test Examples (22) (push) Has been cancelled
Lock Threads / action (push) Has been cancelled
Trigger Release / start (push) Has been cancelled
Stale issue handler / stale (push) Has been cancelled
Update Font Data / create-pull-request (push) Has been cancelled
build-and-deploy / deploy-target (push) Has been cancelled
build-and-deploy / build (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / build-wasm (nodejs) (push) Has been cancelled
build-and-deploy / build-wasm (web) (push) Has been cancelled
build-and-deploy / Deploy preview tarball (push) Has been cancelled
build-and-deploy / Potentially publish release (push) Has been cancelled
build-and-deploy / publish-turbopack-npm-packages (push) Has been cancelled
build-and-deploy / Deploy examples (push) Has been cancelled
build-and-deploy / thank you, build (push) Has been cancelled
build-and-deploy / Upload Turbopack Bytesize metrics to Datadog (push) Has been cancelled
Rspack Next.js development integration tests / Rspack integration tests (push) Has been cancelled
Rspack Next.js production integration tests / Rspack integration tests (push) Has been cancelled
Turbopack Next.js development integration tests / Next.js integration tests (push) Has been cancelled
Turbopack Next.js production integration tests / Next.js integration tests (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack development test manifest (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack production test manifest (push) Has been cancelled
Upload bundler test manifests to areweturboyet.com / Upload test results (push) Has been cancelled
Update React / create-pull-request (push) Has been cancelled
test-e2e-project-reset-cron / reset-test-project (push) Has been cancelled
Notify about the top 15 issues/PRs/feature requests (most reacted) in the last 90 days / run (push) Has been cancelled
first commit
2026-03-10 19:37:31 +03:30

125 lines
3.4 KiB
JavaScript

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')
// files might be lower case and not able to be found on case-sensitive
// file systems (ubuntu)
const potentialLicenseFiles = [
'LICENSE',
'license',
'LICENSE.md',
'License.md',
'license.md',
]
// 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) {
// 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]
}
let precompiled = options.precompiled !== false
delete options.precompiled
return ncc(join(__dirname, file.dir, file.base), {
filename: file.base,
minify: options.minify === false ? false : true,
assetBuilds: true,
cache: false,
...options,
}).then(({ code, assets }) => {
Object.keys(assets).forEach((key) => {
let 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,
precompiled,
options.packageJsonName
)
}
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,
precompiled,
packageJsonName
) {
// 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)),
})
}
let { name, author, license } = require(packagePath)
const compiledPackagePath = join(
__dirname,
`${!precompiled ? 'dist/' : ''}src/compiled/${bundleName || packageName}`
)
for (const licenseFile of potentialLicenseFiles) {
const potentialLicensePath = join(dirname(packagePath), licenseFile)
if (existsSync(potentialLicensePath)) {
this._.files.push({
dir: compiledPackagePath,
base: 'LICENSE',
data: readFileSync(potentialLicensePath, 'utf8'),
})
break
}
}
this._.files.push({
dir: compiledPackagePath,
base: 'package.json',
data:
JSON.stringify(
Object.assign(
{},
{
name: packageJsonName ?? name,
main: `${basename(main, '.' + extname(main))}`,
},
author ? { author } : undefined,
license ? { license } : undefined
)
) + '\n',
})
}