0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-30 22:03:56 -05:00

Make @astrojs/markdown-remark browser-safe (#9738)

This commit is contained in:
Bjorn Lu 2024-01-20 18:59:40 +08:00 committed by GitHub
parent 53c69dcc82
commit a505190933
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 67 additions and 20 deletions

View file

@ -0,0 +1,5 @@
---
"@astrojs/markdown-remark": minor
---
Fixes usage in browser environments by using subpath imports

View file

@ -16,6 +16,12 @@
".": "./dist/index.js", ".": "./dist/index.js",
"./dist/internal.js": "./dist/internal.js" "./dist/internal.js": "./dist/internal.js"
}, },
"imports": {
"#import-plugin": {
"browser": "./dist/import-plugin-browser.js",
"default": "./dist/import-plugin-default.js"
}
},
"files": [ "files": [
"dist" "dist"
], ],
@ -52,6 +58,7 @@
"@types/unist": "^3.0.2", "@types/unist": "^3.0.2",
"astro-scripts": "workspace:*", "astro-scripts": "workspace:*",
"chai": "^4.3.7", "chai": "^4.3.7",
"esbuild": "^0.19.6",
"mdast-util-mdx-expression": "^2.0.0", "mdast-util-mdx-expression": "^2.0.0",
"mocha": "^10.2.0" "mocha": "^10.2.0"
}, },

View file

@ -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;
}

View file

@ -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;
}

View file

@ -1,26 +1,12 @@
import { resolve as importMetaResolve } from 'import-meta-resolve';
import path from 'node:path';
import { pathToFileURL } from 'node:url';
import type * as unified from 'unified'; import type * as unified from 'unified';
import { importPlugin as _importPlugin } from '#import-plugin';
let cwdUrlStr: string | undefined; async function importPlugin(p: string | unified.Plugin<any[], any>) {
async function importPlugin(p: string | unified.Plugin): Promise<unified.Plugin> {
if (typeof p === 'string') { if (typeof p === 'string') {
// Try import from this package first return await _importPlugin(p);
try { } else {
const importResult = await import(p); return 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;
} }
return p;
} }
export function loadPlugins( export function loadPlugins(

View file

@ -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);
});
});

View file

@ -2,6 +2,7 @@
"extends": "../../../tsconfig.base.json", "extends": "../../../tsconfig.base.json",
"include": ["src"], "include": ["src"],
"compilerOptions": { "compilerOptions": {
"outDir": "./dist" "outDir": "./dist",
"rootDir": "./src",
} }
} }

View file

@ -5059,6 +5059,9 @@ importers:
chai: chai:
specifier: ^4.3.7 specifier: ^4.3.7
version: 4.3.10 version: 4.3.10
esbuild:
specifier: ^0.19.6
version: 0.19.11
mdast-util-mdx-expression: mdast-util-mdx-expression:
specifier: ^2.0.0 specifier: ^2.0.0
version: 2.0.0 version: 2.0.0