-
-
Notifications
You must be signed in to change notification settings - Fork 81
warn about missing svelte export condition #206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
9288f82
25751ef
51ffed2
1344757
9750a01
a7463e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<import('.').Options>} | ||
* @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,40 @@ module.exports = function (options = {}) { | |
name += `/${parts.shift()}`; | ||
} | ||
|
||
if (parts.length > 0) return; | ||
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)) { | ||
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 | ||
dummdidumm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (entry === '.' && pkg.svelte) { | ||
return path.resolve(dir, pkg.svelte); | ||
} | ||
|
||
const resolved = await this.resolve(importee, importer, { skipSelf: true }); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should check here if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This won't happen for the foreseeable future (almost no library will run into this), so I'm in favor of merging now to get it out. |
||
// 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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we first say what it was that we failed to resolve? It might be a better message in the case that resolving fails for any other reason. I don't have any other reason in mind, but it might protect against something unexpected happening There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the message is fine as is, I don't see a situation where it would be wrong. |
||
warned = true; | ||
} | ||
} catch (e) { | ||
// do nothing, this isn't a Svelte library | ||
} | ||
} | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a matter of style, but you could make this
if (!pkg) return;
. The wholeif
block doesn't fit on one screen so it's hard to see at a glance if there's anything below it, but if you invert the condition it's easy to tell what happens