Skip to content

Decaffeinate (ESM conversion)#197

Merged
paul90 merged 5 commits intofedwiki:ESMfrom
SamAxe:decaffeinate
Jul 16, 2025
Merged

Decaffeinate (ESM conversion)#197
paul90 merged 5 commits intofedwiki:ESMfrom
SamAxe:decaffeinate

Conversation

@SamAxe
Copy link
Copy Markdown
Contributor

@SamAxe SamAxe commented Jun 2, 2025

I'm not sure if the the branch is ready for ESM or if that's even desirable, but did a bit today and figured I'd at least share the progress I made. npm test passes (in my environment) and I ran the format command somewhat recently...

Copy link
Copy Markdown
Member

@dobbs dobbs left a comment

Choose a reason for hiding this comment

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

I was hoping that we'd be able to switch to imports instead of requires, so thanks for this effort. I've looked over the changes somewhat closely but haven't tried running the code or tests locally yet.

Comment on lines +12 to +15
import path from 'node:path'
import { randomBytes } from 'node:crypto'
import { fileURLToPath } from 'node:url'
import { dirname } from 'node:path'
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can line 12 and line 15 be combined, since both are coming from node:path?
Maybe this would work?

import {default:path, dirname} from 'node:path';

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

For sure. I still consider this an exploratory branch rather than something I'd offer to merge right now. Many things like this to tidy up. Thank you for looking it over and commenting.

argv.id ||= path.join(argv.status, 'owner.json')
argv.uploadLimit ||= '5mb'
argv.cookieSecret ||= require('crypto').randomBytes(64).toString('hex')
argv.cookieSecret ||= randomBytes(64).toString('hex')
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for moving the inline require() to an import() above.

//
// const async = require('async')
//
// // const random_id = require('./random_id.cjs')
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

is random_id not in use?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

There are several unused imports throughout the files. As far as I can tell, random_id is not used in pages.

packageDir: path.join(__dirname, '..', 'node_modules'),
security_legacy: true,
})
const page = require('../lib/page')(argv)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I like this spot too where you pull the inline require() up among the other imports.

const __dirname = path.dirname(__filename)

// CommonJS server module (.cjs)
const server = await import('../index.js')
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just noticing out loud how much this dynamic import() stands out against the background of the other static imports. I think this lends itself to clarify some utility in switching from require() to import. This difference is visible here in a way that it wasn't before.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks again for comments. Some things to note with this repo:

  • 'node test' and 'deno task test' both pass on my machine but I won't be surprised if there are version dependencies.
  • I used ChatGPT to do the dynamic import conversions in server, so it's only as good as the test cases that test it. For example, the version endpoint is currently commented out in the ESM version of server.js in this tree. I tried adding a test to the test suite for testing the version cases, but I'm not sure that the current tests quite allow for that yet.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Note that server then has two await imports too. One for the database and one for the security module. Changing things around to only need static imports seem like it would be a desirable simplification.

@paul90 paul90 merged commit 7638ba6 into fedwiki:ESM Jul 16, 2025
@paul90
Copy link
Copy Markdown
Member

paul90 commented Jul 16, 2025

Rather than risk this PR going AWOL, like a previous one. After merging decaffeinate into main I've merged this into decaffeinate and renamed it ESM.

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.

3 participants