Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate out CSS/JS lint tasks from copy-assets #2863

Merged
merged 9 commits into from
Sep 27, 2022
Merged

Conversation

colinrotherham
Copy link
Contributor

@colinrotherham colinrotherham commented Sep 20, 2022

This PR does a few things:

  1. Renames gulp copy-assetsgulp compile for clarity
  2. Renames gulp copy-files to gulp copy:files for consistency
  3. Separates out CSS/JS lint tasks from build and test
  4. Tackles the ./contributing/tasks.md documentation

Changes to running npm scripts

For npm scripts in particular I've updated runTask()npmScriptTask() to work with Gulp

gulp.task('scripts', gulp.series(
  npmScriptTask('lint:js', ['--silent']),
  'js:compile'
))

Which keeps friendly task names formatted for the CLI too

[12:54:34] Using gulpfile ~/path/to/project/govuk-frontend/gulpfile.js
[12:54:34] Starting 'scripts'...
[12:54:34] Starting 'npm run lint:js --silent'...
[12:54:35] Finished 'npm run lint:js --silent' after 1.7 s
[12:54:35] Starting 'js:compile'...
[12:54:37] Finished 'js:compile' after 2.03 s
[12:54:37] Finished 'scripts' after 3.73 s

When do linting tasks now run?

  1. Linting tasks don't run before tests
  2. Linting tasks don't run before build
  3. Linting tasks do run during gulp dev via watch task
  4. Linting tasks do run during PR checks via GitHub workflow

GitHub workflow showing Lint job

Open for feedback of course. Pairs up nicely with #2862 for a bit of a speed increase and closes #2855

@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-2863 September 20, 2022 08:27 Inactive
@colinrotherham colinrotherham requested a review from a team as a code owner September 20, 2022 08:58
Base automatically changed from upgrade-test-jest-percy to main September 20, 2022 09:11
@colinrotherham colinrotherham changed the title Linting tasks no longer run before copy-assets Separate out CSS/JS lint tasks from copy-assets Sep 20, 2022
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-2863 September 20, 2022 10:49 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-2863 September 20, 2022 11:06 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-2863 September 20, 2022 18:19 Inactive
@colinrotherham colinrotherham changed the base branch from main to gulp-completion September 20, 2022 19:05
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-2863 September 20, 2022 19:05 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-2863 September 20, 2022 19:11 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-2863 September 20, 2022 20:06 Inactive
- run accessibility tests on HTML files
- run tests on the review application

**`npm run heroku` runs on Heroku build/PR and it:**
- compiles components' HTML
- compiles CSS & JS
- compiles Sass & JavaScript
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very understandable, but it would be more accurate to say "compiles Sass to CSS". That's probably needlessly wordy, though?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this @domoscargin

The diff has changed but I've gone with:

Short descriptions

For npm scripts (or gulp tasks with sub tasks)

**`npm run heroku` runs on Heroku build/PR and it will:**
- run `npm run build:compile`
- start up Express

Long descriptions

For the main gulp task that does the work

**`gulp styles`**

This task will:
 - check Sass code quality via Stylelint (`npm run lint:scss`)
 - compile Sass to CSS (`gulp scss:compile`) into `./public`, or another location via the `--destination` flag

**`gulp scripts`**

This task will:
 - check JavaScript code quality via ESLint (`npm run lint:js`) (using JavaScript Standard Style)
 - compile JavaScript ESM to CommonJS (`gulp js:compile`) into `./public`, or another location via the `--destination` flag

Is that alright?

@@ -70,18 +65,21 @@ This task will:
**`gulp styles`**

This task will:
Copy link
Contributor

@domoscargin domoscargin Sep 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"This task performs:"?

Or add a verb to the beginning of each bullet point.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added verbs to all bullets, thanks 😊

This task will:
- concatenate and uglify javascript (`gulp js:compile`) to a destination folder that can be specified via a --destination flag
This task will:
- JavaScript code quality checks via ESLint (`gulp js:lint`) (using JavaScript Standard Style)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a verb to start

@@ -1,3 +1,7 @@
import Button from 'govuk-frontend/govuk/components/button/button'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we split this commit out?

Or add something to the commit message about why it's part of this linting PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've moved the commit to a separate PR, bit simpler that way #2869

