Skip to content

Commit

Permalink
Project update. [p][robotic]
Browse files Browse the repository at this point in the history
  • Loading branch information
jaswrks committed Sep 21, 2023
1 parent a0a4960 commit a9b33c3
Show file tree
Hide file tree
Showing 17 changed files with 206 additions and 234 deletions.
2 changes: 1 addition & 1 deletion .browserslistrc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# Generated data.
# <generated:start>

# Last generated Thu, Sep 21, 2023, 4:13:35 AM EDT.
# Last generated Thu, Sep 21, 2023, 6:16:45 AM EDT.

[production]
last 1 chrome versions
Expand Down
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# Generated data.
# <generated:start>

# Last generated Thu, Sep 21, 2023, 4:13:34 AM EDT.
# Last generated Thu, Sep 21, 2023, 6:16:44 AM EDT.

# Locals

Expand Down
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# Generated data.
# <generated:start>

# Last generated Thu, Sep 21, 2023, 4:13:34 AM EDT.
# Last generated Thu, Sep 21, 2023, 6:16:44 AM EDT.

# Default

Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# Generated data.
# <generated:start>

# Last generated Thu, Sep 21, 2023, 4:13:34 AM EDT.
# Last generated Thu, Sep 21, 2023, 6:16:44 AM EDT.

# Locals

Expand Down
2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# Generated data.
# <generated:start>

# Last generated Thu, Sep 21, 2023, 4:13:34 AM EDT.
# Last generated Thu, Sep 21, 2023, 6:16:44 AM EDT.

# Locals

Expand Down
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# Generated data.
# <generated:start>

# Last generated Thu, Sep 21, 2023, 4:13:34 AM EDT.
# Last generated Thu, Sep 21, 2023, 6:16:44 AM EDT.

# Packages

