-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for npm modules (#454)
* feat: simplify `ImportMap` * refactor: tweak `filterScopes` * chore: fix test * feat: add support for npm modules * refactor: tidy up * fix: only process import statements * feat: support unprefixed Node.js built-ins * feat: add `try/catch` * refactor: convert stub path to file URL * refactor: simplify code * refactor: rename variable * refactor: rename variable * refactor: use `builtinModules` from `node:module` * refactor: add try/catch * chore: update lock file
- Loading branch information
1 parent
3d4ea09
commit 3d8b3f3
Showing
28 changed files
with
1,219 additions
and
355 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
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
*.swp | ||
npm-debug.log | ||
node_modules | ||
!test/fixtures/**/node_modules | ||
/core | ||
.eslintcache | ||
.npmrc | ||
|
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 |
---|---|---|
|
@@ -3,7 +3,7 @@ import { build, LoadResponse } from 'https://deno.land/x/[email protected]/mod.ts' | |
import * as path from 'https://deno.land/[email protected]/path/mod.ts' | ||
|
||
import type { InputFunction, WriteStage2Options } from '../../shared/stage2.ts' | ||
import { importMapSpecifier, virtualRoot } from '../../shared/consts.ts' | ||
import { importMapSpecifier, virtualRoot, virtualVendorRoot } from '../../shared/consts.ts' | ||
import { LEGACY_PUBLIC_SPECIFIER, PUBLIC_SPECIFIER, STAGE2_SPECIFIER } from './consts.ts' | ||
import { inlineModule, loadFromVirtualRoot, loadWithRetry } from './common.ts' | ||
|
||
|
@@ -63,7 +63,13 @@ const getVirtualPath = (basePath: string, filePath: string) => { | |
return url | ||
} | ||
|
||
const stage2Loader = (basePath: string, functions: InputFunction[], externals: Set<string>, importMapData?: string) => { | ||
const stage2Loader = ( | ||
basePath: string, | ||
functions: InputFunction[], | ||
externals: Set<string>, | ||
importMapData: string | undefined, | ||
vendorDirectory?: string, | ||
) => { | ||
return async (specifier: string): Promise<LoadResponse | undefined> => { | ||
if (specifier === STAGE2_SPECIFIER) { | ||
const stage2Entry = getStage2Entry(basePath, functions) | ||
|
@@ -91,13 +97,24 @@ const stage2Loader = (basePath: string, functions: InputFunction[], externals: S | |
return loadFromVirtualRoot(specifier, virtualRoot, basePath) | ||
} | ||
|
||
if (vendorDirectory !== undefined && specifier.startsWith(virtualVendorRoot)) { | ||
return loadFromVirtualRoot(specifier, virtualVendorRoot, vendorDirectory) | ||
} | ||
|
||
return await loadWithRetry(specifier) | ||
} | ||
} | ||
|
||
const writeStage2 = async ({ basePath, destPath, externals, functions, importMapData }: WriteStage2Options) => { | ||
const writeStage2 = async ({ | ||
basePath, | ||
destPath, | ||
externals, | ||
functions, | ||
importMapData, | ||
vendorDirectory, | ||
}: WriteStage2Options) => { | ||
const importMapURL = importMapData ? importMapSpecifier : undefined | ||
const loader = stage2Loader(basePath, functions, new Set(externals), importMapData) | ||
const loader = stage2Loader(basePath, functions, new Set(externals), importMapData, vendorDirectory) | ||
const bytes = await build([STAGE2_SPECIFIER], loader, importMapURL) | ||
const directory = path.dirname(destPath) | ||
|
||
|
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
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
Oops, something went wrong.