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

chore: fix dynamic imports #2122

Merged
merged 1 commit into from
Nov 6, 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
7 changes: 7 additions & 0 deletions src/pages/[top]/[...slug].astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
import MainLayout from '../../layouts/MainLayout.astro';
import * as path from 'path';
import { fundamentalsEntries } from '../../content';
import { blogEntries } from '../../content';
import { guideEntries } from '../../content';
Expand Down Expand Up @@ -37,6 +38,11 @@ const { entry } = Astro.props;
const { Content } = await entry.render();

const blogData = Astro.props.entry.data;

const getPathMdjsStroriesFile = (entry) => {
const mdjsStroriesFileDirectory = path.dirname(entry.slug);
return `/public/docs/${mdjsStroriesFileDirectory}/__mdjs-stories.js`;
}
---

<MainLayout title={entry.slug}>
Expand All @@ -46,4 +52,5 @@ const blogData = Astro.props.entry.data;
{blogData.tags}
{blogData.author}
<Content />
<script type="module" src={getPathMdjsStroriesFile(entry)} mdjs-setup></script>
</MainLayout>
8 changes: 4 additions & 4 deletions src/pages/components/[component].astro
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ const parseEntries = async () => {

await parseEntries();

const getPathForComponentMdjsStrories = () => {
const componentDirectory = path.dirname(componentEntries[0].slug);
return `/public/docs/${componentDirectory}/__mdjs-stories.js`;
const getPathMdjsStroriesFile = (entry) => {
const mdjsStroriesFileDirectory = path.dirname(entry.slug);
return `/public/docs/${mdjsStroriesFileDirectory}/__mdjs-stories.js`;
}
---

Expand All @@ -54,7 +54,7 @@ const getPathForComponentMdjsStrories = () => {
<content.Content />
))
}
<script type="module" src={getPathForComponentMdjsStrories()} mdjs-setup></script>
<script type="module" src={getPathMdjsStroriesFile(componentEntries[0])} mdjs-setup></script>
</ComponentLayout>
</MainLayout>

Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@ async function processImports(source) {
for (const importObj of imports) {
newSource += source.substring(lastPos, importObj.s);
const importSrc = source.substring(importObj.s, importObj.e);
const isDyncamicImport = importObj.d > -1;

if (importSrc.startsWith('.')) {
if (importSrc.startsWith('.') || importSrc.startsWith('/') || isDyncamicImport) {
newSource += importSrc;
} else if (importSrc === `'@mdjs/mdjs-preview/define'`) {
newSource += `'${nodeModulesText}/@mdjs/mdjs-preview/src/define/define.js'`;
} else if (importSrc === `'@mdjs/mdjs-story/define'`) {
newSource += `'${nodeModulesText}/@mdjs/mdjs-story/src/define.js'`;
} else if (importSrc.startsWith('/')) {
newSource += importSrc;
} else {
newSource += nodeModulesText + require.resolve(importSrc).split(nodeModulesText)[1];
}
Expand Down
Loading