Skip to content

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

Merged
merged 6 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 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,17 +72,42 @@ 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) 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 });

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should check here if svelte and default point different places and we resolved to default then we should print a warning

Copy link
Member

Choose a reason for hiding this comment

The 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');
warned = true;
}
} catch (e) {
// do nothing, this isn't a Svelte library
}
}
},
Expand Down
20 changes: 17 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
30 changes: 17 additions & 13 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
});

Expand Down