Skip to content

Commit

Permalink
Merge pull request #206 from sveltejs/warn-about-missing-svelte-condi…
Browse files Browse the repository at this point in the history
…tion

warn about missing svelte export condition
  • Loading branch information
Rich-Harris authored Feb 16, 2023
2 parents f0e64bf + a7463e7 commit 3be2d58
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 25 deletions.
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 });

// 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

0 comments on commit 3be2d58

Please sign in to comment.