Skip to content

Commit

Permalink
Handle missing export error (#119)
Browse files Browse the repository at this point in the history
* handle missing package.json export

* ignore dupes

* tweak formatting

* reword

* reword

* do not check for existence
  • Loading branch information
pngwn committed Aug 3, 2020
1 parent 97dbb86 commit f49db0f
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 0 deletions.
11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { createFilter } = require('rollup-pluginutils');
const { encode, decode } = require('sourcemap-codec');

const major_version = +version[0];
const pkg_export_errors = new Set();

const { compile, preprocess } = major_version >= 3
? require('svelte/compiler.js')
Expand Down Expand Up @@ -50,6 +51,10 @@ function tryResolve(pkg, importer) {
return relative.resolve(pkg, importer);
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') return null;
if (err.code === 'ERR_PACKAGE_PATH_NOT_EXPORTED') {
pkg_export_errors.add(pkg.replace(/\/package.json$/, ''));
return null;
}
throw err;
}
}
Expand Down Expand Up @@ -334,7 +339,13 @@ module.exports = function svelte(options = {}) {
}, this.warn);

css(writer);


}
if (pkg_export_errors.size < 1) return;

console.warn('\nrollup-plugin-svelte: The following packages did not export their `package.json` file so we could not check the `svelte` field. If you had difficulties importing svelte components from a package, then please contact the author and ask them to export the package.json file.\n');
console.warn(Array.from(pkg_export_errors).map(s => `- ${s}`).join('\n') + '\n');
}
};
};
Empty file.
8 changes: 8 additions & 0 deletions test/node_modules/esm-component/package.json

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

1 change: 1 addition & 0 deletions test/node_modules/esm-component/src/Component.svelte

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

Empty file.
6 changes: 6 additions & 0 deletions test/node_modules/esm-no-pkg-export/package.json

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

16 changes: 16 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ describe('rollup-plugin-svelte', () => {
);
});

it('ignores esm modules that do not export package.json', () => {
const { resolveId } = plugin();
assert.equal(
resolveId('esm-no-pkg-export', path.resolve('test/foo/main.js')),
null
);
});

it('resolves esm module that exports package.json', () => {
const { resolveId } = plugin();
assert.equal(
resolveId('esm-component', path.resolve('test/foo/main.js')),
path.resolve('test/node_modules/esm-component/src/Component.svelte')
);
});

it('ignores virtual modules', () => {
const { resolveId } = plugin();
assert.equal(
Expand Down

0 comments on commit f49db0f

Please sign in to comment.