Expand Down
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @note This entire file will be updated automatically.
* @note Instead of editing here, please review `./settings.mjs`.
*
* Last generated using `./settings.mjs` Thu, Sep 21, 2023, 4:13:34 AM EDT.
* Last generated using `./settings.mjs` Thu, Sep 21, 2023, 6:16:44 AM EDT.
*/
{
"editor.autoIndent": "full",
Expand Down
2 changes: 1 addition & 1 deletion .vscodeignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# Generated data.
# <generated:start>

# Last generated Thu, Sep 21, 2023, 4:13:34 AM EDT.
# Last generated Thu, Sep 21, 2023, 6:16:44 AM EDT.

# Locals

Expand Down
16 changes: 16 additions & 0 deletions dev/.files/bin/includes/extensions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ const asBracedGlob = (exts) => {
return (exts.length > 1 ? '{' : '') + noDot(exts).join(',') + (exts.length > 1 ? '}' : '');
};

/**
* Converts an array of extensions into no-brace globstars.
*
* @param exts Array of extensions.
*
* @returns Extensions as no-brace globstars.
*/
const asNoBraceGlobstars = (exts) => {
return noDot(exts).map((ext) => '**/*.' + ext);
};

/**
* Converts an array of extensions into a regular expression fragment.
*
Expand All @@ -59,6 +70,7 @@ const extensions = {
dot,
noDot,
asBracedGlob,
asNoBraceGlobstars,
asRegExpFrag,

/**
Expand Down Expand Up @@ -105,6 +117,10 @@ const extensions = {

allJavaScript: ['.js', '.jsx', '.cjs', '.cjsx', '.mjs', '.mjsx'],
allTypeScript: ['.ts', '.tsx', '.cts', '.ctsx', '.mts', '.mtsx'],

// Compiled WASM (WebAssembly).

wasm: ['.wasm'],
};

/**
Expand Down
37 changes: 11 additions & 26 deletions dev/.files/vite/config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,6 @@ export default async ({ mode, command, ssrBuild: isSSRBuild }) => {
const appBaseURL = env.APP_BASE_URL || ''; // e.g., `https://example.com/base`.
const appBasePath = env.APP_BASE_PATH || ''; // e.g., `/base`.

let appUMDName = (pkg.name || '').toLowerCase();
appUMDName = appUMDName.replace(/\bclevercanyon\b/gu, 'c10n');
appUMDName = appUMDName.replace(/@/gu, '').replace(/\./gu, '-').replace(/\/+/gu, '.');
appUMDName = appUMDName.replace(/[^a-z.0-9]([^.])/gu, (m0, m1) => m1.toUpperCase());
appUMDName = appUMDName.replace(/^\.|\.$/u, '');

const appType = $obp.get(pkg, 'config.c10n.&.' + (isSSRBuild ? 'ssrBuild' : 'build') + '.appType') || 'cma';
const targetEnv = $obp.get(pkg, 'config.c10n.&.' + (isSSRBuild ? 'ssrBuild' : 'build') + '.targetEnv') || 'any';
const entryFiles = $obp.get(pkg, 'config.c10n.&.' + (isSSRBuild ? 'ssrBuild' : 'build') + '.entryFiles') || [];
Expand All @@ -120,14 +114,13 @@ export default async ({ mode, command, ssrBuild: isSSRBuild }) => {
const targetEnvIsServer = ['cfw', 'node'].includes(targetEnv);
const useMinifier = 'dev' !== mode && !['lib'].includes(appType);
const preserveModules = ['lib'].includes(appType) && appEntries.length > 1;
const useUMD = !isSSRBuild && !targetEnvIsServer && !preserveModules && !peerDepKeys.length && useLibMode && 1 === appEntries.length;
const vitestSandboxEnable = $str.parseValue(String(process.env.VITEST_SANDBOX_ENABLE || '')); // We invented this environment variable.
const vitestExamplesEnable = $str.parseValue(String(process.env.VITEST_EXAMPLES_ENABLE || '')); // We invented this environment variable.
const vitestSandboxEnable = $str.parseValue(String(process.env.VITEST_SANDBOX_ENABLE || ''));
const vitestExamplesEnable = $str.parseValue(String(process.env.VITEST_EXAMPLES_ENABLE || ''));

/**
* Validates all of the above.
*/
if (!pkg.name || !appUMDName) {
if (!pkg.name) {
throw new Error('Apps must have a name.');
}
if (!appEntryFiles.length || !appEntries.length) {
Expand All @@ -153,9 +146,9 @@ export default async ({ mode, command, ssrBuild: isSSRBuild }) => {
* Prepares `package.json` property updates.
*/
const pkgUpdates = await vitePkgUpdates({
command, isSSRBuild, projDir, pkg, appType, targetEnv, useUMD,
appEntriesAsProjRelPaths, appEntriesAsSrcSubpaths, appEntriesAsSrcSubpathsNoExt
}); // prettier-ignore
command, isSSRBuild, projDir, pkg, appType, targetEnv,
appEntriesAsProjRelPaths, appEntriesAsSrcSubpaths, appEntriesAsSrcSubpathsNoExt
}); // prettier-ignore

/**
* Configures plugins for Vite.
Expand All @@ -166,9 +159,9 @@ export default async ({ mode, command, ssrBuild: isSSRBuild }) => {
await viteEJSConfig({ mode, projDir, srcDir, pkg, env }),
await viteMinifyConfig({ mode }),
await viteC10nConfig({
mode, command, isSSRBuild, projDir, distDir,
pkg, env, appType, targetEnv, staticDefs, pkgUpdates
}), // prettier-ignore
mode, command, isSSRBuild, projDir, distDir,
pkg, env, appType, targetEnv, staticDefs, pkgUpdates
}), // prettier-ignore
];

/**
Expand All @@ -179,7 +172,7 @@ export default async ({ mode, command, ssrBuild: isSSRBuild }) => {
/**
* Configures rollup for Vite.
*/
const rollupConfig = await viteRollupConfig({ srcDir, distDir, a16sDir, appEntries, peerDepKeys, preserveModules, useMinifier, useUMD });
const rollupConfig = await viteRollupConfig({ srcDir, distDir, a16sDir, appEntries, peerDepKeys, preserveModules, useMinifier });

/**
* Configures tests for Vite.
Expand Down Expand Up @@ -253,15 +246,7 @@ export default async ({ mode, command, ssrBuild: isSSRBuild }) => {
minify: useMinifier ? 'esbuild' : false, // Minify userland code?
modulePreload: false, // Disable. DOM injections conflict with our SPAs.

...(useLibMode // Use library mode in Vite, with specific formats?
? {
lib: {
name: appUMDName, // Name of UMD window global var.
entry: appEntries, // Should match up with `rollupOptions.input`.
formats: isSSRBuild ? ['es'] : useUMD ? ['es', 'umd'] : ['es', 'cjs'],
},
}
: {}),
...(useLibMode ? { lib: { entry: appEntries, formats: ['es'] } } : {}),
rollupOptions: rollupConfig, // See: <https://o5p.me/5Vupql>.
},
};
Expand Down
68 changes: 25 additions & 43 deletions dev/.files/vite/includes/package/updates.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import u from '../../../bin/includes/utilities.mjs';
*
* @returns Build-related property updates.
*/
export default async ({ command, isSSRBuild, projDir, pkg, appType, targetEnv, appEntriesAsProjRelPaths, appEntriesAsSrcSubpaths, appEntriesAsSrcSubpathsNoExt, useUMD }) => {
export default async ({ command, isSSRBuild, projDir, pkg, appType, targetEnv, appEntriesAsProjRelPaths, appEntriesAsSrcSubpaths, appEntriesAsSrcSubpathsNoExt }) => {
const updates = {}; // Initialize.

if (isSSRBuild) {
Expand All @@ -45,7 +45,7 @@ export default async ({ command, isSSRBuild, projDir, pkg, appType, targetEnv, a
throw new Error('Multipage apps must have an `./index.' + extensions.asBracedGlob([...extensions.trueHTML]) + '` entry point.');
}
(updates.exports = null), (updates.typesVersions = {});
updates.module = updates.main = updates.browser = updates.unpkg = updates.types = '';
updates.main = updates.module = updates.unpkg = updates.browser = updates.types = '';

break; // Stop here.
}
Expand All @@ -63,51 +63,33 @@ export default async ({ command, isSSRBuild, projDir, pkg, appType, targetEnv, a
'Library apps must have an `./index.' + extensions.asBracedGlob([...extensions.sTypeScript, ...extensions.sTypeScriptReact]) + '` entry point.',
);
}
if (useUMD) {
updates.exports = {
'.': {
import: './dist/' + appEntryIndexAsSrcSubpathNoExt + '.js',
require: './dist/' + appEntryIndexAsSrcSubpathNoExt + '.umd.cjs',
types: './dist/types/' + appEntryIndexAsSrcSubpathNoExt + '.d.ts',
},
};
updates.module = './dist/' + appEntryIndexAsSrcSubpathNoExt + '.js';
updates.main = './dist/' + appEntryIndexAsSrcSubpathNoExt + '.umd.cjs';

updates.browser = ['web', 'webw'].includes(targetEnv) ? updates.main : '';
updates.unpkg = updates.main;
updates.exports = {
'.': {
types: './dist/types/' + appEntryIndexAsSrcSubpathNoExt + '.d.ts', // First, always.
import: './dist/' + appEntryIndexAsSrcSubpathNoExt + '.js', // ESM module import path.
default: './dist/' + appEntryIndexAsSrcSubpathNoExt + '.js', // Last, always.
},
};
updates.main = './dist/' + appEntryIndexAsSrcSubpathNoExt + '.js';
updates.module = './dist/' + appEntryIndexAsSrcSubpathNoExt + '.js';

updates.types = './dist/types/' + appEntryIndexAsSrcSubpathNoExt + '.d.ts';
updates.typesVersions = { '>=3.1': { './*': ['./dist/types/*'] } };
} else {
updates.exports = {
'.': {
import: './dist/' + appEntryIndexAsSrcSubpathNoExt + '.js',
require: './dist/' + appEntryIndexAsSrcSubpathNoExt + '.cjs',
types: './dist/types/' + appEntryIndexAsSrcSubpathNoExt + '.d.ts',
},
};
updates.module = './dist/' + appEntryIndexAsSrcSubpathNoExt + '.js';
updates.main = './dist/' + appEntryIndexAsSrcSubpathNoExt + '.cjs';
updates.unpkg = updates.module; // Same, same. ESM-only builds.
updates.browser = ['web'].includes(targetEnv) ? updates.module : '';

updates.browser = ['web', 'webw'].includes(targetEnv) ? updates.module : '';
updates.unpkg = updates.module;
updates.typesVersions = { '>=3.1': { './*': ['./dist/types/*'] } };
updates.types = './dist/types/' + appEntryIndexAsSrcSubpathNoExt + '.d.ts';

updates.types = './dist/types/' + appEntryIndexAsSrcSubpathNoExt + '.d.ts';
updates.typesVersions = { '>=3.1': { './*': ['./dist/types/*'] } };

for (const appEntryAsSrcSubpathNoExt of appEntriesAsSrcSubpathsNoExt) {
if (appEntryAsSrcSubpathNoExt === appEntryIndexAsSrcSubpathNoExt) {
continue; // Don't remap the entry index.
}
$obj.patchDeep(updates.exports, {
['./' + appEntryAsSrcSubpathNoExt]: {
import: './dist/' + appEntryAsSrcSubpathNoExt + '.js',
require: './dist/' + appEntryAsSrcSubpathNoExt + '.cjs',
types: './dist/types/' + appEntryAsSrcSubpathNoExt + '.d.ts',
},
});
for (const appEntryAsSrcSubpathNoExt of appEntriesAsSrcSubpathsNoExt) {
if (appEntryAsSrcSubpathNoExt === appEntryIndexAsSrcSubpathNoExt) {
continue; // Don't remap the entry index.
}
$obj.patchDeep(updates.exports, {
['./' + appEntryAsSrcSubpathNoExt]: {
types: './dist/types/' + appEntryAsSrcSubpathNoExt + '.d.ts', // First, always.
import: './dist/' + appEntryAsSrcSubpathNoExt + '.js', // ESM module import path.
default: './dist/' + appEntryAsSrcSubpathNoExt + '.js', // Last, always.
},
});
}
break; // Stop here.
}
Expand Down
54 changes: 15 additions & 39 deletions dev/.files/vite/includes/rollup/config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
*/

import path from 'node:path';
import { $json, $str } from '../../../../../node_modules/@clevercanyon/utilities/dist/index.js';
import { $path, $str } from '../../../../../node_modules/@clevercanyon/utilities/dist/index.js';
import extensions from '../../../bin/includes/extensions.mjs';

/**
* Configures rollup for Vite.
Expand All @@ -21,10 +22,7 @@ import { $json, $str } from '../../../../../node_modules/@clevercanyon/utilities
*
* @returns Rollup configuration.
*/
export default async ({ srcDir, distDir, a16sDir, appEntries, peerDepKeys, preserveModules, useMinifier, useUMD }) => {
const entryCounters = new Map(),
chunkCounters = new Map();

export default async ({ srcDir, distDir, a16sDir, appEntries, peerDepKeys, preserveModules, useMinifier }) => {
return {
input: appEntries,

Expand All @@ -42,50 +40,28 @@ export default async ({ srcDir, distDir, a16sDir, appEntries, peerDepKeys, prese
compact: useMinifier, // Minify wrapper code generated by rollup?

// By default, special chars in a path like `[[name]].js` get changed to `__name__.js`.
// This prevents that by enforcing a custom sanitizer. See: <https://o5p.me/Y2fNf9> for details.
// This prevents that by enforcing a custom sanitizer. More details: <https://o5p.me/Y2fNf9>.
sanitizeFileName: (fileName) => fileName.replace(/[\0?*]/gu, ''),

// By default, in SSR mode, Vite forces all entry files into the distDir root.
// By default, in SSR mode, Vite forces all entry files into the `distDir` root.
// This prevents that by enforcing a consistently relative location for all entries.
entryFileNames: (entry) => {
// This function doesn’t have access to the current output format, unfortunately.
// However, we are setting `build.lib.formats` explicitly in the configuration below.
// Therefore, we know `es` comes first, followed by either `umd` or `cjs` output entries.
// So, entry counters make it possible to infer build output format, based on sequence.

const entryKey = $json.stringify(entry); // JSON serialization.
const entryCounter = Number(entryCounters.get(entryKey) || 0) + 1;

const entryFormat = entryCounter > 1 ? (useUMD ? 'umd' : 'cjs') : 'es';
const entryExt = 'umd' === entryFormat ? 'umd.cjs' : 'cjs' === entryFormat ? 'cjs' : 'js';

entryCounters.set(entryKey, entryCounter); // Updates counter.

if ('.html' === path.extname(entry.facadeModuleId)) {
if (/\//u.test(entry.name)) return '[name]-[hash].' + entryExt;
return path.join(path.relative(distDir, a16sDir), '[name]-[hash].' + entryExt);
if ([...extensions.trueHTML].includes($path.ext(entry.facadeModuleId))) {
if (/\//u.test(entry.name)) return '[name]-[hash].js'; // Already a subpath.
return path.join(path.relative(distDir, a16sDir), '[name]-[hash].js');
}
if (/\//u.test(entry.name)) return '[name].' + entryExt; // Already a subpath.
return path.join(path.relative(srcDir, path.dirname(entry.facadeModuleId)), '[name].' + entryExt);
if (/\//u.test(entry.name)) return '[name].js'; // Already a subpath.
return path.join(path.relative(srcDir, path.dirname(entry.facadeModuleId)), '[name].js');
},

// By default, in library mode, Vite ignores `build.assetsDir`.
// This prevents that by enforcing a consistent location for chunks and assets.
chunkFileNames: (chunk) => {
// This function doesn’t have access to the current output format, unfortunately.
// However, we are setting `build.lib.formats` explicitly in the configuration below.
// Therefore, we know `es` comes first, followed by either `umd` or `cjs` output chunks.
// So, chunk counters make it possible to infer build output format, based on sequence.

const chunkKey = $json.stringify(chunk); // JSON serialization.
const chunkCounter = Number(chunkCounters.get(chunkKey) || 0) + 1;

const chunkFormat = chunkCounter > 1 ? (useUMD ? 'umd' : 'cjs') : 'es';
const chunkExt = 'umd' === chunkFormat ? 'umd.cjs' : 'cjs' === chunkFormat ? 'cjs' : 'js';

chunkCounters.set(chunkKey, chunkCounter); // Updates counter.
return path.join(path.relative(distDir, a16sDir), '[name]-[hash].' + chunkExt);
chunkFileNames: (/* chunk */) => {
return path.join(path.relative(distDir, a16sDir), '[name]-[hash].js');
},

// By default, in library mode, Vite ignores `build.assetsDir`.
// This prevents that by enforcing a consistent location for chunks and assets.
assetFileNames: (/* asset */) => path.join(path.relative(distDir, a16sDir), '[name]-[hash].[ext]'),

// Preserves module structure in apps built explicitly as multi-entry libraries.
Expand Down
Loading

0 comments on commit a9b33c3

Please sign in to comment.