diff --git a/playground/default-export/package.json b/playground/default-export/package.json index 096804fa..30ecf8e3 100644 --- a/playground/default-export/package.json +++ b/playground/default-export/package.json @@ -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" diff --git a/playground/dummy-commonjs/package.json b/playground/dummy-commonjs/package.json index faee9552..b1ec5800 100644 --- a/playground/dummy-commonjs/package.json +++ b/playground/dummy-commonjs/package.json @@ -14,7 +14,6 @@ "require": "./dist/index.browser.js" }, "node": { - "module": "./dist/index.mjs", "import": "./node/index.mjs", "require": "./node/index.js" }, @@ -31,7 +30,6 @@ "require": "./dist/extra.browser.js" }, "node": { - "module": "./dist/extra.mjs", "import": "./node/extra.mjs", "require": "./node/extra.js" }, diff --git a/playground/dummy-module/package.json b/playground/dummy-module/package.json index 61b8e9ee..88cc60ea 100644 --- a/playground/dummy-module/package.json +++ b/playground/dummy-module/package.json @@ -14,7 +14,6 @@ "require": "./dist/index.browser.cjs" }, "node": { - "module": "./dist/index.js", "import": "./node/index.js", "require": "./node/index.cjs" }, @@ -31,7 +30,6 @@ "require": "./dist/extra.browser.cjs" }, "node": { - "module": "./dist/extra.js", "import": "./node/extra.js", "require": "./node/extra.cjs" }, diff --git a/playground/multi-export/package.json b/playground/multi-export/package.json index 833022ad..bc205078 100644 --- a/playground/multi-export/package.json +++ b/playground/multi-export/package.json @@ -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": { diff --git a/playground/multi-exports-commonjs/package.json b/playground/multi-exports-commonjs/package.json index a63bdf64..a8dc3f94 100644 --- a/playground/multi-exports-commonjs/package.json +++ b/playground/multi-exports-commonjs/package.json @@ -8,23 +8,15 @@ ".": { "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" diff --git a/playground/ts-bundler/package.json b/playground/ts-bundler/package.json index 20cc930b..2732523f 100644 --- a/playground/ts-bundler/package.json +++ b/playground/ts-bundler/package.json @@ -8,12 +8,8 @@ ".": { "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" diff --git a/playground/ts-node16/package.json b/playground/ts-node16/package.json index 8df3014b..9f855e1a 100644 --- a/playground/ts-node16/package.json +++ b/playground/ts-node16/package.json @@ -8,12 +8,8 @@ ".": { "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" diff --git a/playground/ts/package.json b/playground/ts/package.json index 4bbc6650..e8de4834 100644 --- a/playground/ts/package.json +++ b/playground/ts/package.json @@ -8,12 +8,8 @@ ".": { "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" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 59516ed1..fe5373aa 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -372,7 +372,7 @@ importers: version: 2.35.1(eslint@8.57.0)(svelte@4.2.12)(ts-node@10.9.2) multi-export: specifier: workspace:* - version: link:../multi-export + version: link:../../test/env/__tmp__/84a7ad61-34ae-4ca3-b1d9-66f16e778fb2 prettier: specifier: ^3.2.5 version: 3.2.5 @@ -435,6 +435,38 @@ importers: specifier: ^18.2.0 version: 18.2.0 + test/env/__tmp__/77872de5-3c53-44d1-8198-12f17c8e7885: {} + + test/env/__tmp__/84a7ad61-34ae-4ca3-b1d9-66f16e778fb2: + dependencies: + '@sanity/visual-editing': + specifier: ^1.5.2 + version: 1.6.0(@sanity/client@6.15.1)(@sveltejs/kit@2.5.2)(svelte@4.2.12) + dummy-side-effects: + specifier: workspace:* + version: link:../../../../playground/dummy-side-effects + devDependencies: + '@sveltejs/adapter-auto': + specifier: ^3.0.0 + version: 3.1.1(@sveltejs/kit@2.5.2) + '@sveltejs/kit': + specifier: ^2.0.0 + version: 2.5.2(@sveltejs/vite-plugin-svelte@3.0.2)(svelte@4.2.12)(vite@5.1.5) + '@sveltejs/package': + specifier: ^2.0.0 + version: 2.2.7(svelte@4.2.12)(typescript@5.4.2) + '@sveltejs/vite-plugin-svelte': + specifier: ^3.0.0 + version: 3.0.2(svelte@4.2.12)(vite@5.1.5) + svelte: + specifier: ^4.2.7 + version: 4.2.12 + vite: + specifier: ^5.0.11 + version: 5.1.5(@types/node@20.11.19) + + test/env/__tmp__/ab5cae5f-6b01-4331-a677-2fd9a1395132: {} + packages: /@aashutoshrathi/word-wrap@1.2.6: diff --git a/src/node/core/pkg/loadPkgWithReporting.ts b/src/node/core/pkg/loadPkgWithReporting.ts index ab7047da..a9789ce9 100644 --- a/src/node/core/pkg/loadPkgWithReporting.ts +++ b/src/node/core/pkg/loadPkgWithReporting.ts @@ -34,27 +34,6 @@ export async function loadPkgWithReporting(options: { } 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.import && exp.node.import && !assertOrder('node', 'import', keys)) { shouldError = true logger.error( @@ -62,23 +41,10 @@ export async function loadPkgWithReporting(options: { ) } - if (exp.module && exp.node.module && !assertOrder('node', 'module', keys)) { - shouldError = true - logger.error( - `exports["${expPath}"]: the \`node\` property should come before the \`module\` property`, - ) - } - - if ( - exp.node.import && - !exp.node.require && - exp.node.module && - exp.import && - exp.node.module !== exp.import - ) { + if (exp.node.module) { shouldError = true logger.error( - `exports["${expPath}"]: the \`node.module\` property should match \`import\``, + `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"`, ) } diff --git a/src/node/core/pkg/validatePkg.ts b/src/node/core/pkg/validatePkg.ts index f48c1c71..7a6ca04f 100644 --- a/src/node/core/pkg/validatePkg.ts +++ b/src/node/core/pkg/validatePkg.ts @@ -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(), diff --git a/test/parseExports.test.ts b/test/parseExports.test.ts index 2f041235..84ccdf4b 100644 --- a/test/parseExports.test.ts +++ b/test/parseExports.test.ts @@ -192,17 +192,6 @@ describe('parseExports', () => { }, `"node" can output both a "import" and "require" condition`, ], - [ - { - require: './dist/index.cjs', - node: { - module: './dist/index.js', - import: './dist/index.cjs.js', - }, - import: './dist/index.js', - }, - `"node" can be used to implement a wrapper that protects against the "dual package hazard"`, - ], ])('%o', (json, msg) => { const extMap = getPkgExtMap({legacyExports: false}) @@ -272,50 +261,58 @@ describe('parseExports', () => { import: './dist/index.js', node: { module: './dist/index.js', - import: './dist/index.cjs.js', }, default: './dist/index.js', }, - `"node.import" must be before "import"`, + `"node.module" considered harmful`, ], [ { source: './src/index.ts', + module: './dist/index.js', + require: './dist/index.cjs', + import: './dist/index.js', + default: './dist/index.js', + }, + `"module" considered harmful`, + ], + [ + { + source: './src/index.ts', + require: './dist/index.cjs', + import: './dist/index.js', node: { module: './dist/index.js', import: './dist/index.cjs.js', - require: './dist/index.cjs', }, - import: './dist/index.js', - require: './dist/index.cjs', default: './dist/index.js', }, - `"node.require" is unnecesary if it's the same as "require"`, + `"node.import" must be before "import"`, ], [ { source: './src/index.ts', - require: './dist/index.cjs', node: { + module: './dist/index.js', import: './dist/index.cjs.js', + require: './dist/index.cjs', }, import: './dist/index.js', + require: './dist/index.cjs', default: './dist/index.js', }, - `"node.module" should be specified when a "node.import" dual package hazard is used`, + `"node.require" is unnecesary if it's the same as "require"`, ], [ { - source: './src/index.ts', require: './dist/index.cjs', - module: './dist/index.js', node: { + module: './dist/index.js', import: './dist/index.cjs.js', }, import: './dist/index.js', - default: './dist/index.js', }, - `"module" should be moved to "node.module" when it's the same as "import"`, + `the "node" re-export pattern considered harmful, protect against the "dual package hazard" in ways that have high ecosystem compatibility`, ], [ {