Skip to content

Commit

Permalink
feat: remove node.import ESM CJS wrapper support
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Mar 7, 2024
1 parent f79c63d commit dca3a3b
Show file tree
Hide file tree
Showing 26 changed files with 53 additions and 298 deletions.
1 change: 1 addition & 0 deletions playground/babel-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"private": true,
"license": "MIT",
"type": "module",
"sideEffects": false,
"exports": {
".": {
"types": "./dist/index.d.ts",
Expand Down
1 change: 1 addition & 0 deletions playground/custom-dist/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"private": true,
"license": "MIT",
"type": "module",
"sideEffects": false,
"exports": {
".": {
"types": "./lib/src/index.d.ts",
Expand Down
7 changes: 2 additions & 5 deletions playground/default-export/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@
"private": true,
"license": "MIT",
"type": "module",
"sideEffects": false,
"exports": {
".": {
"source": "./src/index.js",
"require": "./dist/index.cjs",
"node": {
"module": "./dist/index.js",
"import": "./dist/index.cjs.js"
},
"import": "./dist/index.js",
"require": "./dist/index.cjs",
"default": "./dist/index.js"
},
"./package.json": "./package.json"
Expand Down
7 changes: 0 additions & 7 deletions playground/dummy-commonjs/node/extra.js

This file was deleted.

5 changes: 0 additions & 5 deletions playground/dummy-commonjs/node/extra.mjs

This file was deleted.

7 changes: 0 additions & 7 deletions playground/dummy-commonjs/node/index.js

This file was deleted.

5 changes: 0 additions & 5 deletions playground/dummy-commonjs/node/index.mjs

This file was deleted.

12 changes: 1 addition & 11 deletions playground/dummy-commonjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"private": true,
"license": "MIT",
"type": "commonjs",
"sideEffects": false,
"exports": {
".": {
"types": "./dist/index.d.ts",
Expand All @@ -13,11 +14,6 @@
"import": "./dist/index.browser.mjs",
"require": "./dist/index.browser.js"
},
"node": {
"module": "./dist/index.mjs",
"import": "./node/index.mjs",
"require": "./node/index.js"
},
"import": "./dist/index.mjs",
"require": "./dist/index.js",
"default": "./dist/index.mjs"
Expand All @@ -30,11 +26,6 @@
"import": "./dist/extra.browser.mjs",
"require": "./dist/extra.browser.js"
},
"node": {
"module": "./dist/extra.mjs",
"import": "./node/extra.mjs",
"require": "./node/extra.js"
},
"import": "./dist/extra.mjs",
"require": "./dist/extra.js",
"default": "./dist/extra.mjs"
Expand All @@ -58,7 +49,6 @@
},
"files": [
"dist",
"node",
"src"
],
"scripts": {
Expand Down
7 changes: 0 additions & 7 deletions playground/dummy-module/node/extra.cjs

This file was deleted.

5 changes: 0 additions & 5 deletions playground/dummy-module/node/extra.js

This file was deleted.

7 changes: 0 additions & 7 deletions playground/dummy-module/node/index.cjs

This file was deleted.

5 changes: 0 additions & 5 deletions playground/dummy-module/node/index.js

This file was deleted.

12 changes: 1 addition & 11 deletions playground/dummy-module/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"private": true,
"license": "MIT",
"type": "module",
"sideEffects": false,
"exports": {
".": {
"types": "./dist/index.d.ts",
Expand All @@ -13,11 +14,6 @@
"import": "./dist/index.browser.js",
"require": "./dist/index.browser.cjs"
},
"node": {
"module": "./dist/index.js",
"import": "./node/index.js",
"require": "./node/index.cjs"
},
"import": "./dist/index.js",
"require": "./dist/index.cjs",
"default": "./dist/index.js"
Expand All @@ -30,11 +26,6 @@
"import": "./dist/extra.browser.js",
"require": "./dist/extra.browser.cjs"
},
"node": {
"module": "./dist/extra.js",
"import": "./node/extra.js",
"require": "./node/extra.cjs"
},
"import": "./dist/extra.js",
"require": "./dist/extra.cjs",
"default": "./dist/extra.js"
Expand All @@ -58,7 +49,6 @@
},
"files": [
"dist",
"node",
"src"
],
"scripts": {
Expand Down
12 changes: 2 additions & 10 deletions playground/multi-export/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,15 @@
".": {
"types": "./dist/index.d.ts",
"source": "./src/index.ts",
"require": "./dist/index.cjs",
"node": {
"module": "./dist/index.js",
"import": "./dist/index.cjs.js"
},
"import": "./dist/index.js",
"require": "./dist/index.cjs",
"default": "./dist/index.js"
},
"./plugin": {
"types": "./dist/plugin.d.ts",
"source": "./src/plugin.ts",
"require": "./dist/plugin.cjs",
"node": {
"module": "./dist/plugin.js",
"import": "./dist/plugin.cjs.js"
},
"import": "./dist/plugin.js",
"require": "./dist/plugin.cjs",
"default": "./dist/plugin.js"
},
"./pure": {
Expand Down
13 changes: 3 additions & 10 deletions playground/multi-exports-commonjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,20 @@
"private": true,
"license": "MIT",
"type": "commonjs",
"sideEffects": false,
"exports": {
".": {
"types": "./dist/index.d.ts",
"source": "./src/index.ts",
"require": "./dist/index.js",
"node": {
"module": "./dist/index.esm.js",
"import": "./dist/index.cjs.mjs"
},
"import": "./dist/index.esm.js",
"require": "./dist/index.js",
"default": "./dist/index.esm.js"
},
"./plugin": {
"types": "./dist/plugin.d.ts",
"source": "./src/plugin.ts",
"require": "./dist/plugin.js",
"node": {
"module": "./dist/plugin.esm.js",
"import": "./dist/plugin.cjs.mjs"
},
"import": "./dist/plugin.esm.js",
"require": "./dist/plugin.js",
"default": "./dist/plugin.esm.js"
},
"./package.json": "./package.json"
Expand Down
7 changes: 2 additions & 5 deletions playground/ts-bundler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@
"private": true,
"license": "MIT",
"type": "module",
"sideEffects": false,
"exports": {
".": {
"types": "./dist/index.d.ts",
"source": "./src/index.ts",
"require": "./dist/index.cjs",
"node": {
"module": "./dist/index.js",
"import": "./dist/index.cjs.js"
},
"import": "./dist/index.js",
"require": "./dist/index.cjs",
"default": "./dist/index.js"
},
"./package.json": "./package.json"
Expand Down
7 changes: 2 additions & 5 deletions playground/ts-node16/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@
"private": true,
"license": "MIT",
"type": "module",
"sideEffects": false,
"exports": {
".": {
"types": "./dist/index.d.ts",
"source": "./src/index.ts",
"require": "./dist/index.cjs",
"node": {
"module": "./dist/index.js",
"import": "./dist/index.cjs.js"
},
"import": "./dist/index.js",
"require": "./dist/index.cjs",
"default": "./dist/index.js"
},
"./package.json": "./package.json"
Expand Down
7 changes: 2 additions & 5 deletions playground/ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@
"private": true,
"license": "MIT",
"type": "module",
"sideEffects": false,
"exports": {
".": {
"types": "./dist/index.d.ts",
"source": "./src/index.ts",
"require": "./dist/index.cjs",
"node": {
"module": "./dist/index.js",
"import": "./dist/index.cjs.js"
},
"import": "./dist/index.js",
"require": "./dist/index.cjs",
"default": "./dist/index.js"
},
"./package.json": "./package.json"
Expand Down
1 change: 1 addition & 0 deletions playground/use-client-directive/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"private": true,
"license": "MIT",
"type": "module",
"sideEffects": false,
"exports": {
".": {
"types": "./dist/index.d.ts",
Expand Down
47 changes: 13 additions & 34 deletions src/node/core/pkg/loadPkgWithReporting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,52 +33,37 @@ export async function loadPkgWithReporting(options: {
logger.error(`exports["${expPath}"]: the \`types\` property should be the first property`)
}

if (exp.node) {
const nodeKeys = Object.keys(exp.node)

if (!assertOrder('module', 'import', nodeKeys)) {
shouldError = true
logger.error(
`exports["${expPath}"]: the \`node.module\` property should come before the \`node.import\` property`,
)
}

if (!assertOrder('import', 'require', nodeKeys)) {
logger.warn(
`exports["${expPath}"]: the \`node.import\` property should come before the \`node.require\` property`,
)
}

if (!assertOrder('module', 'require', nodeKeys)) {
logger.warn(
`exports["${expPath}"]: the \`node.module\` property should come before \`node.require\` property`,
)
}
if (exp.module) {
shouldError = true
logger.error(
`exports["${expPath}"]: the \`module\` condition shouldn't be used as it's not well supported in all bundlers.`,
)
}

if (exp.node) {
if (exp.import && exp.node.import && !assertOrder('node', 'import', keys)) {
shouldError = true
logger.error(
`exports["${expPath}"]: the \`node\` property should come before the \`import\` property`,
)
}

if (exp.module && exp.node.module && !assertOrder('node', 'module', keys)) {
if (exp.node.module) {
shouldError = true
logger.error(
`exports["${expPath}"]: the \`node\` property should come before the \`module\` property`,
`exports["${expPath}"]: the \`node.module\` condition shouldn't be used as it's not well supported in all bundlers. A better strategy is to refactor the codebase to no longer be vulnerable to the "dual package hazard"`,
)
}

if (
!exp.node.source &&
exp.node.import &&
!exp.node.require &&
exp.node.module &&
exp.import &&
exp.node.module !== exp.import
(exp.node.require || exp.require) &&
(exp.node.import.endsWith('.cjs.js') || exp.node.import.endsWith('.cjs.mjs'))
) {
shouldError = true
logger.error(
`exports["${expPath}"]: the \`node.module\` property should match \`import\``,
`exports["${expPath}"]: the \`node.import\` re-export pattern shouldn't be used as it's not well supported in all bundlers. A better strategy is to refactor the codebase to no longer be vulnerable to the "dual package hazard"`,
)
}

Expand All @@ -99,12 +84,6 @@ export async function loadPkgWithReporting(options: {
`exports["${expPath}"]: the \`import\` property should come before the \`require\` property`,
)
}

if (!assertOrder('module', 'import', keys)) {
logger.warn(
`exports["${expPath}"]: the \`module\` property should come before \`import\` property`,
)
}
}

if (!assertLast('default', keys)) {
Expand Down
2 changes: 0 additions & 2 deletions src/node/core/pkg/validatePkg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@ const pkgSchema = z.object({
node: z.optional(
z.object({
source: z.optional(z.string()),
module: z.optional(z.string()),
import: z.optional(z.string()),
require: z.optional(z.string()),
}),
),
module: z.optional(z.string()),
import: z.optional(z.string()),
require: z.optional(z.string()),
default: z.string(),
Expand Down
Loading

0 comments on commit dca3a3b

Please sign in to comment.