diff --git a/.changeset/tough-parrots-travel.md b/.changeset/tough-parrots-travel.md new file mode 100644 index 00000000..aeb0f16a --- /dev/null +++ b/.changeset/tough-parrots-travel.md @@ -0,0 +1,6 @@ +--- +"@chialab/esbuild-rna": patch +"@chialab/rna-config-loader": patch +--- + +Fix entry names config. diff --git a/packages/esbuild-plugin-meta-url/lib/index.js b/packages/esbuild-plugin-meta-url/lib/index.js index 181b6533..d4d8cd13 100644 --- a/packages/esbuild-plugin-meta-url/lib/index.js +++ b/packages/esbuild-plugin-meta-url/lib/index.js @@ -241,7 +241,7 @@ export default function({ emit = true } = {}) { } } - helpers.overwrite(startToken.start, endToken.end, `new URL('./${entryPoint}', ${baseUrl})`); + helpers.overwrite(startToken.start, endToken.end, `new URL('./${entryPoint.split(path.sep).join('/')}', ${baseUrl})`); return; } diff --git a/packages/esbuild-plugin-meta-url/test/test.spec.js b/packages/esbuild-plugin-meta-url/test/test.spec.js index 5fb946e9..4b42f064 100644 --- a/packages/esbuild-plugin-meta-url/test/test.spec.js +++ b/packages/esbuild-plugin-meta-url/test/test.spec.js @@ -16,6 +16,7 @@ describe('esbuild-plugin-meta-url', () => { }, format: 'esm', outdir: 'out', + assetNames: '[name]', loader: { '.txt': 'file', }, @@ -102,7 +103,7 @@ export const file = new URL(fileName, import.meta.url);`, }); expect(result.text).to.be.equal(`// test.spec.js -var file = new URL("./file.txt?hash=4e1243bd", import.meta.url); +var file = new URL("./file-4e1243bd.txt?hash=4e1243bd", import.meta.url); export { file }; @@ -159,7 +160,7 @@ export { expect(result.text).to.be.equal(`(() => { var __currentScriptUrl__ = document.currentScript && document.currentScript.src || document.baseURI; - var file = new URL("./file.txt?hash=4e1243bd", __currentScriptUrl__); + var file = new URL("./file-4e1243bd.txt?hash=4e1243bd", __currentScriptUrl__); })(); `); expect(file.text).to.be.equal('test\n'); @@ -211,7 +212,7 @@ __export(test_spec_exports, { file: () => file }); module.exports = __toCommonJS(test_spec_exports); -var file = new URL("./file.txt?hash=4e1243bd", "file://" + __filename); +var file = new URL("./file-4e1243bd.txt?hash=4e1243bd", "file://" + __filename); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { file @@ -234,6 +235,7 @@ var file = new URL("./file.txt?hash=4e1243bd", "file://" + __filename); loader: { '.txt': 'file', }, + assetNames: '[name]', bundle: true, write: false, plugins: [ @@ -352,4 +354,36 @@ export { })(); `); }); + + it('should load a file outside and src into the dist folder', async () => { + const { outputFiles: [result, file] } = await esbuild.build({ + absWorkingDir: fileURLToPath(new URL('.', import.meta.url)), + stdin: { + resolveDir: fileURLToPath(new URL('src/', import.meta.url)), + sourcefile: fileURLToPath(new URL('src/index.js', import.meta.url)), + contents: 'export const file = new URL(\'../file.txt\', import.meta.url);', + }, + format: 'esm', + outdir: 'out', + outbase: fileURLToPath(new URL('src/', import.meta.url)), + assetNames: '[dir]/[name]-[hash]', + loader: { + '.txt': 'file', + }, + bundle: true, + write: false, + plugins: [ + metaUrl(), + ], + }); + + expect(result.text).to.be.equal(`// src/index.js +var file = new URL("./_.._/file-4e1243bd.txt?hash=4e1243bd", import.meta.url); +export { + file +}; +`); + expect(file.text).to.be.equal('test\n'); + expect(path.join(path.dirname(result.path), '_.._')).to.be.equal(path.dirname(file.path)); + }); }); diff --git a/packages/esbuild-plugin-worker/lib/index.js b/packages/esbuild-plugin-worker/lib/index.js index 746be034..1b0e5342 100644 --- a/packages/esbuild-plugin-worker/lib/index.js +++ b/packages/esbuild-plugin-worker/lib/index.js @@ -202,7 +202,7 @@ export default function({ constructors = ['Worker', 'SharedWorker'], proxy = fal importer: args.path, namespace: 'file', resolveDir: path.dirname(args.path), - pluginData: undefined, + pluginData: null, }); if (external) { diff --git a/packages/esbuild-rna/lib/Build.js b/packages/esbuild-rna/lib/Build.js index 88a04c29..395a18ba 100644 --- a/packages/esbuild-rna/lib/Build.js +++ b/packages/esbuild-rna/lib/Build.js @@ -827,18 +827,22 @@ export class Build { return `${pattern .replace('[name]', () => path.basename(inputFile, path.extname(inputFile))) .replace('[ext]', () => path.extname(inputFile)) - .replace(/(\/)?\[dir\](\/)?/, (fullMatch, match1, match2) => { - const dir = path.relative(outBase, path.dirname(filePath)); - if (dir) { - return `${match1 || ''}${dir}${match2 || ''}`; + .replace('[hash]', () => this.hash(buffer)) + .split('/') + .reduce((parts, part) => { + if (part === '[dir]') { + return [...parts, ...(path.relative(outBase, path.dirname(filePath)) || '').split(path.sep)]; } - if (!match1 && match2) { - return ''; + return [...parts, part]; + }, /** @type {string[]} */ ([])) + .map((part) => { + if (part === '..') { + return '_.._'; } - return match1 || ''; + return part; }) - .replace('[dir]', () => path.relative(outBase, path.dirname(filePath))) - .replace('[hash]', () => this.hash(buffer)) + .filter((part) => part && part !== '.') + .join('/') }${path.extname(inputFile)}`; } @@ -917,7 +921,7 @@ export class Build { return path.resolve( this.getWorkingDir(), this.getOutDir() || this.getSourceRoot(), - this.computeName(this.getOption(key) || '[dir]/[name]', filePath, buffer) + this.computeName(this.getOption(key) || (type === Build.ASSET ? '[name]-[hash]' : '[name]'), filePath, buffer) ); } diff --git a/packages/rna-config-loader/lib/index.js b/packages/rna-config-loader/lib/index.js index 1d128433..57b5b797 100644 --- a/packages/rna-config-loader/lib/index.js +++ b/packages/rna-config-loader/lib/index.js @@ -119,8 +119,8 @@ export function getEntryConfig(entrypoint, config) { splitting: entrypoint.splitting ?? config.splitting, globalName: entrypoint.globalName || entrypoint.name || (format === 'iife' ? camelize(Array.isArray(entrypoint.input) ? entrypoint.input[0] : entrypoint.input) : undefined), entryNames: entrypoint.entryNames || config.entryNames || '[dir]/[name]', - chunkNames: entrypoint.chunkNames || config.chunkNames || '[dir]/[name]', - assetNames: entrypoint.assetNames || config.assetNames || '[dir]/[name]', + chunkNames: entrypoint.chunkNames || config.chunkNames || '[name]-[hash]', + assetNames: entrypoint.assetNames || config.assetNames || '[name]-[hash]', define: { ...(entrypoint.define || {}), ...(config.define || {}),