From eb94cb82464b6f93f26f7740902cd847f1ac16fd Mon Sep 17 00:00:00 2001 From: Jon Uhlmann Date: Wed, 18 Jan 2023 16:43:29 +0100 Subject: [PATCH] New: Add option to set file extension --- Installer/Distribution/Defaults/pipeline.yaml | 29 +++++++------ Lib/helper.mjs | 6 +++ Readme.md | 42 +++++++++++-------- defaults.yaml | 7 +++- esbuild.mjs | 5 +-- 5 files changed, 55 insertions(+), 34 deletions(-) diff --git a/Installer/Distribution/Defaults/pipeline.yaml b/Installer/Distribution/Defaults/pipeline.yaml index 3687f14..cd2d5e8 100644 --- a/Installer/Distribution/Defaults/pipeline.yaml +++ b/Installer/Distribution/Defaults/pipeline.yaml @@ -1,18 +1,21 @@ # A package entry can have the following options: -# package (required) The name of the package -# files The names of the entry files. If none given, all parsable files in the input folder get rendered -# folder.input (default: `Fusion`) The folder under `Resources/Private` where to look for the entry files -# folder.output.package If set, the files will be writen in a different package (one or multiple packages) -# folder.output.inline (default: `Private/Templates/InlineAssets`) The folder where inline files get rendered -# folder.output.style (default: `Public/Styles`) The folder where styles get rendered -# folder.output.script (default: `Public/Scripts`) The folder where scripts get rendered -# folder.output.module (default: `Public/Modules`) The folder where modules files get rendered -# folder.output.commonJS (default: `Public/CommonJS`) The folder where commonJS files get rendered -# external (default: `null`) You can mark a file or a package as external to exclude it from your build. https://esbuild.github.io/api/#external -# inline (default: `false`) Flag to toggle if the files should be written to `Resources/Private/Templates/InlineAssets` -# sourcemap (default: `true`) Flag to toggle source map generation -# format (default: `iife`) +# package (required) The name of the package +# files The names of the entry files. If none given, all parsable files in the input folder get rendered +# folder.input (default: `Fusion`) The folder under `Resources/Private` where to look for the entry files +# folder.output.package If set, the files will be writen in a different package (one or multiple packages) +# folder.output.inline (default: `Private/Templates/InlineAssets`) The folder where inline files get rendered +# folder.output.style (default: `Public/Styles`) The folder where styles get rendered +# folder.output.script (default: `Public/Scripts`) The folder where scripts get rendered +# folder.output.module (default: `Public/Modules`) The folder where modules files get rendered +# folder.output.commonJS (default: `Public/CommonJS`) The folder where commonJS files get rendered +# external (default: `null`) You can mark a file or a package as external to exclude it from your build. https://esbuild.github.io/api/#external +# inline (default: `false`) Flag to toggle if the files should be written to `Resources/Private/Templates/InlineAssets` +# sourcemap (default: `true`) Flag to toggle source map generation +# format (default: `iife`) +# jsFileExtension.script (default: `.js`) Extension for script files. If you set `jsFileExtension` to `false`, it will set to `.js` +# jsFileExtension.module (default: `.mjs`) Extension for module files. If you set `jsFileExtension` to `false`, it will set to `.js` +# jsFileExtension.commonJS (default: `.cjs`) Extension for commonJS files. If you set `jsFileExtension` to `false`, it will set to `.js` packages: - package: Vendor.Bar diff --git a/Lib/helper.mjs b/Lib/helper.mjs index e7a1beb..e44870c 100644 --- a/Lib/helper.mjs +++ b/Lib/helper.mjs @@ -153,6 +153,11 @@ function entryConfig(entry, type) { const sourcemap = inline ? false : getValue(entry, "sourcemap"); const outputFolderKey = inline ? "inline" : type; const folderOutput = entry.folder?.output; + + const defaultExtensions = config.buildDefaults.jsFileExtension; + const extensions = getValue(entry, "jsFileExtension"); + const extension = extensions ? extensions[type] || defaultExtensions[type] || ".js" : ".js"; + let outputFolder = config.folder.output[outputFolderKey]; let packageName = entry.package; if (folderOutput) { @@ -164,6 +169,7 @@ function entryConfig(entry, type) { sourcemap, outdir, inline, + extension, }; } diff --git a/Readme.md b/Readme.md index 2c0b48d..93f2428 100644 --- a/Readme.md +++ b/Readme.md @@ -30,8 +30,8 @@ Carbon.Pipeline assumes the following project directory structure for resources: A configured location under -* `Resources/Private`: input files -* `Resources/`: output files +- `Resources/Private`: input files +- `Resources/`: output files The whole configuration, including which files to build, is configured in [`pipeline.yaml`]. The default values are set in [`defaults.yaml`] and merged with your configuration. Under the key `packages`, you can either add an array with package settings or, if you have just one entry, you can directly add the configuration: @@ -104,21 +104,25 @@ packages: A package entry has the following options: -| Key | Type | Description | Example | -| ------------------------ | ------------------- | ------------------------------------------------------------------------------------------------ | -------------------- | -| `package` | `string` | The name of the package **(required)** | `Vendor.Foo` | -| `files` | `string` or `array` | The names of the entry files. If none given, all parsable files in the input folder get rendered | `Main.js` | -| `folder.input` | `string` | The folder under `Resources/Private` where to look for the entry files | `Assets` | -| `folder.output.package` | `string` or `array` | If set, the files will be writen in a different package (one or multiple) | `Foo.Bar` | -| `folder.output.inline` | `string` | The folder where inline files get rendered | `Private/Templates/` | -| `folder.output.style` | `string` | The folder where inline styles rendered | `Public/Assets` | -| `folder.output.script` | `string` | The folder where inline scripts rendered | `Public/Assets` | -| `folder.output.module` | `string` | The folder where inline modules rendered | `Public/Assets` | -| `folder.output.commonJS` | `string` | The folder where inline commonJS files get rendered | `Public/Assets` | -| `external` | `string` or `array` | You can mark a file or a package as [external] to exclude it from your build. | `*/Modules/*` | -| `inline` | `boolean` | Flag to toggle if the files should be inlined. If set, sourcemaps are disabled | `true` | -| `sourcemap` | `boolean` | Flag to toggle source map generation | `false` | -| `format` | `string` | Set the format of the output file. [Read more][esbuild format] | `cjs` | +| Key | Type | Description | Example | +| -------------------------- | ------------------- | ------------------------------------------------------------------------------------------------ | -------------------- | +| `package` | `string` | The name of the package **(required)** | `Vendor.Foo` | +| `files` | `string` or `array` | The names of the entry files. If none given, all parsable files in the input folder get rendered | `Main.js` | +| `folder.input` | `string` | The folder under `Resources/Private` where to look for the entry files | `Assets` | +| `folder.output.package` | `string` or `array` | If set, the files will be writen in a different package (one or multiple) | `Foo.Bar` | +| `folder.output.inline` | `string` | The folder where inline files get rendered | `Private/Templates/` | +| `folder.output.style` | `string` | The folder where inline styles rendered | `Public/Assets` | +| `folder.output.script` | `string` | The folder where inline scripts rendered | `Public/Assets` | +| `folder.output.module` | `string` | The folder where inline modules rendered | `Public/Assets` | +| `folder.output.commonJS` | `string` | The folder where inline commonJS files get rendered | `Public/Assets` | +| `external` | `string` or `array` | You can mark a file or a package as [external] to exclude it from your build. | `*/Modules/*` | +| `inline` | `boolean` | Flag to toggle if the files should be inlined. If set, sourcemaps are disabled | `true` | +| `sourcemap` | `boolean` | Flag to toggle source map generation | `false` | +| `format` | `string` | Set the format of the output file. [Read more][esbuild format] | `cjs` | +| `jsFileExtension` | `array` or `false` | File extensions of javascript files. If set to `false`, all files will have the extension `.js` | `false` | +| `jsFileExtension.script` | `string` | File extension of script files | `.js` | +| `jsFileExtension.module` | `string` | Output extension of module JS files. | `.module` | +| `jsFileExtension.commonJS` | `string` | Output extension of common JS files. | `.common` | These are the default values for the folders: @@ -140,6 +144,10 @@ external: null inline: false sourcemap: true format: iife +jsFileExtension: + script: .js + module: .mjs + commonJS: .cjs ``` The target folders can be adjusted under the key `folder.output`. If you want to change the defaults for all your packages, you can also set this globally in your [`pipeline.yaml`]: diff --git a/defaults.yaml b/defaults.yaml index 321287a..d886bec 100644 --- a/defaults.yaml +++ b/defaults.yaml @@ -40,7 +40,12 @@ buildDefaults: inline: false sourcemap: true format: iife - # external an be a string or an array + jsFileExtension: + script: .js + module: .mjs + commonJS: .cjs + + # external can be a string or an array # https://esbuild.github.io/api/#external external: null # purge can be a string or an array diff --git a/esbuild.mjs b/esbuild.mjs index 9ef4c02..4c2366e 100644 --- a/esbuild.mjs +++ b/esbuild.mjs @@ -5,8 +5,7 @@ import { browserlist, options, importPlugins, flowSettings } from "./Lib/esbuild async function build() { // Pre-import plugins const plugins = await importPlugins(); - await asyncForEach(files, async ({ entryPoints, sourcemap, outdir, format, external, inline }) => { - const jsExtension = format === "esm" ? ".mjs" : format === "cjs" ? ".cjs" : ".js"; + await asyncForEach(files, async ({ entryPoints, sourcemap, outdir, format, external, inline, extension }) => { const firstOutdir = outdir[0]; const multiplePackages = outdir.length > 1; const write = !compression || inline; @@ -24,7 +23,7 @@ async function build() { target: browserlist, outdir: firstOutdir, outExtension: { - ".js": jsExtension, + ".js": extension, }, define: { ...flowSettings,