Skip to content

Commit 9288f82

Browse files
committed
warn about missing svelte export condition
1 parent 9744823 commit 9288f82

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

index.js

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const path = require('path');
22
const fs = require('fs');
3+
const { resolve } = require('resolve.exports');
34
const { createFilter } = require('@rollup/pluginutils');
45
const { compile, preprocess } = require('svelte/compiler');
56

@@ -14,6 +15,8 @@ const plugin_options = new Set([
1415
'preprocess'
1516
]);
1617

18+
let warned = false;
19+
1720
/**
1821
* @param [options] {Partial<import('.').Options>}
1922
* @returns {import('rollup').Plugin}
@@ -51,7 +54,7 @@ module.exports = function (options = {}) {
5154
/**
5255
* Resolve an import's full filepath.
5356
*/
54-
resolveId(importee, importer) {
57+
async resolveId(importee, importer) {
5558
if (cache_emit.has(importee)) return importee;
5659
if (
5760
!importer ||
@@ -69,16 +72,39 @@ module.exports = function (options = {}) {
6972
name += `/${parts.shift()}`;
7073
}
7174

72-
if (parts.length > 0) return;
75+
const entry = parts.join('/') || '.';
76+
77+
let pkg;
7378

7479
let search_dir = importer;
7580
while (search_dir !== (search_dir = path.dirname(search_dir))) {
7681
const dir = path.join(search_dir, 'node_modules', name);
7782
const file = path.join(dir, 'package.json');
7883
if (fs.existsSync(file)) {
79-
const pkg = JSON.parse(fs.readFileSync(file, 'utf-8'));
80-
if (pkg.svelte) {
81-
return path.resolve(dir, pkg.svelte);
84+
pkg = JSON.parse(fs.readFileSync(file, 'utf-8'));
85+
break;
86+
}
87+
}
88+
89+
if (pkg) {
90+
// resolve pkg.svelte first
91+
if (entry === '.' && pkg.svelte) {
92+
return path.resolve(dir, pkg.svelte);
93+
}
94+
95+
const resolved = await this.resolve(importee, importer, { skipSelf: true });
96+
97+
// if we can't resolve this import without the `svelte` condition, warn the user
98+
if (!resolved) {
99+
try {
100+
resolve(pkg, entry, { conditions: ['svelte'] });
101+
102+
if (!warned) {
103+
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'`);
104+
warned = true;
105+
}
106+
} catch (e) {
107+
// do nothing, this isn't a Svelte library
82108
}
83109
}
84110
}

0 commit comments

Comments
 (0)