Skip to content

Commit

Permalink
Merge pull request #201 from chialab/support-entrypoints-map
Browse files Browse the repository at this point in the history
Support entrypoints map
  • Loading branch information
edoardocavazza authored Nov 4, 2024
2 parents 3d55727 + 3b6bd21 commit b4fc48d
Show file tree
Hide file tree
Showing 25 changed files with 2,727 additions and 2,821 deletions.
7 changes: 7 additions & 0 deletions .changeset/clever-mirrors-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@chialab/rna-config-loader': patch
'@chialab/wds-plugin-rna': patch
'@chialab/rna-bundler': patch
---

Add support for mapped entrypoints like esbuild does.
6 changes: 6 additions & 0 deletions .changeset/nasty-flies-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@chialab/esbuild-plugin-html': patch
'@chialab/wds-plugin-legacy': patch
---

Use stable cheerio.
5 changes: 5 additions & 0 deletions .changeset/shiny-cycles-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@chialab/rna-bundler': patch
---

Update esbuild constraint.
2 changes: 1 addition & 1 deletion packages/esbuild-plugin-env/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"node": ">=18"
},
"devDependencies": {
"esbuild": "^0.23.0",
"esbuild": "^0.24.0",
"typescript": "^5.0.0"
}
}
2 changes: 1 addition & 1 deletion packages/esbuild-plugin-html/lib/collectAssets.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { isRelativeUrl } from './utils.js';

/**
* @param {import('cheerio').CheerioAPI} $ The cheerio selector.
* @param {import('cheerio').Cheerio<import('cheerio').Element>} element The DOM element.
* @param {import('cheerio').Cheerio<import('domhandler').Element>} element The DOM element.
* @param {string} attribute The element attribute to load.
* @param {import('./index.js').BuildOptions} options Build options.
* @param {import('./index.js').Helpers} helpers Helpers.
Expand Down
2 changes: 1 addition & 1 deletion packages/esbuild-plugin-html/lib/collectIcons.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ async function generateAppleIcons(image, icons) {

/**
* @param {import('cheerio').CheerioAPI} $ The cheerio selector.
* @param {import('cheerio').Cheerio<import('cheerio').Element>} element The DOM element.
* @param {import('cheerio').Cheerio<import('domhandler').Element>} element The DOM element.
* @param {Icon} icon The generated icon file.
* @param {string} rel Rel attribute.
* @param {boolean} shortcut Should include shortcut.
Expand Down
2 changes: 1 addition & 1 deletion packages/esbuild-plugin-html/lib/collectScreens.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ async function generateAppleLaunchScreens(image, launchScreens) {

/**
* @param {import('cheerio').CheerioAPI} $ The cheerio selector.
* @param {import('cheerio').Cheerio<import('cheerio').Element>} element The DOM element.
* @param {import('cheerio').Cheerio<import('domhandler').Element>} element The DOM element.
* @param {Screen} screen The generated screen file.
* @param {import('./index.js').BuildOptions} options Build options.
* @param {import('./index.js').Helpers} helpers Helpers.
Expand Down
8 changes: 4 additions & 4 deletions packages/esbuild-plugin-html/lib/collectScripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { isRelativeUrl } from './utils.js';

/**
* @param {import('cheerio').CheerioAPI} $ The cheerio selector.
* @param {import('cheerio').Cheerio<import('cheerio').Document>} dom The DOM element.
* @param {import('cheerio').Element[]} elements List of nodes.
* @param {import('cheerio').Cheerio<import('domhandler').Document>} dom The DOM element.
* @param {import('domhandler').Element[]} elements List of nodes.
* @param {string|string[]} target Build target.
* @param {import('esbuild').Format} format Build format.
* @param {string} type Script type.
Expand All @@ -21,12 +21,12 @@ async function innerCollect($, dom, elements, target, format, type, attrs = {},
}

/**
* @type {Map<import('cheerio').Element, import('@chialab/esbuild-rna').VirtualEntry|string>}
* @type {Map<import('domhandler').Element, import('@chialab/esbuild-rna').VirtualEntry|string>}
*/
const builds = new Map();

/**
* @type {Map<string, import('cheerio').Element>}
* @type {Map<string, import('domhandler').Element>}
*/
const entrypoints = new Map();

