Skip to content

Commit

Permalink
warn about missing svelte export condition
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Feb 15, 2023
1 parent 9744823 commit 9288f82
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions index.js
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');

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

let warned = false;

/**
* @param [options] {Partial<import('.').Options>}
* @returns {import('rollup').Plugin}
Expand Down Expand Up @@ -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 ||
Expand All @@ -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
}
}
}
Expand Down

0 comments on commit 9288f82

Please sign in to comment.