Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: sanity-io/pkg-utils
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v2.2.0
Choose a base ref
...
head repository: sanity-io/pkg-utils
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v2.2.1
Choose a head ref
  • 4 commits
  • 6 files changed
  • 2 contributors

Commits on Jan 13, 2023

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    da20199 View commit details
  2. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    87cd512 View commit details
  3. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    0467e6e View commit details
  4. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    e31c281 View commit details
Showing with 69 additions and 9 deletions.
  1. +6 −0 CHANGELOG.md
  2. +2 −1 package.json
  3. +27 −0 playground/default-export/package.json
  4. +7 −0 playground/default-export/src/index.js
  5. +2 −2 playground/multi-export/package.json
  6. +25 −6 src/node/tasks/node/reexportCjsTask.ts
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -5,6 +5,12 @@
All notable changes to this project will be documented in this file. See
[Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [2.2.1](https://github.com/sanity-io/pkg-utils/compare/v2.2.0...v2.2.1) (2023-01-13)

### Bug Fixes

- re-export default export ([da20199](https://github.com/sanity-io/pkg-utils/commit/da20199e437e841caf7a07bf886e48558e115fbf))

## [2.2.0](https://github.com/sanity-io/pkg-utils/compare/v2.1.1...v2.2.0) (2023-01-12)

### Features
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "module",
"name": "@sanity/pkg-utils",
"version": "2.2.0",
"version": "2.2.1",
"author": "Sanity.io <hello@sanity.io>",
"license": "MIT",
"description": "Simple utilities for modern npm packages.",
@@ -48,6 +48,7 @@
"extract": "sanity-tsdoc etl --outDir etc/api/@sanity/pkg-utils --tsconfig tsconfig.dist.json",
"format": "prettier --write --cache --ignore-unknown .",
"lint": "eslint . --ext .cjs,.js,.ts,.tsx",
"playground:build": "pnpm --filter './playground/**' build",
"playground:clean": "pnpm --filter './playground/**' --silent clean",
"prepublishOnly": "pnpm run build",
"release": "semantic-release",
27 changes: 27 additions & 0 deletions playground/default-export/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"private": true,
"type": "module",
"name": "default-export",
"version": "1.0.0",
"license": "MIT",
"source": "./src/index.js",
"module": "./dist/index.js",
"main": "./dist/index.cjs",
"exports": {
".": {
"source": "./src/index.js",
"require": "./dist/index.cjs",
"node": {
"import": "./dist/index.cjs.js",
"require": "./dist/index.cjs"
},
"import": "./dist/index.js",
"default": "./dist/index.js"
},
"./package.json": "./package.json"
},
"scripts": {
"build": "run-s clean && pkg build --strict && pkg --strict",
"clean": "rimraf dist"
}
}
7 changes: 7 additions & 0 deletions playground/default-export/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function main() {
return true
}

export default main

export const version = '1.0.0'
4 changes: 2 additions & 2 deletions playground/multi-export/package.json
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@
"source": "./src/index.ts",
"require": "./dist/index.cjs",
"node": {
"import": "./dist/node/index.cjs.js",
"import": "./dist/index.cjs.js",
"require": "./dist/index.cjs"
},
"import": "./dist/index.js",
@@ -25,7 +25,7 @@
"source": "./src/plugin.ts",
"require": "./dist/plugin.cjs",
"node": {
"import": "./dist/node/plugin.cjs.js",
"import": "./dist/plugin.cjs.js",
"require": "./dist/plugin.cjs"
},
"import": "./dist/plugin.js",
31 changes: 25 additions & 6 deletions src/node/tasks/node/reexportCjsTask.ts
Original file line number Diff line number Diff line change
@@ -51,13 +51,32 @@ async function exec(ctx: BuildContext, task: NodeReExportFromCJSTask) {
relativeImport = relativeImport.slice(2)
}

const code = [
`import cjs from '${relativeImport}';`,
'',
...Object.keys(mod).map((k) => `export const ${k} = cjs.${k};`),
'',
].join('\n')
const code = compileESMWrapper(mod, relativeImport)

await writeFile(targetPath, code, 'utf8')
}
}

function compileESMWrapper(mod: any, relativeImport: string) {
const keys = Object.keys(mod).filter((k) => k !== '__esModule')

let code = `import cjs from '${relativeImport}';\n`

if (keys.length) {
code += `\n`
code += keys
.filter((k) => (mod.__esModule ? k !== 'default' : true))
.map((k) => `export const ${k} = cjs.${k};\n`)
.join('')
}

code += `\n`

if (mod.__esModule) {
code += `export default cjs.default;\n`
} else {
code += `export default cjs;\n`
}

return code
}