Expand Down
4 changes: 2 additions & 2 deletions packages/esbuild-plugin-html/lib/collectStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ export async function collectStyles($, dom, options, helpers) {
}

/**
* @type {Map<import('cheerio').Element, import('@chialab/esbuild-rna').VirtualEntry|string>}
* @type {Map<import('domhandler').Element, import('@chialab/esbuild-rna').VirtualEntry|string>}
*/
const builds = new Map();

/**
* @type {Map<string, import('cheerio').Element>}
* @type {Map<string, import('domhandler').Element>}
*/
const entrypoints = new Map();

Expand Down
11 changes: 3 additions & 8 deletions packages/esbuild-plugin-html/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@ import { Buffer } from 'buffer';
import { copyFile, readFile, rm } from 'fs/promises';
import path from 'path';
import { Build, useRna } from '@chialab/esbuild-rna';
import * as cheerio from 'cheerio';
import { load } from 'cheerio';
import beautify from 'js-beautify';

/**
* @typedef {import('esbuild').Metafile} Metafile
*/

/**
* Cheerio esm support is unstable for some Node versions.
*/
const loadHtml = /** @type {typeof cheerio.load} */ (cheerio.load || cheerio.default?.load);

/**
* @typedef {Object} PluginOptions
* @property {string} [scriptsTarget]
Expand Down Expand Up @@ -54,7 +49,7 @@ const loadHtml = /** @type {typeof cheerio.load} */ (cheerio.load || cheerio.def
*/

/**
* @typedef {($: import('cheerio').CheerioAPI, dom: import('cheerio').Cheerio<import('cheerio').Document>, options: CollectOptions<T>, helpers: Helpers) => Promise<import('@chialab/esbuild-rna').OnTransformResult[]>} Collector
* @typedef {($: import('cheerio').CheerioAPI, dom: import('cheerio').Cheerio<import('domhandler').Document>, options: CollectOptions<T>, helpers: Helpers) => Promise<import('@chialab/esbuild-rna').OnTransformResult[]>} Collector
* @template {object} T
*/

Expand Down Expand Up @@ -215,7 +210,7 @@ export default function ({
]);

const code = await preprocess(args.code, args.path);
const $ = loadHtml(code);
const $ = load(code);
const root = $.root();
let count = 0;

Expand Down
4 changes: 2 additions & 2 deletions packages/esbuild-plugin-html/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
"@jimp/plugin-resize": "^0.22.0",
"@jimp/png": "^0.22.0",
"@types/js-beautify": "^1.13.3",
"cheerio": "^1.0.0-rc.12",
"esbuild": "^0.23.0",
"cheerio": "^1.0.0",
"esbuild": "^0.24.0",
"handlebars": "^4.7.8",
"htmlnano": "^2.1.0",
"js-beautify": "^1.14.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/esbuild-plugin-meta-url/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"devDependencies": {
"@types/mime-types": "^2.1.1",
"esbuild": "^0.23.0",
"esbuild": "^0.24.0",
"typescript": "^5.0.0"
}
}
2 changes: 1 addition & 1 deletion packages/esbuild-plugin-metadata/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"node": ">=18"
},
"devDependencies": {
"esbuild": "^0.23.0",
"esbuild": "^0.24.0",
"typescript": "^5.0.0"
}
}
2 changes: 1 addition & 1 deletion packages/esbuild-plugin-postcss/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"devDependencies": {
"@chialab/postcss-dart-sass": "^0.19.0-beta.1",
"cssnano": "^6.0.1",
"esbuild": "^0.23.0",
"esbuild": "^0.24.0",
"postcss-scss": "^4.0.0",
"typescript": "^5.0.0"
}
Expand Down
6 changes: 3 additions & 3 deletions packages/esbuild-rna/lib/Build.js
Original file line number Diff line number Diff line change
Expand Up @@ -1101,14 +1101,14 @@ export class Build {
format,
outdir: options.outdir ? path.resolve(virtualOutDir, options.outdir) : this.getFullOutDir(),
bundle: options.bundle ?? buildOptions.bundle,
splitting: format === 'esm' ? options.splitting ?? buildOptions.splitting : false,
splitting: format === 'esm' ? (options.splitting ?? buildOptions.splitting) : false,
platform: options.platform ?? buildOptions.platform,
target: options.target ?? buildOptions.target,
plugins: options.plugins ?? buildOptions.plugins,
external: options.external ?? this.getInitialOptions().external,
jsxFactory: 'jsxFactory' in options ? options.jsxFactory : buildOptions.jsxFactory,
entryNames,
write: buildOptions.write !== false ? options.write ?? true : false,
write: buildOptions.write !== false ? (options.write ?? true) : false,
globalName: undefined,
outfile: undefined,
metafile: true,
Expand Down Expand Up @@ -1184,7 +1184,7 @@ export class Build {
format,
outdir: options.outdir ? this.resolvePath(options.outdir) : this.getFullOutDir(),
bundle: options.bundle ?? buildOptions.bundle,
splitting: format === 'esm' ? options.splitting ?? buildOptions.splitting ?? true : false,
splitting: format === 'esm' ? (options.splitting ?? buildOptions.splitting ?? true) : false,
platform: options.platform ?? buildOptions.platform,
target: options.target ?? buildOptions.target,
plugins: options.plugins ?? buildOptions.plugins,
Expand Down
2 changes: 1 addition & 1 deletion packages/esbuild-rna/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"@chialab/node-resolve": "^0.19.0-beta.1"
},
"devDependencies": {
"esbuild": "^0.23.0",
"esbuild": "^0.24.0",
"typescript": "^5.0.0"
}
}
5 changes: 3 additions & 2 deletions packages/rna-bundler/lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from 'path';
import process from 'process';
import { hasPlugin } from '@chialab/esbuild-rna';
import esbuild from 'esbuild';
import { resolveSourceFile } from './helpers.js';
import { loaders } from './loaders.js';

