From 306742213686df929fbf051f547b34e40dcace60 Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Thu, 16 Sep 2021 14:55:41 +0200 Subject: [PATCH] [fix] add svelte field when packaging (#2431) Several heuristics in Kit/vite-plugin-svelte to tell Vite to mark Svelte packages rely on the "svelte" property. Vite/Rollup/Webpack plugin can all deal with it. Fixes #1959 Co-authored-by: Bjorn Lu <34116392+bluwy@users.noreply.github.com> --- .changeset/friendly-chefs-draw.md | 5 ++++ packages/kit/src/packaging/index.js | 30 +++++++++++++++++++ .../emitTypes-false/expected/package.json | 3 +- .../exports-merge/expected/package.json | 3 +- .../exports-replace/expected/package.json | 3 +- .../fixtures/exports-replace/package.json | 3 +- .../files-exclude/expected/package.json | 3 +- .../fixtures/javascript/expected/package.json | 3 +- .../fixtures/typescript/expected/package.json | 3 +- 9 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 .changeset/friendly-chefs-draw.md diff --git a/.changeset/friendly-chefs-draw.md b/.changeset/friendly-chefs-draw.md new file mode 100644 index 000000000000..6569f074547a --- /dev/null +++ b/.changeset/friendly-chefs-draw.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +Add "svelte" field to package.json when running package command diff --git a/packages/kit/src/packaging/index.js b/packages/kit/src/packaging/index.js index 62416451cf7c..7a82a6cef0a2 100644 --- a/packages/kit/src/packaging/index.js +++ b/packages/kit/src/packaging/index.js @@ -39,6 +39,7 @@ export async function make_package(config, cwd = process.cwd()) { /** @type {Record} */ const clashes = {}; + let contains_svelte_files = false; for (const file of files) { const ext = path.extname(file); @@ -66,6 +67,7 @@ export async function make_package(config, cwd = process.cwd()) { out_contents = config.preprocess ? (await preprocess(source, config.preprocess, { filename })).code : source; + contains_svelte_files = true; } else if (ext === '.ts' && file.endsWith('.d.ts')) { // TypeScript's declaration emit won't copy over the d.ts files, so we do it here out_file = file; @@ -104,6 +106,34 @@ export async function make_package(config, cwd = process.cwd()) { } pkg.exports = { ...generated, ...pkg.exports }; + + if (!pkg.svelte && contains_svelte_files) { + // Several heuristics in Kit/vite-plugin-svelte to tell Vite to mark Svelte packages + // rely on the "svelte" property. Vite/Rollup/Webpack plugin can all deal with it. + // See https://github.com/sveltejs/kit/issues/1959 for more info and related threads. + if (pkg.exports['.']) { + const svelte_export = + typeof pkg.exports['.'] === 'string' + ? pkg.exports['.'] + : pkg.exports['.'].import || pkg.exports['.'].default; + if (svelte_export) { + pkg.svelte = svelte_export; + } else { + console.warn( + 'Cannot generate a "svelte" entry point because ' + + 'the "." entry in "exports" is not a string. ' + + 'If you set it by hand, please also set one of the options as a "svelte" entry point' + ); + } + } else { + console.warn( + 'Cannot generate a "svelte" entry point because ' + + 'the "." entry in "exports" is missing. ' + + 'Please specify one or set a "svelte" entry point yourself' + ); + } + } + write(path.join(cwd, config.kit.package.dir, 'package.json'), JSON.stringify(pkg, null, ' ')); const whitelist = fs.readdirSync(cwd).filter((file) => { diff --git a/packages/kit/src/packaging/test/fixtures/emitTypes-false/expected/package.json b/packages/kit/src/packaging/test/fixtures/emitTypes-false/expected/package.json index 455754648019..b92c803cdd24 100644 --- a/packages/kit/src/packaging/test/fixtures/emitTypes-false/expected/package.json +++ b/packages/kit/src/packaging/test/fixtures/emitTypes-false/expected/package.json @@ -8,5 +8,6 @@ "./Test.svelte": "./Test.svelte", "./Test2.svelte": "./Test2.svelte", ".": "./index.js" - } + }, + "svelte": "./index.js" } diff --git a/packages/kit/src/packaging/test/fixtures/exports-merge/expected/package.json b/packages/kit/src/packaging/test/fixtures/exports-merge/expected/package.json index cecfa56af8c7..36398680c453 100644 --- a/packages/kit/src/packaging/test/fixtures/exports-merge/expected/package.json +++ b/packages/kit/src/packaging/test/fixtures/exports-merge/expected/package.json @@ -16,5 +16,6 @@ "./Test": "./Test.svelte", "./package.json": "./package.json" }, - "type": "module" + "type": "module", + "svelte": "./index.js" } diff --git a/packages/kit/src/packaging/test/fixtures/exports-replace/expected/package.json b/packages/kit/src/packaging/test/fixtures/exports-replace/expected/package.json index 74f8241157f9..64f5503df960 100644 --- a/packages/kit/src/packaging/test/fixtures/exports-replace/expected/package.json +++ b/packages/kit/src/packaging/test/fixtures/exports-replace/expected/package.json @@ -8,5 +8,6 @@ "import": "./index.js" }, "./package.json": "./package.json" - } + }, + "svelte": "./Test.svelte" } diff --git a/packages/kit/src/packaging/test/fixtures/exports-replace/package.json b/packages/kit/src/packaging/test/fixtures/exports-replace/package.json index fab128fb8675..9a1f196ad238 100644 --- a/packages/kit/src/packaging/test/fixtures/exports-replace/package.json +++ b/packages/kit/src/packaging/test/fixtures/exports-replace/package.json @@ -7,5 +7,6 @@ ".": { "import": "./index.js" } - } + }, + "svelte": "./Test.svelte" } diff --git a/packages/kit/src/packaging/test/fixtures/files-exclude/expected/package.json b/packages/kit/src/packaging/test/fixtures/files-exclude/expected/package.json index 68d90e6dab5a..43785b17dc8f 100644 --- a/packages/kit/src/packaging/test/fixtures/files-exclude/expected/package.json +++ b/packages/kit/src/packaging/test/fixtures/files-exclude/expected/package.json @@ -8,5 +8,6 @@ "./internal": "./internal/index.js", "./Test.svelte": "./Test.svelte", ".": "./index.js" - } + }, + "svelte": "./index.js" } diff --git a/packages/kit/src/packaging/test/fixtures/javascript/expected/package.json b/packages/kit/src/packaging/test/fixtures/javascript/expected/package.json index 878f80800d0e..61989d1d853b 100644 --- a/packages/kit/src/packaging/test/fixtures/javascript/expected/package.json +++ b/packages/kit/src/packaging/test/fixtures/javascript/expected/package.json @@ -11,5 +11,6 @@ "./Test2.svelte": "./Test2.svelte", "./utils": "./utils.js", ".": "./index.js" - } + }, + "svelte": "./index.js" } diff --git a/packages/kit/src/packaging/test/fixtures/typescript/expected/package.json b/packages/kit/src/packaging/test/fixtures/typescript/expected/package.json index 9ef5c7780483..f573564d5834 100644 --- a/packages/kit/src/packaging/test/fixtures/typescript/expected/package.json +++ b/packages/kit/src/packaging/test/fixtures/typescript/expected/package.json @@ -9,5 +9,6 @@ "./Test2.svelte": "./Test2.svelte", "./utils": "./utils.js", ".": "./index.js" - } + }, + "svelte": "./index.js" }