From 9288f82a7e75718541049e2b60653870cb0167f9 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Wed, 15 Feb 2023 18:31:07 -0500 Subject: [PATCH 1/6] warn about missing svelte export condition --- index.js | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index a028921..ca1e84d 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,6 @@ const path = require('path'); const fs = require('fs'); +const { resolve } = require('resolve.exports'); const { createFilter } = require('@rollup/pluginutils'); const { compile, preprocess } = require('svelte/compiler'); @@ -14,6 +15,8 @@ const plugin_options = new Set([ 'preprocess' ]); +let warned = false; + /** * @param [options] {Partial} * @returns {import('rollup').Plugin} @@ -51,7 +54,7 @@ module.exports = function (options = {}) { /** * Resolve an import's full filepath. */ - resolveId(importee, importer) { + async resolveId(importee, importer) { if (cache_emit.has(importee)) return importee; if ( !importer || @@ -69,16 +72,39 @@ module.exports = function (options = {}) { name += `/${parts.shift()}`; } - if (parts.length > 0) return; + const entry = parts.join('/') || '.'; + + let pkg; let search_dir = importer; while (search_dir !== (search_dir = path.dirname(search_dir))) { const dir = path.join(search_dir, 'node_modules', name); const file = path.join(dir, 'package.json'); if (fs.existsSync(file)) { - const pkg = JSON.parse(fs.readFileSync(file, 'utf-8')); - if (pkg.svelte) { - return path.resolve(dir, pkg.svelte); + pkg = JSON.parse(fs.readFileSync(file, 'utf-8')); + break; + } + } + + if (pkg) { + // resolve pkg.svelte first + if (entry === '.' && pkg.svelte) { + return path.resolve(dir, pkg.svelte); + } + + const resolved = await this.resolve(importee, importer, { skipSelf: true }); + + // if we can't resolve this import without the `svelte` condition, warn the user + if (!resolved) { + try { + resolve(pkg, entry, { conditions: ['svelte'] }); + + if (!warned) { + console.error(`\n\u001B[1m\u001B[31mWARNING: Your @rollup/plugin-node-resolve configuration's 'exportConditions' array should include 'svelte'. See https://github.com/sveltejs/rollup-plugin-svelte#svelte-exports-condition for more information\u001B[39m\u001B[22m\n'`); + warned = true; + } + } catch (e) { + // do nothing, this isn't a Svelte library } } } From 25751efa25ab0356ab5deea38d4753e4b7f0c652 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Wed, 15 Feb 2023 18:47:24 -0500 Subject: [PATCH 2/6] lint --- index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index ca1e84d..add7a66 100644 --- a/index.js +++ b/index.js @@ -75,10 +75,11 @@ module.exports = function (options = {}) { const entry = parts.join('/') || '.'; let pkg; + let dir; let search_dir = importer; while (search_dir !== (search_dir = path.dirname(search_dir))) { - const dir = path.join(search_dir, 'node_modules', name); + dir = path.join(search_dir, 'node_modules', name); const file = path.join(dir, 'package.json'); if (fs.existsSync(file)) { pkg = JSON.parse(fs.readFileSync(file, 'utf-8')); @@ -100,7 +101,7 @@ module.exports = function (options = {}) { resolve(pkg, entry, { conditions: ['svelte'] }); if (!warned) { - console.error(`\n\u001B[1m\u001B[31mWARNING: Your @rollup/plugin-node-resolve configuration's 'exportConditions' array should include 'svelte'. See https://github.com/sveltejs/rollup-plugin-svelte#svelte-exports-condition for more information\u001B[39m\u001B[22m\n'`); + console.error('\n\u001B[1m\u001B[31mWARNING: Your @rollup/plugin-node-resolve configuration\'s \'exportConditions\' array should include \'svelte\'. See https://github.com/sveltejs/rollup-plugin-svelte#svelte-exports-condition for more information\u001B[39m\u001B[22m\n'); warned = true; } } catch (e) { From 51ffed21cc302631e45b0c9fe5f5f8b439cb185b Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Wed, 15 Feb 2023 18:59:52 -0500 Subject: [PATCH 3/6] guess it would help to install resolve.exports, huh --- package-lock.json | 20 +++++++++++++++++--- package.json | 7 ++++--- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index 08778d4..a79dcce 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,15 +1,16 @@ { "name": "rollup-plugin-svelte", - "version": "7.1.1", + "version": "7.1.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "rollup-plugin-svelte", - "version": "7.1.1", + "version": "7.1.2", "license": "MIT", "dependencies": { - "@rollup/pluginutils": "^4.1.0" + "@rollup/pluginutils": "^4.1.0", + "resolve.exports": "^2.0.0" }, "devDependencies": { "eslint": "^7.14.0", @@ -1111,6 +1112,14 @@ "node": ">=4" } }, + "node_modules/resolve.exports": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/resolve.exports/-/resolve.exports-2.0.0.tgz", + "integrity": "sha512-6K/gDlqgQscOlg9fSRpWstA8sYe8rbELsSTNpx+3kTrsVCzvSl0zIvRErM7fdl9ERWDsKnrLnwB+Ne89918XOg==", + "engines": { + "node": ">=10" + } + }, "node_modules/rimraf": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-3.0.2.tgz", @@ -2281,6 +2290,11 @@ "integrity": "sha512-pb/MYmXstAkysRFx8piNI1tGFNQIFA3vkE3Gq4EuA1dF6gHp/+vgZqsCGJapvy8N3Q+4o7FwvquPJcnZ7RYy4g==", "dev": true }, + "resolve.exports": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/resolve.exports/-/resolve.exports-2.0.0.tgz", + "integrity": "sha512-6K/gDlqgQscOlg9fSRpWstA8sYe8rbELsSTNpx+3kTrsVCzvSl0zIvRErM7fdl9ERWDsKnrLnwB+Ne89918XOg==" + }, "rimraf": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-3.0.2.tgz", diff --git a/package.json b/package.json index 78c2115..2982934 100644 --- a/package.json +++ b/package.json @@ -25,11 +25,12 @@ "node": ">=10" }, "dependencies": { - "@rollup/pluginutils": "^4.1.0" + "@rollup/pluginutils": "^4.1.0", + "resolve.exports": "^2.0.0" }, "peerDependencies": { - "svelte": ">=3.5.0", - "rollup": ">=2.0.0" + "rollup": ">=2.0.0", + "svelte": ">=3.5.0" }, "devDependencies": { "eslint": "^7.14.0", From 134475705e47e1485b07cf1a72b0ee6b54cd4192 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Wed, 15 Feb 2023 19:19:56 -0500 Subject: [PATCH 4/6] get tests passing --- test/index.js | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/test/index.js b/test/index.js index 14cc9ed..aca355f 100644 --- a/test/index.js +++ b/test/index.js @@ -8,40 +8,44 @@ const sander = require('sander'); const plugin = require('..'); -test('resolves using pkg.svelte', () => { +const context = { + resolve: () => 'resolved' +}; + +test('resolves using pkg.svelte', async () => { const p = plugin(); assert.is( - p.resolveId('widget', path.resolve('test/foo/main.js')), + await p.resolveId.call(context, 'widget', path.resolve('test/foo/main.js')), path.resolve('test/node_modules/widget/src/Widget.svelte') ); }); -test('ignores built-in modules', () => { +test('ignores built-in modules', async () => { const p = plugin(); - assert.ok( - p.resolveId('path', path.resolve('test/foo/main.js')) == null + assert.is( + await p.resolveId.call(context, 'path', path.resolve('test/foo/main.js')), undefined ); }); -test('ignores esm modules that do not export package.json', () => { +test('ignores esm modules that do not export package.json', async () => { const p = plugin(); - assert.ok( - p.resolveId('esm-no-pkg-export', path.resolve('test/foo/main.js')) == null + assert.is( + await p.resolveId.call(context, 'esm-no-pkg-export', path.resolve('test/foo/main.js')), undefined ); }); -test('resolves esm module that exports package.json', () => { +test('resolves esm module that exports package.json', async () => { const p = plugin(); assert.is( - p.resolveId('esm-component', path.resolve('test/foo/main.js')), + await p.resolveId.call(context, 'esm-component', path.resolve('test/foo/main.js')), path.resolve('test/node_modules/esm-component/src/Component.svelte') ); }); -test('ignores virtual modules', () => { +test('ignores virtual modules', async () => { const p = plugin(); - assert.ok( - p.resolveId('path', path.resolve('\0some-plugin-generated-module')) == null + assert.is( + await p.resolveId.call(context, 'path', path.resolve('\0some-plugin-generated-module')), undefined ); }); From 9750a01b292c7c4fa3e2e5bf0c94f080fc954da9 Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Thu, 16 Feb 2023 21:17:48 +0100 Subject: [PATCH 5/6] Update index.js Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> --- index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index add7a66..5a21533 100644 --- a/index.js +++ b/index.js @@ -88,7 +88,8 @@ module.exports = function (options = {}) { } if (pkg) { - // resolve pkg.svelte first + // resolve pkg.svelte first for backwards compatibility + // we should resolve it after exports longer-term if (entry === '.' && pkg.svelte) { return path.resolve(dir, pkg.svelte); } From a7463e7a18585e4b6bf17eff50365a91773eb30a Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Thu, 16 Feb 2023 21:22:51 +0100 Subject: [PATCH 6/6] dedent --- index.js | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/index.js b/index.js index 5a21533..2bd1d13 100644 --- a/index.js +++ b/index.js @@ -87,27 +87,27 @@ module.exports = function (options = {}) { } } - if (pkg) { - // resolve pkg.svelte first for backwards compatibility - // we should resolve it after exports longer-term - if (entry === '.' && pkg.svelte) { - return path.resolve(dir, pkg.svelte); - } + if (!pkg) return; + + // resolve pkg.svelte first for backwards compatibility + // we should resolve it after exports longer-term + if (entry === '.' && pkg.svelte) { + return path.resolve(dir, pkg.svelte); + } - const resolved = await this.resolve(importee, importer, { skipSelf: true }); + const resolved = await this.resolve(importee, importer, { skipSelf: true }); - // if we can't resolve this import without the `svelte` condition, warn the user - if (!resolved) { - try { - resolve(pkg, entry, { conditions: ['svelte'] }); + // if we can't resolve this import without the `svelte` condition, warn the user + if (!resolved) { + try { + resolve(pkg, entry, { conditions: ['svelte'] }); - if (!warned) { - console.error('\n\u001B[1m\u001B[31mWARNING: Your @rollup/plugin-node-resolve configuration\'s \'exportConditions\' array should include \'svelte\'. See https://github.com/sveltejs/rollup-plugin-svelte#svelte-exports-condition for more information\u001B[39m\u001B[22m\n'); - warned = true; - } - } catch (e) { - // do nothing, this isn't a Svelte library + if (!warned) { + console.error('\n\u001B[1m\u001B[31mWARNING: Your @rollup/plugin-node-resolve configuration\'s \'exportConditions\' array should include \'svelte\'. See https://github.com/sveltejs/rollup-plugin-svelte#svelte-exports-condition for more information\u001B[39m\u001B[22m\n'); + warned = true; } + } catch (e) { + // do nothing, this isn't a Svelte library } } },