Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bundled build options #6296

Merged
merged 13 commits into from
May 6, 2024
15 changes: 10 additions & 5 deletions build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
* Usage: node build.mjs [options] -- [rollup options]
*
* Options:
* target:<moduleFormat>:<buildType> - Specify the target module format and build type. Example: target:esm:release
* target:<moduleFormat> - Specify the target module format only. Example: target:esm
* target:<buildType> - Specify the build type only. Example: target:release
* target[:<moduleFormat>][:<buildType>][:<bundleState>] - Specify the target
* - moduleFormat (esm, umd)
* - buildType (release, debug, profiler, min)
* - bundleState (unbundled, bundled)
* Example: target:esm:release:bundled
*
* treemap - Enable treemap build visualization.
* treenet - Enable treenet build visualization.
Expand Down Expand Up @@ -34,5 +36,8 @@ for (let i = 0; i < args.length; i++) {
}

const cmd = `rollup -c ${args.join(' ')} ${env.join(' ')}`;
console.log(cmd);
execSync(cmd, { stdio: 'inherit' });
try {
execSync(cmd, { stdio: 'inherit' });
} catch (e) {
console.error(e.message);
}
12 changes: 6 additions & 6 deletions examples/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,29 +112,29 @@ function getEngineTargets() {
targets.push(...buildTarget({
moduleFormat: 'esm',
buildType: 'release',
bundleState: 'unbundled',
input: '../src/index.js',
dir: 'dist/iframe',
skipBundled: true
dir: 'dist/iframe'
}));
}
if (NODE_ENV === 'production' || NODE_ENV === 'development') {
// Outputs: dist/iframe/playcanvas.dbg.mjs
targets.push(...buildTarget({
moduleFormat: 'esm',
buildType: 'debug',
bundleState: 'unbundled',
input: '../src/index.js',
dir: 'dist/iframe',
skipBundled: true
dir: 'dist/iframe'
}));
}
if (NODE_ENV === 'production' || NODE_ENV === 'profiler') {
// Outputs: dist/iframe/playcanvas.prf.mjs
targets.push(...buildTarget({
moduleFormat: 'esm',
buildType: 'profiler',
bundleState: 'unbundled',
input: '../src/index.js',
dir: 'dist/iframe',
skipBundled: true
dir: 'dist/iframe'
}));
}
return targets;
Expand Down
61 changes: 53 additions & 8 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import { typesFixup } from './utils/plugins/rollup-types-fixup.mjs';

/** @typedef {import('rollup').RollupOptions} RollupOptions */

console.log(`Building PlayCanvas Engine v${version} revision ${revision}`);
const BLUE_OUT = '\x1b[34m';
const RED_OUT = '\x1b[31m';
const BOLD_OUT = `\x1b[1m`;
const REGULAR_OUT = `\x1b[22m`;
const RESET_OUT = `\x1b[0m`;

/**
* @type {['release', 'debug', 'profiler', 'min']}
Expand All @@ -23,6 +27,11 @@ const BUILD_TYPES = ['release', 'debug', 'profiler', 'min'];
*/
const MODULE_FORMAT = ['umd', 'esm'];

/**
* @type {['unbundled', 'bundled']}
*/
const BUNDLE_STATES = ['unbundled', 'bundled'];

/**
* @type {RollupOptions[]}
*/
Expand All @@ -40,31 +49,67 @@ const TYPES_TARGET = [{
]
}];

/**
* @type {RollupOptions[]}
*/
const targets = [];

const envTarget = process.env.target ? process.env.target.toLowerCase() : null;

const title = [
`Building PlayCanvas Engine`,
`version ${BOLD_OUT}v${version}${REGULAR_OUT}`,
`revision ${BOLD_OUT}${revision}${REGULAR_OUT}`,
`target ${BOLD_OUT}${envTarget ?? 'all'}${REGULAR_OUT}`
].join('\n');
console.log(`${BLUE_OUT}${title}${RESET_OUT}`);

if (envTarget === null && fs.existsSync('build')) {
// no targets specified, clean build directory
fs.rmSync('build', { recursive: true });
}

