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

feat(build): clean build folder before rebuilding all targets #5695

Merged
merged 2 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
104 changes: 12 additions & 92 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { execSync } from 'child_process';
import fs from 'fs';
import path from 'path';
import { execSync } from 'node:child_process';
import * as fs from 'node:fs';

// 1st party Rollup plugins
import { createFilter } from '@rollup/pluginutils';
import { babel } from '@rollup/plugin-babel';
import resolve from '@rollup/plugin-node-resolve';
import strip from '@rollup/plugin-strip';
Expand All @@ -13,10 +11,13 @@ import terser from '@rollup/plugin-terser';
import dts from 'rollup-plugin-dts';
import jscc from 'rollup-plugin-jscc';
import { visualizer } from 'rollup-plugin-visualizer';

// custom Rollup plugins
import { shaderChunks } from './utils/rollup-shader-chunks.mjs';
import { engineLayerImportValidation } from './utils/rollup-import-validation.mjs';
import { spacesToTabs } from './utils/rollup-spaces-to-tabs.mjs';

/** @typedef {import('rollup').RollupOptions} RollupOptions */
/** @typedef {import('rollup').Plugin} Plugin */
/** @typedef {import('rollup').OutputOptions} OutputOptions */
/** @typedef {import('@rollup/plugin-babel').RollupBabelInputPluginOptions} RollupBabelInputPluginOptions */
/** @typedef {import('@rollup/plugin-strip').RollupStripOptions} RollupStripOptions */
Expand Down Expand Up @@ -63,93 +64,6 @@ function getBanner(config) {
].join('\n');
}

/**
* This plugin converts every two spaces into one tab. Two spaces is the default the babel plugin
* outputs, which is independent of the four spaces of the code base.
*
* @param {boolean} enable - Enable or disable the plugin.
* @returns {Plugin} The plugin.
*/
function spacesToTabs(enable) {
const filter = createFilter([
'**/*.js'
], []);

return {
name: "spacesToTabs",
transform(code, id) {
if (!enable || !filter(id)) return undefined;
// ^ = start of line
// " +" = one or more spaces
// gm = find all + multiline
const regex = /^ +/gm;
code = code.replace(
regex,
startSpaces => startSpaces.replace(/ {2}/g, '\t')
);
return {
code,
map: null
};
}
};
}

/**
* Validate and print warning if an engine module on a lower level imports module on a higher level
*
* @param {string} rootFile - The root file, typically `src/index.js`.
* @param {boolean} enable - Enable or disable the plugin.
* @returns {Plugin} The plugin.
*/
function engineLayerImportValidation(rootFile, enable) {

const folderLevels = {
'core': 0,
'platform': 1,
'scene': 2,
'framework': 3
};

let rootPath;

return {
name: 'engineLayerImportValidation',

buildStart() {
rootPath = path.parse(path.resolve(rootFile)).dir;
},

resolveId(imported, importer) {
if (enable) {

// skip non-relative paths, those are not our imports, for example 'rollupPluginBabelHelpers.js'
if (importer && imported && imported.includes('./')) {

// convert importer path
const importerDir = path.parse(importer).dir;
const relImporter = path.dirname(path.relative(rootPath, importer));
const folderImporter = relImporter.split(path.sep)[0];
const levelImporter = folderLevels[folderImporter];

// convert imported path
const absImported = path.resolve(path.join(importerDir, imported));
const relImported = path.dirname(path.relative(rootPath, absImported));
const folderImported = relImported.split(path.sep)[0];
const levelImported = folderLevels[folderImported];

if (levelImporter < levelImported) {
console.log(`(!) Incorrect import: [${path.relative(rootPath, importer)}] -> [${imported}]`);
}
}
}

// we don't process imports, return null to allow chaining
return null;
}
};
}

/**
* The ES5 options for babel(...) plugin.
*
Expand Down Expand Up @@ -440,6 +354,12 @@ export default (args) => {
let targets = [];

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

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

if (envTarget === 'types') {
targets.push(target_types);
} else if (envTarget === 'extras') {
Expand Down
58 changes: 58 additions & 0 deletions utils/rollup-import-validation.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import path from 'node:path';

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

/**
* Validate and print warning if an engine module on a lower level imports module on a higher level
*
* @param {string} rootFile - The root file, typically `src/index.js`.
* @param {boolean} enable - Enable or disable the plugin.
* @returns {Plugin} The plugin.
*/
export function engineLayerImportValidation(rootFile, enable) {

const folderLevels = {
'core': 0,
'platform': 1,
'scene': 2,
'framework': 3
};

let rootPath;

return {
name: 'engineLayerImportValidation',

buildStart() {
rootPath = path.parse(path.resolve(rootFile)).dir;
},

resolveId(imported, importer) {
if (enable) {

// skip non-relative paths, those are not our imports, for example 'rollupPluginBabelHelpers.js'
if (importer && imported && imported.includes('./')) {

// convert importer path
const importerDir = path.parse(importer).dir;
const relImporter = path.dirname(path.relative(rootPath, importer));
const folderImporter = relImporter.split(path.sep)[0];
const levelImporter = folderLevels[folderImporter];

// convert imported path
const absImported = path.resolve(path.join(importerDir, imported));
const relImported = path.dirname(path.relative(rootPath, absImported));
const folderImported = relImported.split(path.sep)[0];
const levelImported = folderLevels[folderImported];

if (levelImporter < levelImported) {
console.log(`(!) Incorrect import: [${path.relative(rootPath, importer)}] -> [${imported}]`);
}
}
}

// we don't process imports, return null to allow chaining
return null;
}
};
}
35 changes: 35 additions & 0 deletions utils/rollup-spaces-to-tabs.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { createFilter } from '@rollup/pluginutils';

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

/**
* This plugin converts every two spaces into one tab. Two spaces is the default the babel plugin
* outputs, which is independent of the four spaces of the code base.
*
* @param {boolean} enable - Enable or disable the plugin.
* @returns {Plugin} The plugin.
*/
export function spacesToTabs(enable) {
const filter = createFilter([
'**/*.js'
], []);

return {
name: "spacesToTabs",
transform(code, id) {
if (!enable || !filter(id)) return undefined;
// ^ = start of line
// " +" = one or more spaces
// gm = find all + multiline
const regex = /^ +/gm;
code = code.replace(
regex,
startSpaces => startSpaces.replace(/ {2}/g, '\t')
);
return {
code,
map: null
};
}
};
}