// --------------------------------------
/**
* Serve task
* Restarts Node.js app when there are changes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we reflect that Sass changes cause a rebuild (via the watch task)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added more detail to the watch task for .scss and .mjs files

This one is serve (via Nodemon) so will only restart for Node.js related changes

gulpfile.js Outdated
// --------------------------------------
/**
* Dev task
* Runs a sequence of task on start
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Runs a sequence of task on start
* Runs a sequence of tasks on start

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, fixed

/**
* Copy assets task
* Copies assets to taskArguments.destination (public)
*/
gulp.task('copy:assets', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're referring to two things as "assets" in these tasks:

  • The static assets in src/assets
  • The compiled JS and CSS files

Maybe we should rename this to copy:static-assets or something.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep I noticed this too!

Hope the change I went with clears things up?

Rename gulp copy-assetsgulp compile (compiles CSS, Sass, Sassdoc)
Rename gulp copy-filesgulp copy:files (compiles selected files into --destination)

Gulp also looks for .displayName properties (see Task metadata) on plain Node.js functions so I've added these so everything makes sense from the command line:

clean:dist
clean:package
clean:public

Oh and as a bonus, it'll now output the actual npm script name too (not our task wrappers)

[12:27:26] Using gulpfile ~/path/to/project/govuk-frontend/gulpfile.js
[12:27:26] Starting 'styles'...
[12:27:26] Starting 'npm run lint:scss --silent'...
[12:27:29] Finished 'npm run lint:scss --silent' after 3.68 s
[12:27:29] Starting 'scss:compile'...
[12:27:30] Finished 'scss:compile' after 409 ms
[12:27:30] Finished 'styles' after 4.09 s

@domoscargin
Copy link
Contributor

domoscargin commented Sep 21, 2022

I've left a few small comments, but overall I think this looks good.

It'd be good to update this sheet when this gets approved: https://docs.google.com/spreadsheets/d/1aprdKVIx6FzBtX9ohWx6LR8p5XH3s2PNq1tev1ZwxkY/edit#gid=77403370

I can't think of any situation where we'd be hindered by not running linting before builds. Our Github workflow would catch anything getting pushed, and our dev/start task would catch anything we were working on locally.

I'll leave this open to see if anybody else wants to chime in.

@colinrotherham colinrotherham force-pushed the gulp-completion branch 3 times, most recently from f8b2827 to 0b54976 Compare September 21, 2022 12:18
Base automatically changed from gulp-completion to main September 21, 2022 12:22
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-2863 September 22, 2022 11:21 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-2863 September 22, 2022 11:46 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-2863 September 22, 2022 11:48 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-2863 September 26, 2022 11:26 Inactive
Copy link
Member

@romaricpascal romaricpascal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a few naming comments as I was trying to wrap my head around what runTask was doing, but otherwise it looks neat to me 😄

I think we'll need some further work around linting and testing though. There's a scenario that's pretty annoying when working on tests running with Puppeteer.

  • if you update a non-test JS file with a linting error,
  • the linting still runs on the gulp dev that runs to recompile, so nothing will be re-compiled
  • your test will keep running on the old version of the code, and likely still be failing
  • and, that's the annoying bit, the info on why it fails is in another terminal than the one running your test and that's tricky to see.

I'm not sure how we'd feel about removing the linting from the dev task altogether and just having it run on CI? Perhaps something to be done part of adding Git hooks to ensure there's still some local checks before code reaches Github.

tasks/run.js Outdated
Comment on lines 35 to 43
const runTask = (name, args = []) => {
const script = `${name} ${args.join(' ')}`.trim()

// Create named runner
// https://gulpjs.com/docs/en/api/task#task-metadata
const runner = () => task(name, args)
runner.displayName = `npm run ${script}`

return runner
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused by the naming. From what I understand, this'll only run npm scripts, so maybe we should clarify this by renaming to runNpmScript ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mind if I use the following?

Function for npm script
task()npmScript()

Function for gulp task (that returns npm script)
runTask()npmScriptTask()

tasks/run.js Outdated

// Create named runner
// https://gulpjs.com/docs/en/api/task#task-metadata
const runner = () => task(name, args)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's a bit of naming collision there as this is not the task function from Gulp, but our own from right above. We may want to rename that taskForNpmScript for clarification, especially as it doesn't have the same signature as Gulp's task.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Gulp v4 tasks can be "a stream, promise, event emitter, child process, or observable"

I've flipped it the other way around (function that returns Promise<number>)

Suggested change
const runner = () => task(name, args)
const task = () => npmScript(name, args)

tasks/run.js Outdated
* @returns {() => Promise<number>} Exit code
*/
const runTask = (name, args = []) => {
const script = `${name} ${args.join(' ')}`.trim()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe slightly nitpicky, but it seems that line is only used to generate the displayName. I got confused by why it was computed early, but then not passed to task down below, so I think it'd flow more clearly if it was right before the runner.displayName call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fine, can put it all on the same line

// Add task alias
// https://gulpjs.com/docs/en/api/task#task-metadata
task.displayName = `npm run ${name} ${args.join(' ')}`.trim()

Improves compatibility on Windows/Linux/Mac for linting tools
Also includes `configPaths` consistency and JSDoc descriptions
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-2863 September 26, 2022 13:31 Inactive
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-2863 September 27, 2022 08:56 Inactive
@colinrotherham
Copy link
Contributor Author

@romaricpascal Mind taking another look?

Can you give me an example of an ESLint error that blocks the JavaScript compile too?

I tried a simple one here showing how we resolve (not reject) the npm script Promise for all exit codes

[09:48:48] Starting 'scripts'...
[09:48:48] Starting 'npm run lint:js --silent'...
Server started at http://localhost:3000

/path/to/project/govuk-frontend/src/govuk/components/accordion/accordion.mjs
  36:71  error  Extra semicolon  semi

✖ 1 problem (1 error, 0 warnings)
  1 error and 0 warnings potentially fixable with the `--fix` option.


[09:48:49] Finished 'npm run lint:js --silent' after 1.28 s
[09:48:49] Starting 'js:compile'...
[09:48:51] Finished 'js:compile' after 1.64 s
[09:48:51] Finished 'scripts' after 2.92 s

Copy link
Member

@romaricpascal romaricpascal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you give me an example of an ESLint error that blocks the JavaScript compile too?

I tried a simple one here showing how we resolve (not reject) the npm script Promise for all exit codes

Oh my bad, I missed that, sorry 😞 . Now it does work nicely with the scenario I described before 🙌🏻 Looks good to go for me 😄

@colinrotherham colinrotherham merged commit 8f370c2 into main Sep 27, 2022
@colinrotherham colinrotherham deleted the lint-separately branch September 27, 2022 11:46
@colinrotherham
Copy link
Contributor Author

This PR was related to #2243

colinrotherham added a commit that referenced this pull request Nov 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Development

Successfully merging this pull request may close these issues.

Stop linting errors from stopping JS being compiled for tests
4 participants