Skip to content

Commit

Permalink
feat: support wildcard export in svelte entry point (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
ethan-jones-vizio committed May 18, 2022
1 parent ee741b4 commit d502bdb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.DS_Store
.idea
lib
build
dist
node_modules
pnpm*
pnpm*
38 changes: 34 additions & 4 deletions src/parse-exports.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import * as acorn from "acorn";
import * as fs from "fs";
import path from "path";
import { normalizeSeparators } from "./path";

interface NodeImportDeclaration extends acorn.Node {
type: "ImportDeclaration";
Expand All @@ -16,11 +19,15 @@ interface NodeExportDefaultDeclaration extends acorn.Node {
declaration: { name: string };
}

type BodyNode = NodeImportDeclaration | NodeExportNamedDeclaration | NodeExportDefaultDeclaration;
interface NodeExportAllDeclaration extends acorn.Node, Pick<NodeImportDeclaration, "source"> {
type: "ExportAllDeclaration";
}

type BodyNode = NodeImportDeclaration | NodeExportNamedDeclaration | NodeExportDefaultDeclaration | NodeExportAllDeclaration;

export type ParsedExports = Record<string, { source: string; default: boolean; mixed?: boolean }>;

export function parseExports(source: string) {
export function parseExports(source: string, dir: string) {
const ast = acorn.parse(source, {
ecmaVersion: "latest",
sourceType: "module",
Expand All @@ -38,9 +45,32 @@ export function parseExports(source: string) {
} else {
exports_by_identifier[id] = { source: "", default: true };
}
}
} else if (node.type === "ExportAllDeclaration") {
if (!node.source) return;

if (node.type === "ExportNamedDeclaration") {
let file_path = path.resolve(dir, node.source.value);

if (!fs.lstatSync(file_path).isFile()){
const files = fs.readdirSync(file_path);

for (const file of files)
if (file.includes("index")) {
file_path = path.join(file_path, file);
break;
}
}

const export_file = fs.readFileSync(file_path, "utf-8");
const exports = parseExports(export_file, path.dirname(file_path));

for (const [key, value] of Object.entries(exports)) {
const source = normalizeSeparators("./" + path.join(node.source.value, value.source));
exports_by_identifier[key] = {
...value,
source
};
}
} else if (node.type === "ExportNamedDeclaration") {
node.specifiers.forEach((specifier) => {
const exported_name = specifier.exported.name;
const id = exported_name || specifier.local.name;
Expand Down
2 changes: 1 addition & 1 deletion src/rollup-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ interface GenerateBundleResult {
export async function generateBundle(input: string, glob: boolean) {
const dir = fs.lstatSync(input).isFile() ? path.dirname(input) : input;
const entry = fs.readFileSync(input, "utf-8");
const exports = parseExports(entry);
const exports = parseExports(entry, dir);

if (glob) {
fg.sync([`${dir}/**/*.svelte`]).forEach((file) => {
Expand Down

0 comments on commit d502bdb

Please sign in to comment.