function includeBuild(buildType, moduleFormat, bundleState) {
return envTarget === null ||
envTarget === buildType ||
envTarget === moduleFormat ||
envTarget === bundleState ||
envTarget === `${moduleFormat}:${buildType}` ||
envTarget === `${moduleFormat}:${bundleState}` ||
envTarget === `${buildType}:${bundleState}` ||
envTarget === `${moduleFormat}:${buildType}:${bundleState}`;
}

/**
* @type {RollupOptions[]}
*/
const targets = [];
BUILD_TYPES.forEach((buildType) => {
MODULE_FORMAT.forEach((moduleFormat) => {
if (envTarget === null || envTarget === buildType || envTarget === moduleFormat || envTarget === `${moduleFormat}:${buildType}`) {
BUNDLE_STATES.forEach((bundleState) => {
if (bundleState === 'unbundled' && moduleFormat === 'umd') {
return;
}
if (bundleState === 'unbundled' && buildType === 'min') {
return;
}

if (!includeBuild(buildType, moduleFormat, bundleState)) {
return;
}

targets.push(...buildTarget({
moduleFormat,
buildType
buildType,
bundleState
}));
}
});
});
});

if (envTarget === null || envTarget === 'types') {
targets.push(...TYPES_TARGET);
}

if (!targets.length) {
console.error(`${RED_OUT}${BOLD_OUT}No targets found${RESET_OUT}`);
process.exit(1);
}

export default targets;
56 changes: 29 additions & 27 deletions utils/rollup-build-target.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -149,19 +149,45 @@ function getOutPlugins() {
* @param {object} options - The build target options.
* @param {'umd'|'esm'} options.moduleFormat - The module format.
* @param {'debug'|'release'|'profiler'|'min'} options.buildType - The build type.
* @param {'unbundled'|'bundled'} [options.bundleState] - The bundle state.
* @param {string} [options.input] - Only used for examples to change it to `../src/index.js`.
* @param {string} [options.dir] - Only used for examples to change the output location.
* @param {boolean} [options.skipBundled] - Whether to skip the bundled target (ESM only).
* @returns {RollupOptions[]} Rollup targets.
*/
function buildTarget({ moduleFormat, buildType, input = 'src/index.js', dir = 'build', skipBundled = false }) {
function buildTarget({ moduleFormat, buildType, bundleState, input = 'src/index.js', dir = 'build' }) {
const isUMD = moduleFormat === 'umd';
const isDebug = buildType === 'debug';
const isMin = buildType === 'min';
const bundled = isUMD || isMin;
const bundled = isUMD || isMin || bundleState === 'bundled';

const targets = [];

// bundle from unbundled
if (bundled && HISTORY.has(`${buildType}-${moduleFormat}-false`)) {
const unbundled = HISTORY.get(`${buildType}-${moduleFormat}-false`);

/**
* @type {RollupOptions}
*/
const target = {
input: `${unbundled.output.dir}/src/index.js`,
output: {
banner: getBanner(BANNER[buildType]),
format: 'es',
indent: '\t',
sourcemap: isDebug && 'inline',
name: 'pc',
preserveModules: false,
file: `${dir}/${OUT_PREFIX[buildType]}.mjs`
}
};

HISTORY.set(`${buildType}-${moduleFormat}-true`, target);
targets.push(target);

return targets;
}

// minify from release build
if (isMin && HISTORY.has(`release-${moduleFormat}-true`)) {
const release = HISTORY.get(`release-${moduleFormat}-true`);
Expand Down Expand Up @@ -217,30 +243,6 @@ function buildTarget({ moduleFormat, buildType, input = 'src/index.js', dir = 'b
HISTORY.set(`${buildType}-${moduleFormat}-${bundled}`, target);
targets.push(target);

// bundle ESM from unbundled ESM build
if (!skipBundled && !bundled && HISTORY.has(`${buildType}-${moduleFormat}-false`)) {
const unbundled = HISTORY.get(`${buildType}-${moduleFormat}-false`);

/**
* @type {RollupOptions}
*/
const target = {
input: `${unbundled.output.dir}/src/index.js`,
output: {
banner: getBanner(BANNER[buildType]),
format: 'es',
indent: '\t',
sourcemap: isDebug && 'inline',
name: 'pc',
preserveModules: false,
file: `${dir}/${OUT_PREFIX[buildType]}.mjs`
}
};

HISTORY.set(`${buildType}-${moduleFormat}-true`, target);
targets.push(target);
}

return targets;
}

Expand Down