/**
Expand Down Expand Up @@ -68,10 +69,10 @@ export async function build(config) {
entryOptions.stdin = {
contents: code,
resolveDir: rootDir,
sourcefile: Array.isArray(input) ? input[0] : input,
sourcefile: resolveSourceFile(input),
};
} else if (input) {
entryOptions.entryPoints = Array.isArray(input) ? input : [input];
entryOptions.entryPoints = typeof input === 'string' ? [input] : input;
}

const outputDir = hasOutputFile ? path.dirname(output) : output;
Expand Down
16 changes: 16 additions & 0 deletions packages/rna-bundler/lib/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Resolve the first source file from the input config.
* @param {import('@chialab/rna-config-loader').Input} input
*/
export function resolveSourceFile(input) {
if (typeof input === 'string') {
return input;
}
if (Array.isArray(input)) {
if (typeof input[0] === 'string') {
return input[0];
}
return input[0].in;
}
return Object.values(input)[0];
}
3 changes: 2 additions & 1 deletion packages/rna-bundler/lib/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from 'path';
import process from 'process';
import { hasPlugin } from '@chialab/esbuild-rna';
import esbuild from 'esbuild';
import { resolveSourceFile } from './helpers.js';
import { transformLoaders } from './loaders.js';

/**
Expand Down Expand Up @@ -73,7 +74,7 @@ export async function transform(config) {
)
);

const sourceFile = path.resolve(rootDir, Array.isArray(input) ? input[0] : input);
const sourceFile = path.resolve(rootDir, resolveSourceFile(input));
const absWorkingDir = path.dirname(sourceFile);
const result = /** @type {import('@chialab/esbuild-rna').Result} */ (
await esbuild.build({
Expand Down
2 changes: 1 addition & 1 deletion packages/rna-bundler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"@chialab/esbuild-rna": "^0.19.0-beta.2",
"@chialab/rna-config-loader": "^0.19.0-beta.0",
"@chialab/rna-logger": "^0.19.0-beta.0",
"esbuild": "^0.21.0 || ^0.22.0 || ^0.23.0"
"esbuild": "^0.21.0 || ^0.22.0 || ^0.23.0 || ^0.24.0"
},
"devDependencies": {
"typescript": "^5.0.0"
Expand Down
35 changes: 8 additions & 27 deletions packages/rna-config-loader/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import path from 'path';
import process from 'process';
import { pathToFileURL } from 'url';

/**
* @typedef {string | string[] | Record<string, string> | { in: string, out: string }[]} Input
*/

/**
* @typedef {import('esbuild').BuildOptions} BuildOptions
*/
Expand Down Expand Up @@ -55,7 +59,7 @@ import { pathToFileURL } from 'url';

/**
* @typedef {Object} RnaEntrypointConfig
* @property {string|string[]} input
* @property {Input} input
* @property {string} [output]
* @property {string} [name]
* @property {string} [code]
Expand All @@ -75,23 +79,10 @@ import { pathToFileURL } from 'url';
* @typedef {BuildOptions & RnaConfig & RnaProjectConfig} ProjectConfig
*/

/**
* Convert a file path to CamelCase.
*
* @param {string} file The file path.
* @returns {string}
*/
export function camelize(file) {
const filename = path.basename(file, path.extname(file));
return filename
.replace(/(^[a-z0-9]|[-_]([a-z0-9]))/g, (g) => (g[1] || g[0]).toUpperCase())
.replace(/[^a-zA-Z0-9]/g, '');
}

/**
* @param {EntrypointConfig} entrypoint
* @param {ProjectConfig} config
* @returns {ProjectConfig & { input: string|string[] }}
* @returns {ProjectConfig & { input: Input }}
*/
export function getEntryConfig(entrypoint, config) {
const root = entrypoint.root || config.root || process.cwd();
Expand All @@ -117,12 +108,7 @@ export function getEntryConfig(entrypoint, config) {
minify: entrypoint.minify ?? config.minify ?? false,
clean: entrypoint.clean ?? config.clean ?? false,
splitting: entrypoint.splitting ?? config.splitting,
globalName:
entrypoint.globalName ||
entrypoint.name ||
(format === 'iife'
? camelize(Array.isArray(entrypoint.input) ? entrypoint.input[0] : entrypoint.input)
: undefined),
globalName: entrypoint.globalName || entrypoint.name,
entryNames: entrypoint.entryNames || config.entryNames || '[dir]/[name]',
chunkNames: entrypoint.chunkNames || config.chunkNames || '[name]-[hash]',
assetNames: entrypoint.assetNames || config.assetNames || '[name]-[hash]',
Expand Down Expand Up @@ -157,16 +143,11 @@ export function getEntryBuildConfig(entrypoint, config) {
throw new Error('Missing required `output` path');
}

const format = entrypoint.format || config.format;

return /** @type {EntrypointConfig} */ (
getEntryConfig(
{
...entrypoint,
globalName:
entrypoint.globalName ||
entrypoint.name ||
(format === 'iife' ? camelize(entrypoint.output) : undefined),
globalName: entrypoint.globalName || entrypoint.name,
},
config
)
Expand Down
7 changes: 1 addition & 6 deletions packages/wds-plugin-legacy/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@ import crypto from 'crypto';
import { createRequire } from 'module';
import { createHelperUrl, isPlainScript } from '@chialab/wds-plugin-node-resolve';
import { inject } from '@chialab/wds-plugin-polyfill';
import * as cheerio from 'cheerio';
import { load } from 'cheerio';
import { checkEsmSupport } from './checkEsmSupport.js';
import { readFile } from './readFile.js';
import { transform } from './transform.js';

const require = createRequire(import.meta.url);

/**
* Cheerio esm support is unstable for some Node versions.
*/
const load = /** typeof cheerio.load */ cheerio.load || cheerio.default?.load;

/**
* Create an hash for the given buffer.
* @param {Buffer|Uint8Array|string} buffer The buffer input.
Expand Down
2 changes: 1 addition & 1 deletion packages/wds-plugin-legacy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"@chialab/wds-plugin-node-resolve": "^0.19.0-beta.0",
"@chialab/wds-plugin-polyfill": "^0.19.0-beta.0",
"browserslist-useragent": "^4.0.0",
"cheerio": "^1.0.0-rc.9",
"cheerio": "^1.0.0",
"regenerator-runtime": "^0.14.0",
"systemjs": "^6.9.0"
},
Expand Down
Loading

0 comments on commit b4fc48d

Please sign in to comment.