Skip to content
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

Add option to ignore warnings #198

Merged
merged 7 commits into from
Feb 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
78 changes: 52 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const path = require('path');
const fs = require('fs');
const relative = require('require-relative');
const { createFilter } = require('@rollup/pluginutils');
const { compile, preprocess } = require('svelte/compiler');

const PREFIX = '[rollup-plugin-svelte]';
const pkg_export_errors = new Set();

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

const parse_pkg = function(dir) {
const pkg_file = path.join(dir, 'package.json');

try {
return JSON.parse(fs.readFileSync(pkg_file, 'utf-8'));
} catch (e) {
return false;
}
}

const get_dir = (file, importer) => relative.resolve(file, path.dirname(importer));

const find_pkg = function(name, importer) {
let dir, pkg;

try {
const file = `${name}/package.json`;
const resolved = get_dir(file, importer);
dir = path.dirname(resolved);
pkg = require(resolved);
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') return {pkg: null, dir};
if (err.code === 'ERR_PACKAGE_PATH_NOT_EXPORTED') {
dir = path.dirname(get_dir(file, importer));

while (dir) {
pkg = parse_pkg(dir);

if (pkg && pkg.name === name) {
return {pkg, dir};
}

const parent = path.dirname(dir);
if (parent === dir) {
break;
}
dir = parent;
}

return {pkg: null, dir};
}

throw err;
}

return {pkg, dir};
}

/**
* @param [options] {Partial<import('.').Options>}
* @returns {import('rollup').Plugin}
Expand Down Expand Up @@ -55,27 +103,15 @@ module.exports = function (options = {}) {
// if this is a bare import, see if there's a valid pkg.svelte
const parts = importee.split('/');

let dir, pkg, name = parts.shift();
let name = parts.shift();
if (name && name[0] === '@') {
name += `/${parts.shift()}`;
}

try {
const file = `${name}/package.json`;
const resolved = relative.resolve(file, path.dirname(importer));
dir = path.dirname(resolved);
pkg = require(resolved);
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') return null;
if (err.code === 'ERR_PACKAGE_PATH_NOT_EXPORTED') {
pkg_export_errors.add(name);
return null;
}
throw err;
}
const {pkg, dir} = find_pkg(name, importer);

// use pkg.svelte
if (parts.length === 0 && pkg.svelte) {
if (parts.length === 0 && pkg && pkg.svelte) {
return path.resolve(dir, pkg.svelte);
}
},
Expand Down Expand Up @@ -129,16 +165,6 @@ module.exports = function (options = {}) {
}

return compiled.js;
},

/**
* All resolutions done; display warnings wrt `package.json` access.
*/
generateBundle() {
if (pkg_export_errors.size > 0) {
console.warn(`\n${PREFIX} 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, s => `- ${s}`).join('\n') + '\n');
}
}
};
};