Skip to content

Commit

Permalink
Prefer “UMD” in documentation (CommonJS, AMD compatible)
Browse files Browse the repository at this point in the history
  • Loading branch information
colinrotherham committed Mar 31, 2023
1 parent b1af2b3 commit f8a3a20
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
2 changes: 1 addition & 1 deletion docs/contributing/coding-standards/js.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export function exampleHelper2 () {}

You must specify the file extension when using the import keyword.

Avoid using namespace imports (`import * as namespace`) in code transpiled to CommonJS (or AMD) bundled code as this can prevent "tree shaking" optimisations.
Avoid using namespace imports (`import * as namespace`) in code transpiled to UMD (or AMD, CommonJS) bundled code as this can prevent "tree shaking" optimisations.

Prefer named exports over default exports to avoid compatibility issues with transpiler "synthetic default" as discussed in: https://github.com/alphagov/govuk-frontend/issues/2829

Expand Down
17 changes: 7 additions & 10 deletions docs/contributing/tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ npm scripts are defined in `package.json`. These trigger a number of Gulp tasks.

**`npm start` will trigger `npm run dev --workspace app` that will:**

- clean the `./app/dist` folder
- copy fonts and images
- compile JavaScript and Sass, including documentation
- compile again when `.scss` and `.mjs` files change
- runs `npm run serve --workspace app`
- runs tasks from `npm run build:app`
- starts up Express, restarting when `.mjs`, `.json` or `.yaml` files change
- compile again when frontend `.mjs` and `.scss` files change

**`npm test` will do the following:**

Expand All @@ -31,8 +29,7 @@ npm scripts are defined in `package.json`. These trigger a number of Gulp tasks.
- clean the `./app/dist` folder
- output files into `./app/dist`
- copy fonts and images
- compile Sass to CSS, including documentation
- compile JavaScript ESM to CommonJS, including documentation
- compile JavaScript and Sass, including documentation

**`npm run build:package` will do the following:**

Expand All @@ -41,8 +38,8 @@ npm scripts are defined in `package.json`. These trigger a number of Gulp tasks.
- copy Sass files, applying Autoprefixer via PostCSS
- copy Nunjucks component template/macro files, including JSON configs
- copy GOV.UK Prototype Kit config files
- copy JavaScript ESM source files
- compile JavaScript ESM to CommonJS
- compile JavaScript to ECMAScript Modules (ESM)
- compile JavaScript to Universal Module Definition (UMD)
- runs `npm run postbuild:package` (which will test the output is correct)

**`npm run build:dist` will do the following:**
Expand Down Expand Up @@ -81,7 +78,7 @@ This task will:
This task will:

- check JavaScript code quality via ESLint (`npm run lint:js`) (using JavaScript Standard Style)
- compile JavaScript ESM to CommonJS into `./app/dist/javascripts`
- compile JavaScript to Universal Module Definition (UMD) into `./app/dist/javascripts`
- compile JavaScript documentation into `./app/dist/docs/jsdoc`

## Express app only
Expand Down
6 changes: 3 additions & 3 deletions tasks/build/package.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,15 @@ describe('package/', () => {
const componentPackage = componentsFilesPackage.filter(componentFilter)
const componentPackageESM = componentsFilesPackageESM.filter(componentFilter)

// CommonJS module not found at source
// UMD module not found at source
expect(componentSource)
.toEqual(expect.not.arrayContaining([join(componentName, `${componentName}.js`)]))

// CommonJS generated in package
// UMD module generated in package
expect(componentPackage)
.toEqual(expect.arrayContaining([join(componentName, `${componentName}.js`)]))

// ESM module generated in package
// ES module generated in package
expect(componentsFilesPackageESM)
.toEqual(expect.arrayContaining([join(componentName, `${componentName}.mjs`)]))

Expand Down

0 comments on commit f8a3a20

Please sign in to comment.