-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make
@astrojs/markdown-remark
browser-safe (#9738)
- Loading branch information
Showing
8 changed files
with
67 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@astrojs/markdown-remark": minor | ||
--- | ||
|
||
Fixes usage in browser environments by using subpath imports |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// This file should be imported as `#import-plugin` | ||
import type * as unified from 'unified'; | ||
|
||
// In the browser, we can try to do a plain import | ||
export async function importPlugin(p: string): Promise<unified.Plugin> { | ||
const importResult = await import(p); | ||
return importResult.default; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// This file should be imported as `#import-plugin` | ||
import { resolve as importMetaResolve } from 'import-meta-resolve'; | ||
import path from 'node:path'; | ||
import { pathToFileURL } from 'node:url'; | ||
import type * as unified from 'unified'; | ||
|
||
let cwdUrlStr: string | undefined; | ||
|
||
// In non-browser enviroments, we can try to resolve from the filesystem too | ||
export async function importPlugin(p: string): Promise<unified.Plugin> { | ||
// Try import from this package first | ||
try { | ||
const importResult = await import(p); | ||
return importResult.default; | ||
} catch {} | ||
|
||
// Try import from user project | ||
cwdUrlStr ??= pathToFileURL(path.join(process.cwd(), 'package.json')).toString(); | ||
const resolved = importMetaResolve(p, cwdUrlStr); | ||
const importResult = await import(resolved); | ||
return importResult.default; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import esbuild from 'esbuild'; | ||
import { expect } from 'chai'; | ||
|
||
describe('Bundle for browsers', async () => { | ||
it('esbuild browser build should work', async () => { | ||
const result = await esbuild.build({ | ||
platform: 'browser', | ||
entryPoints: ['@astrojs/markdown-remark'], | ||
bundle: true, | ||
write: false, | ||
}); | ||
// If some non-browser-safe stuff sneaks in, esbuild should error before reaching here | ||
expect(result.outputFiles.length).to.be.greaterThan(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.