diff options
author | 2021-04-30 16:33:35 -0500 | |
---|---|---|
committer | 2021-04-30 16:33:35 -0500 | |
commit | 4df1347156cf2632ea2f3475d3a5f8f08d197cc3 (patch) | |
tree | 9d50de89dfe62827c32a8a4046120af4ab61dc0c /src/compiler/codegen/content.ts | |
parent | 1d498facc8f78a3ffbfecd05cc6ecd45e8a4a1ae (diff) | |
download | astro-4df1347156cf2632ea2f3475d3a5f8f08d197cc3.tar.gz astro-4df1347156cf2632ea2f3475d3a5f8f08d197cc3.tar.zst astro-4df1347156cf2632ea2f3475d3a5f8f08d197cc3.zip |
Migrate to `yarn` monorepo (#157)
* chore: use monorepo
* chore: scaffold astro-scripts
* chore: move tests inside packages/astro
* chore: refactor tests, add scripts
* chore: move parser to own module
* chore: move runtime to packages/astro
* fix: move parser to own package
* test: fix prettier-plugin-astro tests
* fix: tests
* chore: update package-lock
* chore: add changesets
* fix: cleanup examples
* fix: starter example
* chore: update changeset config
* chore: update changeset config
* chore: setup changeset release workflow
* chore: bump lockfiles
* chore: prism => astro-prism
* fix: tsc --emitDeclarationOnly
* chore: final cleanup, switch to yarn
* chore: add lerna
* chore: update workflows to yarn
* chore: update workflows
* chore: remove lint workflow
* chore: add astro-dev script
* chore: add symlinked README
Diffstat (limited to 'src/compiler/codegen/content.ts')
-rw-r--r-- | src/compiler/codegen/content.ts | 78 |
1 files changed, 0 insertions, 78 deletions
diff --git a/src/compiler/codegen/content.ts b/src/compiler/codegen/content.ts deleted file mode 100644 index fb8f9e307..000000000 --- a/src/compiler/codegen/content.ts +++ /dev/null @@ -1,78 +0,0 @@ -import path from 'path'; -import { fdir, PathsOutput } from 'fdir'; - -/** - * Handling for import.meta.glob and import.meta.globEager - */ - -interface GlobOptions { - namespace: string; - filename: string; -} - -interface GlobResult { - /** Array of import statements to inject */ - imports: Set<string>; - /** Replace original code with */ - code: string; -} - -const crawler = new fdir(); - -/** General glob handling */ -function globSearch(spec: string, { filename }: { filename: string }): string[] { - try { - // Note: fdir’s glob requires you to do some work finding the closest non-glob folder. - // For example, this fails: .glob("./post/*.md").crawl("/…/src/pages") ❌ - // …but this doesn’t: .glob("*.md").crawl("/…/src/pages/post") ✅ - let globDir = ''; - let glob = spec; - for (const part of spec.split('/')) { - if (!part.includes('*')) { - // iterate through spec until first '*' is reached - globDir = path.posix.join(globDir, part); // this must be POSIX-style - glob = glob.replace(`${part}/`, ''); // move parent dirs off spec, and onto globDir - } else { - // at first '*', exit - break; - } - } - - const cwd = path.join(path.dirname(filename), globDir.replace(/\//g, path.sep)); // this must match OS (could be '/' or '\') - let found = crawler.glob(glob).crawl(cwd).sync() as PathsOutput; - if (!found.length) { - throw new Error(`No files matched "${spec}" from ${filename}`); - } - return found.map((importPath) => { - if (importPath.startsWith('http') || importPath.startsWith('.')) return importPath; - return `./` + globDir + '/' + importPath; - }); - } catch (err) { - throw new Error(`No files matched "${spec}" from ${filename}`); - } -} - -/** Astro.fetchContent() */ -export function fetchContent(spec: string, { namespace, filename }: GlobOptions): GlobResult { - let code = ''; - const imports = new Set<string>(); - const importPaths = globSearch(spec, { filename }); - - // gather imports - importPaths.forEach((importPath, j) => { - const id = `${namespace}_${j}`; - imports.add(`import { __content as ${id} } from '${importPath}';`); - - // add URL if this appears within the /pages/ directory (probably can be improved) - const fullPath = path.resolve(path.dirname(filename), importPath); - if (fullPath.includes(`${path.sep}pages${path.sep}`)) { - const url = importPath.replace(/^\./, '').replace(/\.md$/, ''); - imports.add(`${id}.url = '${url}';`); - } - }); - - // generate replacement code - code += `${namespace} = [${importPaths.map((_, j) => `${namespace}_${j}`).join(',')}];\n`; - - return { imports, code }; -} |