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 Aug 24, 2023
1 parent 22b6c89 commit d01a483
Show file tree
Hide file tree
Showing 4 changed files with 535 additions and 513 deletions.
72 changes: 47 additions & 25 deletions dev/.files/vite/config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ import { $fs, $glob } from '../../../node_modules/@clevercanyon/utilities.node/d
import { $http as $cfpꓺhttp } from '../../../node_modules/@clevercanyon/utilities.cfp/dist/index.js';
import { $is, $str, $obj, $obp, $time } from '../../../node_modules/@clevercanyon/utilities/dist/index.js';

import $preactꓺ404 from '../../../node_modules/@clevercanyon/utilities/dist/preact/components/404.js';
import { renderToString as $preactꓺrenderToString } from '../../../node_modules/@clevercanyon/utilities/dist/preact/apis/ssr.js';

import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);

Expand Down Expand Up @@ -109,8 +112,8 @@ export default async ({ mode, command, ssrBuild: isSSRBuild }) => {
const appEntriesAsSrcSubpathsNoExt = appEntriesAsSrcSubpaths.map((subpath) => subpath.replace(/\.[^.]+$/u, ''));

const isTargetEnvSSR = ['cfw', 'node'].includes(targetEnv); // Target environment requires server-side rendering?
const shouldPreserveModules = ['cma'].includes(appType) && appEntries.length > 1 && Object.keys(pkg.peerDependencies || {}).length > 0;
const shouldMinify = 'dev' === mode || shouldPreserveModules ? false : true; // Should minify output files?
const isLib = ['cma'].includes(appType) && appEntries.length > 1 && Object.keys(pkg.peerDependencies || {}).length > 0;
const shouldMinify = 'dev' === mode || isLib ? false : true; // Should minify output files?

/**
* Validates all of the above.
Expand Down Expand Up @@ -158,7 +161,23 @@ export default async ({ mode, command, ssrBuild: isSSRBuild }) => {
if (!appEntryIndexAsSrcSubpath || !appEntryIndexAsSrcSubpathNoExt) {
throw new Error('Custom apps must have an `./index.{ts,tsx}` entry point.');
}
if (isTargetEnvSSR || appEntries.length > 1) {
if (!isTargetEnvSSR && isLib && 1 === appEntries.length) {
updatePkg.exports = {
'.': {
import: './dist/' + appEntryIndexAsSrcSubpathNoExt + '.js',
require: './dist/' + appEntryIndexAsSrcSubpathNoExt + '.umd.cjs',
types: './dist/types/' + appEntryIndexAsSrcSubpathNoExt + '.d.ts',
},
};
updatePkg.module = './dist/' + appEntryIndexAsSrcSubpathNoExt + '.js';
updatePkg.main = './dist/' + appEntryIndexAsSrcSubpathNoExt + '.umd.cjs';

updatePkg.browser = ['web', 'webw'].includes(targetEnv) ? updatePkg.main : '';
updatePkg.unpkg = updatePkg.main;

updatePkg.types = './dist/types/' + appEntryIndexAsSrcSubpathNoExt + '.d.ts';
updatePkg.typesVersions = { '>=3.1': { './*': ['./dist/types/*'] } };
} else {
updatePkg.exports = {
'.': {
import: './dist/' + appEntryIndexAsSrcSubpathNoExt + '.js',
Expand Down Expand Up @@ -187,22 +206,6 @@ export default async ({ mode, command, ssrBuild: isSSRBuild }) => {
},
});
}
} /* CMA with a UMD bundle. */ else {
updatePkg.exports = {
'.': {
import: './dist/' + appEntryIndexAsSrcSubpathNoExt + '.js',
require: './dist/' + appEntryIndexAsSrcSubpathNoExt + '.umd.cjs',
types: './dist/types/' + appEntryIndexAsSrcSubpathNoExt + '.d.ts',
},
};
updatePkg.module = './dist/' + appEntryIndexAsSrcSubpathNoExt + '.js';
updatePkg.main = './dist/' + appEntryIndexAsSrcSubpathNoExt + '.umd.cjs';

updatePkg.browser = ['web', 'webw'].includes(targetEnv) ? updatePkg.main : '';
updatePkg.unpkg = updatePkg.main;

updatePkg.types = './dist/types/' + appEntryIndexAsSrcSubpathNoExt + '.d.ts';
updatePkg.typesVersions = { '>=3.1': { './*': ['./dist/types/*'] } };
}
break; // Stop here.
}
Expand Down Expand Up @@ -307,7 +310,7 @@ export default async ({ mode, command, ssrBuild: isSSRBuild }) => {
* Deletes a few files that interfere with apps running on Cloudflare Pages.
*/
if ('build' === command && ['spa', 'mpa'].includes(appType) && ['cfp'].includes(targetEnv)) {
for (const fileOrDir of await $glob.promise(['types', '.env.vault', 'manifest.json', 'index.*'], { cwd: distDir, onlyFiles: false })) {
for (const fileOrDir of await $glob.promise(['types', '.env.vault', 'index.*'], { cwd: distDir, onlyFiles: false })) {
await fsp.rm(fileOrDir, { force: true, recursive: true });
}
}
Expand All @@ -331,6 +334,10 @@ export default async ({ mode, command, ssrBuild: isSSRBuild }) => {
const cfpDefaultHeaders = $cfpꓺhttp.prepareDefaultHeaders({ appType, isC10n: env.APP_IS_C10N || false });
fileContents = fileContents.replace('$$__APP_CFP_DEFAULT_HEADERS__$$', cfpDefaultHeaders);
}
if (['404.html'].includes(fileRelPath)) {
const cfpDefault404 = '<!DOCTYPE html>' + $preactꓺrenderToString($preactꓺ404);
fileContents = fileContents.replace('$$__APP_CFP_DEFAULT_404_HTML__$$', cfpDefault404);
}
if (['_headers', '_redirects', 'robots.txt'].includes(fileRelPath)) {
fileContents = fileContents.replace(/^#[^\n]*\n/gmu, '');
//
Expand Down Expand Up @@ -368,6 +375,20 @@ export default async ({ mode, command, ssrBuild: isSSRBuild }) => {
const plugins = [pluginBasicSSLConfig, pluginEJSConfig, pluginMinifyHTMLConfig, pluginC10NPostProcessConfig];
const importedWorkerPlugins = []; // <https://vitejs.dev/guide/features.html#web-workers>.

/**
* Configures esbuild for Vite.
*
* @see https://o5p.me/XOFuJp
*/
const esbuildConfig = {
// See <https://o5p.me/Wk8Fm9>.
jsxFactory: 'h', // Required preact config.
jsxFragment: 'Fragment',
jsxInject: `import { h, Fragment } from 'preact'`,

legalComments: 'none', // See <https://o5p.me/DZKXwX>.
};

/**
* Configures rollup for Vite.
*
Expand Down Expand Up @@ -440,15 +461,15 @@ export default async ({ mode, command, ssrBuild: isSSRBuild }) => {
},
assetFileNames: (/* asset */) => path.join(path.relative(distDir, a16sDir), '[name]-[hash].[ext]'),

// Preserves module structure in CMAs built explicitly as dependencies.
// Preserves module structure in CMAs built explicitly as libraries.
// The expectation is that peers will build w/ this flag set as false, which is
// recommended, because preserving module structure in a final build has performance costs.
// However, in builds that are not final (e.g., CMAs with peer dependencies), preserving modules
// has performance benefits, as it allows for tree-shaking optimization in final builds.
...(shouldPreserveModules ? { preserveModules: true } : {}),
...(isLib ? { preserveModules: true } : {}),

// Cannot inline dynamic imports when `preserveModules` is enabled, so set as `false` explicitly.
...(shouldPreserveModules ? { inlineDynamicImports: false } : {}),
...(isLib ? { inlineDynamicImports: false } : {}),
},
};
// <https://vitejs.dev/guide/features.html#web-workers>
Expand Down Expand Up @@ -600,17 +621,18 @@ export default async ({ mode, command, ssrBuild: isSSRBuild }) => {
minify: shouldMinify ? 'esbuild' : false, // See: <https://o5p.me/ZyQ4sv>.
...(isTargetEnvSSR ? { modulePreload: { resolveDependencies: () => [] } } : {}),

...(['cma'].includes(appType) // Custom-made apps = library code.
...(isLib // Custom-made app built as a library consumed by others.
? {
lib: {
name: appLibName, // Name of UMD window global var.
entry: appEntries, // Should match up with `rollupOptions.input`.
formats: isSSRBuild ? ['es'] : appEntries.length > 1 ? ['es', 'cjs'] : ['es', 'umd'],
formats: isSSRBuild ? ['es'] : 1 === appEntries.length ? ['es', 'umd'] : ['es', 'cjs'],
},
}
: {}),
rollupOptions: rollupConfig, // See: <https://o5p.me/5Vupql>.
},
esbuild: esbuildConfig, // esBuild config options.
test: vitestConfig, // Vitest configuration options.
};

Expand Down
Loading

0 comments on commit d01a483

Please sign in to comment.