Skip to content

Commit

Permalink
New: Add option to set file extension
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnitto committed Jan 18, 2023
1 parent a216c76 commit eb94cb8
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 34 deletions.
29 changes: 16 additions & 13 deletions Installer/Distribution/Defaults/pipeline.yaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
# A package entry can have the following options:

# package <string> (required) The name of the package
# files <string|array> The names of the entry files. If none given, all parsable files in the input folder get rendered
# folder.input <string> (default: `Fusion`) The folder under `Resources/Private` where to look for the entry files
# folder.output.package <string|array> If set, the files will be writen in a different package (one or multiple packages)
# folder.output.inline <string> (default: `Private/Templates/InlineAssets`) The folder where inline files get rendered
# folder.output.style <string> (default: `Public/Styles`) The folder where styles get rendered
# folder.output.script <string> (default: `Public/Scripts`) The folder where scripts get rendered
# folder.output.module <string> (default: `Public/Modules`) The folder where modules files get rendered
# folder.output.commonJS <string> (default: `Public/CommonJS`) The folder where commonJS files get rendered
# external <string|array> (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 <boolean> (default: `false`) Flag to toggle if the files should be written to `Resources/Private/Templates/InlineAssets`
# sourcemap <boolean> (default: `true`) Flag to toggle source map generation
# format <string> (default: `iife`)
# package <string> (required) The name of the package
# files <string|array> The names of the entry files. If none given, all parsable files in the input folder get rendered
# folder.input <string> (default: `Fusion`) The folder under `Resources/Private` where to look for the entry files
# folder.output.package <string|array> If set, the files will be writen in a different package (one or multiple packages)
# folder.output.inline <string> (default: `Private/Templates/InlineAssets`) The folder where inline files get rendered
# folder.output.style <string> (default: `Public/Styles`) The folder where styles get rendered
# folder.output.script <string> (default: `Public/Scripts`) The folder where scripts get rendered
# folder.output.module <string> (default: `Public/Modules`) The folder where modules files get rendered
# folder.output.commonJS <string> (default: `Public/CommonJS`) The folder where commonJS files get rendered
# external <string|array> (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 <boolean> (default: `false`) Flag to toggle if the files should be written to `Resources/Private/Templates/InlineAssets`
# sourcemap <boolean> (default: `true`) Flag to toggle source map generation
# format <string> (default: `iife`)
# jsFileExtension.script <string> (default: `.js`) Extension for script files. If you set `jsFileExtension` to `false`, it will set to `.js`
# jsFileExtension.module <string> (default: `.mjs`) Extension for module files. If you set `jsFileExtension` to `false`, it will set to `.js`
# jsFileExtension.commonJS <string> (default: `.cjs`) Extension for commonJS files. If you set `jsFileExtension` to `false`, it will set to `.js`

packages:
- package: Vendor.Bar
Expand Down
6 changes: 6 additions & 0 deletions Lib/helper.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -164,6 +169,7 @@ function entryConfig(entry, type) {
sourcemap,
outdir,
inline,
extension,
};
}

Expand Down
42 changes: 25 additions & 17 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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:

Expand All @@ -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`]:
Expand Down
7 changes: 6 additions & 1 deletion defaults.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions esbuild.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,7 +23,7 @@ async function build() {
target: browserlist,
outdir: firstOutdir,
outExtension: {
".js": jsExtension,
".js": extension,
},
define: {
...flowSettings,
Expand Down

0 comments on commit eb94cb8

Please sign in to comment.