Skip to content

[WIP] Convert code base to ESM #91

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

Draft
wants to merge 41 commits into
base: master
Choose a base branch
from
Draft

[WIP] Convert code base to ESM #91

wants to merge 41 commits into from

Conversation

agibson-godaddy
Copy link
Contributor

@agibson-godaddy agibson-godaddy commented Feb 28, 2025

Issue: https://godaddy-corp.atlassian.net/browse/MWC-17961

Summary

This PR converts the codebase to ESM. The changes/impacts are:

Require => import

Instead of const { spawn } = require('child_process') we'd use this syntax: import { spawn } from 'node:child_process'

So moving from require to import

Exports

module.exports is no longer a thing, so all instances of that have been changed.

Task registration

Although not strictly part of the ESM migration, I also updated our deprecated usage of the gulp task() function. See https://gulpjs.com/docs/en/api/task

Previously we registered a task using task('my-task-name', functionNameHere . This is deprecated and the new way to do it is to simply export the function -- e.g. export function functionNameHere

One challenge was how to get our old task names back. Previously we could just specify the task name as a string, which allowed us to use characters like: copy:build . But we don't have that same flexibility when exporting a function directly, since a function can't contain some special characters. The way around that is to set the displayName on the thing we're exporting. Example:

const validateReadmeHeaders = (done) => {
    // do things
    done()
}

validateReadmeHeaders.displayName = 'validate:readme_headers'

export {
  validateReadmeHeaders
}

Without the change, we'd have to run sake validateReadmeHeaders, but by setting the display name, sake validate:readme_headers still works!

Task Updates

I tested each of these as I went.

  • build.js
  • bump.js
  • bundle.js
  • clean.js
  • compile.js
  • config.js
  • copy.js
  • defacceinate.js
  • deploy.js
  • github.js
  • imagemin.js
  • lint.js
  • makepot.js
  • prerelease.js
  • prompt.js
  • scripts.js
  • shell.js
  • styles.js
  • upfw.js
  • validate.js
  • watch.js
  • wc.js
  • zip.js

QA

Build / zip

  1. Run sake zip using a plugin that includes blocks (like a payment gateway)
    • Command runs successfully
  2. Upload zip to a test site and activate the plugin
    • Plugin loads, no fatal errors
  3. Visit checkout page using block checkout
    • The gateway's assets / block load correctly

Deployment

To be completed only once the above has been done successfully!

  1. Deploy any plugin using sake deploy
  2. Deploy a payment gateway or blocks plugin using sake deploy
    • Deployment completes successfully
  3. Deploy any plugin to WordPress dot org using sake deploy

After merge

@agibson-godaddy agibson-godaddy self-assigned this Feb 28, 2025
@@ -37,116 +40,12 @@ if (fs.existsSync(devEnv)) {
// enable forward-referencing tasks, see https://github.com/gulpjs/gulp/issues/1028
gulp.registry(ForwardReference())

// define default config
let defaults = {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Config was moved to new, separate file lib/config.js so that it can now be imported where required, instead of passing it around function parameters.

@agibson-godaddy
Copy link
Contributor Author

agibson-godaddy commented May 7, 2025

Note this PR is affected by this: gulpjs/gulp-cli#269

As such, it only works on Node <= v22.11.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant