-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #809 from CleverCloud/bundle-project-to-single-cjs
Big rework (no lazy require, CJS => ESM sources, rollup to CJS build)
- Loading branch information
Showing
98 changed files
with
2,031 additions
and
2,415 deletions.
There are no files selected for viewing
File renamed without changes.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { defineConfig } from 'rollup'; | ||
import { nodeResolve } from '@rollup/plugin-node-resolve'; | ||
import commonjs from '@rollup/plugin-commonjs'; | ||
import json from '@rollup/plugin-json'; | ||
import MagicString from 'magic-string'; | ||
|
||
export default defineConfig({ | ||
input: 'bin/clever.js', | ||
output: { | ||
file: 'build/clever.cjs', | ||
format: 'cjs', | ||
sourcemap: 'inline', | ||
}, | ||
plugins: [ | ||
{ | ||
transform (code, id) { | ||
|
||
// formidable (used by superagent) hijacks require :-( | ||
if (id.includes('/node_modules/formidable/')) { | ||
const ms = new MagicString(code); | ||
ms.replaceAll( | ||
'if (global.GENTLY) require = GENTLY.hijack(require);', | ||
'', | ||
); | ||
return { | ||
code: ms.toString(), | ||
map: ms.generateMap(), | ||
}; | ||
} | ||
|
||
// for update notifier | ||
if (id.includes('/node_modules/update-notifier/')) { | ||
const ms = new MagicString(code); | ||
ms | ||
.replaceAll( | ||
`const importLazy = require('import-lazy')(require);`, | ||
'', | ||
) | ||
.replaceAll( | ||
/const ([^ ]+) = importLazy\(\'([^']+)\'\);/g, | ||
'const $1_ = require(\'$2\'); const $1 = () => $1_', | ||
); | ||
|
||
return { | ||
code: ms.toString(), | ||
map: ms.generateMap(), | ||
}; | ||
} | ||
|
||
// for ws peer deps | ||
if (id.includes('/node_modules/ws/')) { | ||
const ms = new MagicString(code); | ||
ms | ||
.replaceAll( | ||
'require(\'bufferutil\')', | ||
'null', | ||
) | ||
.replaceAll( | ||
'require(\'utf-8-validate\')', | ||
'{}', | ||
); | ||
|
||
return { | ||
code: ms.toString(), | ||
map: ms.generateMap(), | ||
}; | ||
} | ||
}, | ||
}, | ||
commonjs(), | ||
nodeResolve({ | ||
preferBuiltins: true, | ||
}), | ||
json(), | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,32 @@ | ||
const { exec: pkg } = require('pkg'); | ||
const cfg = require('./config.js'); | ||
const { | ||
getBinaryFilepath, | ||
getBinaryDirectory, | ||
} = require('./paths.js'); | ||
const { | ||
startTask, | ||
endTask, | ||
cleanupDirectory, | ||
} = require('./utils.js'); | ||
import { exec as pkg } from 'pkg'; | ||
import * as cfg from './config.js'; | ||
import { getBinaryFilepath, getBinaryDirectory, getWorkingDirectory } from './paths.js'; | ||
import { startTask, endTask, cleanupDirectory, exec } from './utils.js'; | ||
|
||
module.exports = async function build (version) { | ||
export async function build (version) { | ||
await cleanupDirectory(getBinaryDirectory(version)); | ||
|
||
const singleCjsScriptFilepath = getWorkingDirectory(version) + '/clever.cjs'; | ||
|
||
await bundleToSingleCjsScript(singleCjsScriptFilepath); | ||
|
||
for (const arch of cfg.archList) { | ||
const binaryFilepath = getBinaryFilepath(arch, version); | ||
await buildBinary(arch, binaryFilepath); | ||
await buildBinary(arch, singleCjsScriptFilepath, binaryFilepath); | ||
} | ||
}; | ||
|
||
// --- private | ||
|
||
async function buildBinary (arch, binaryFilepath) { | ||
async function bundleToSingleCjsScript (singleCjsScriptFilepath) { | ||
startTask('Bundling project with rollup to single CJS script'); | ||
await exec(`npm run build -- -o ${singleCjsScriptFilepath}`); | ||
endTask(`Bundling project with rollup to single CJS script to ${singleCjsScriptFilepath}`); | ||
} | ||
|
||
async function buildBinary (arch, sourceFilepath, binaryFilepath) { | ||
const { nodeVersion } = cfg; | ||
startTask(`Building binary for ${arch}`); | ||
await pkg(['.', '-t', `node${nodeVersion}-${arch}`, '-o', binaryFilepath]); | ||
await pkg([sourceFilepath, '-t', `node${nodeVersion}-${arch}`, '-o', binaryFilepath]); | ||
endTask(`Building binary for ${arch} to ${binaryFilepath}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,29 @@ | ||
'use strict'; | ||
import os from 'node:os'; | ||
import { getPackageJson } from '../src/load-package-json.cjs'; | ||
const pkg = getPackageJson(); | ||
|
||
const os = require('os'); | ||
const pkgJson = require('../package.json'); | ||
|
||
const archList = ['linux', 'macos', 'win']; | ||
const archEmoji = { | ||
export const archList = ['linux', 'macos', 'win']; | ||
export const archEmoji = { | ||
linux: '🐧', | ||
macos: '🍏', | ||
win: '🪟', | ||
}; | ||
const nodeVersion = pkgJson['pkg-node-version']; | ||
const git = { | ||
export const nodeVersion = pkg['pkg-node-version']; | ||
export const git = { | ||
email: '[email protected]', | ||
name: 'Clever Cloud CI', | ||
}; | ||
const appInfos = { | ||
name: pkgJson.name, | ||
export const appInfos = { | ||
name: pkg.name, | ||
vendor: 'Clever Cloud', | ||
url: pkgJson.homepage, | ||
description: pkgJson.description, | ||
license: pkgJson.license, | ||
url: pkg.homepage, | ||
description: pkg.description, | ||
license: pkg.license, | ||
maintainer: `${git.name} <${git.email}>`, | ||
keywords: pkgJson.keywords.join(' '), | ||
keywords: pkg.keywords.join(' '), | ||
}; | ||
|
||
function getNexusAuth () { | ||
export function getNexusAuth () { | ||
const user = process.env.NEXUS_USER || 'ci'; | ||
const password = process.env.NEXUS_PASSWORD; | ||
const nugetApiKey = process.env.NUGET_API_KEY; | ||
|
@@ -37,15 +36,15 @@ function getNexusAuth () { | |
return { user, password, nugetApiKey }; | ||
} | ||
|
||
function getNpmToken () { | ||
export function getNpmToken () { | ||
const token = process.env.NPM_TOKEN; | ||
if (!token) { | ||
throw new Error('Could not read NPM token!'); | ||
} | ||
return token; | ||
} | ||
|
||
function getDockerHubConf () { | ||
export function getDockerHubConf () { | ||
const username = process.env.DOCKERHUB_USERNAME; | ||
const token = process.env.DOCKERHUB_TOKEN; | ||
if (username == null || token == null) { | ||
|
@@ -54,15 +53,15 @@ function getDockerHubConf () { | |
return { username, token, imageName: 'clevercloud/clever-tools' }; | ||
} | ||
|
||
function getGpgConf () { | ||
export function getGpgConf () { | ||
const gpgPrivateKey = process.env.RPM_GPG_PRIVATE_KEY; | ||
const gpgPath = process.env.RPM_GPG_PATH || os.homedir(); | ||
const gpgName = process.env.RPM_GPG_NAME; | ||
const gpgPass = process.env.RPM_GPG_PASS; | ||
return { gpgPrivateKey, gpgPath, gpgName, gpgPass }; | ||
} | ||
|
||
function getCellarConf (scope) { | ||
export function getCellarConf (scope) { | ||
if (scope === 'previews') { | ||
return { | ||
host: 'cellar-c2.services.clever-cloud.com', | ||
|
@@ -81,16 +80,3 @@ function getCellarConf (scope) { | |
} | ||
throw new Error(`Unsupported cellar scope "${scope}". Supported scopes: "previews", "releases".`); | ||
} | ||
|
||
module.exports = { | ||
archList, | ||
archEmoji, | ||
nodeVersion, | ||
git, | ||
appInfos, | ||
getNexusAuth, | ||
getNpmToken, | ||
getDockerHubConf, | ||
getGpgConf, | ||
getCellarConf, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.