await freeTimeRequest();
homer0 / cjs2esm Goto Github PK
View Code? Open in Web Editor NEWTransforms a project that uses CommonJS to ES Modules.
Home Page: https://homer0.github.io/cjs2esm
License: MIT License
Transforms a project that uses CommonJS to ES Modules.
Home Page: https://homer0.github.io/cjs2esm
License: MIT License
await freeTimeRequest();
Happens with both //
and /*
. E.g. eslint directives get removed
Happens when rewriting a file that is already es module (but transpiled via webpack):
export default {foo: true}
becomes
const exported = {foo: true}
export default exported
export const {} = exported
export const {foo} = exported
I use pnpm, and it seems like pnpx cjs2esm
tries to import 5to6-codemod directly from node_modules but fails.
Once I added 5to6-codemod to the project it worked.
I'm converting a NodeJS project from CJS to ESM, and pretty much everything works with this CLI which is lovely in comparison to figuring out how jscodeshift
and 5to6-codemod
works! Our actual goal is to migrate everything to TypeScript, but in order to get import
statements working properly, I need to do some conversions into ESM before running ts-migrate
to mitigate errors and warnings.
Most of the codemods work, but named-export-generation
always fails for me. This occurs when I run it using this CLI, but also when I run the codemods directly with jscodeshift
and a locally installed version of 5to6-codemod. I'm running the same versions as this project specifies in package.json
.
Before:
const { GCLOUD_IMAGE_STORAGE_BUCKET_NAME } = process.env
const { DateTime } = require('luxon')
const { logger } = require('../../../../logger')
const { imageStorage } = require('./client')
const getSignedUploadUrl = async (filename, mimeType) => {
// ...
}
module.exports = {
getSignedUploadUrl,
}
After:
const { GCLOUD_IMAGE_STORAGE_BUCKET_NAME } = process.env
import { DateTime } from 'luxon';
import { logger } from '../../../../logger';
import { imageStorage } from './client.js';
const getSignedUploadUrl = async (filename, mimeType) => {
// ...
}
export default {
getSignedUploadUrl,
};
What I want here is for getSignedUploadUrl()
to become a named export (export const
), and for the default export to be deleted, because that's how my project typically imports functions like this. When you use Node with ESM, though, it doesn't recognize this older style of exporting, so I get errors all over the place unless manually go through and change named imports and exports myself, which kinda defeats the purpose of a codemod.
Not sure if this is the right place to put this, because I know the issue still exists in 5to6-codemod, but it didn't seem like that project was still getting any updates...
We use ?. extensively and it fails to parse this ๐ข
with
"modules": [{"name": "lodash", "path": "lodash-es"}],
running it on import ... from 'lodash-es'
results in import ... from 'lodash-es-es'
Hi, and thanks for this great tool!
I found an edge case causing it to fail. Please try out this file:
'use strict'
const v8 = require('v8')
const myClone = (o) => v8.deserialize(v8.serialize(o))
module.exports = { myClone }
Stacktrace:
ERR /Users/me/projects/customer/project/dev-repo/repos/api/esm/utils.js Transformation error (no value or default function given for field "exported" of ExportSpecifier("id": Identifier | null, "name": Identifier | null))
Error: no value or default function given for field "exported" of ExportSpecifier("id": Identifier | null, "name": Identifier | null)
at addParam (/Users/me/Library/pnpm/store/v3/tmp/dlx-96597/node_modules/.pnpm/[email protected]/node_modules/ast-types/src/types.ts:595:17)
at /Users/me/Library/pnpm/store/v3/tmp/dlx-96597/node_modules/.pnpm/[email protected]/node_modules/ast-types/src/types.ts:635:11
at Array.forEach (<anonymous>)
at Function.exportSpecifier (/Users/me/Library/pnpm/store/v3/tmp/dlx-96597/node_modules/.pnpm/[email protected]/node_modules/ast-types/src/types.ts:633:37)
at map (/Users/me/Library/pnpm/store/v3/tmp/dlx-96597/node_modules/.pnpm/[email protected]/node_modules/5to6-codemod/transforms/named-export-generation.js:187:13)
at Array.map (<anonymous>)
at createSpecificedNamedExportDeclaration (/Users/me/Library/pnpm/store/v3/tmp/dlx-96597/node_modules/.pnpm/[email protected]/node_modules/5to6-codemod/transforms/named-export-generation.js:186:6)
at NodePath.addNamedExports (/Users/me/Library/pnpm/store/v3/tmp/dlx-96597/node_modules/.pnpm/[email protected]/node_modules/5to6-codemod/transforms/named-export-generation.js:133:41)
at /Users/me/Library/pnpm/store/v3/tmp/dlx-96597/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/jscodeshift/src/Collection.js:75:36
at Array.forEach (<anonymous>)
at Collection.forEach (/Users/me/Library/pnpm/store/v3/tmp/dlx-96597/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/jscodeshift/src/Collection.js:74:18)
at module.exports (/Users/me/Library/pnpm/store/v3/tmp/dlx-96597/node_modules/.pnpm/[email protected]/node_modules/5to6-codemod/transforms/named-export-generation.js:222:40)
[cjs2esm] Error: At least one file couldn't be transformed with `named-export-generation`
[cjs2esm] at transformOutput (/Users/me/Library/pnpm/store/v3/tmp/dlx-96597/node_modules/.pnpm/[email protected]/node_modules/cjs2esm/src/index.js:446:11)
[cjs2esm] at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
[cjs2esm] at async /Users/me/Library/pnpm/store/v3/tmp/dlx-96597/node_modules/.pnpm/[email protected]/node_modules/cjs2esm/src/bin.js:25:3
[cjs2esm]
[cjs2esm] If the issue persist, create a ticket and I may be able to help you: https://github.com/homer0/cjs2esm/issues/new :D
Node v20.12.2
Running npx cjs2esm
in root file with no config shows:
[cjs2esm] Loading configuration...
[cjs2esm] No configuration was found, using defaults...
[cjs2esm] Output directory successfully cleaned
[cjs2esm] TypeError: Cannot read properties of undefined (reading 'to')
[cjs2esm] at transformOutput (/home/o/.npm/_npx/b6a0d7ead30377a5/node_modules/cjs2esm/src/index.js:338:25)
[cjs2esm] at /home/o/.npm/_npx/b6a0d7ead30377a5/node_modules/cjs2esm/src/bin.js:25:9
[cjs2esm]
[cjs2esm] If the issue persist, create a ticket and I may be able to help you: https://github.com/homer0/cjs2esm/issues/new :D
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.