@web/dev-server-core
Advanced tools
Comparing version 0.2.5 to 0.2.6
# @web/dev-server-core | ||
## 0.2.6 | ||
### Patch Changes | ||
- cd1213e: improved logging of resolving outside root dir | ||
## 0.2.5 | ||
@@ -4,0 +10,0 @@ |
@@ -6,4 +6,2 @@ "use strict"; | ||
const utils_1 = require("../utils"); | ||
const PluginSyntaxError_1 = require("../logger/PluginSyntaxError"); | ||
const PluginError_1 = require("../logger/PluginError"); | ||
/** | ||
@@ -73,7 +71,7 @@ * Sets up a middleware which allows plugins to transform files before they are served to the browser. | ||
context.status = 500; | ||
if (error instanceof PluginSyntaxError_1.PluginSyntaxError) { | ||
if (error.name === 'PluginSyntaxError') { | ||
logger.logSyntaxError(error); | ||
return; | ||
} | ||
if (error instanceof PluginError_1.PluginError) { | ||
if (error.name === 'PluginError') { | ||
logger.error(error.message); | ||
@@ -80,0 +78,0 @@ return; |
@@ -43,2 +43,5 @@ /// <reference types="node" /> | ||
context: Context; | ||
code?: string; | ||
column?: number; | ||
line?: number; | ||
}): ResolveResult | Promise<ResolveResult>; | ||
@@ -48,2 +51,5 @@ transformImport?(args: { | ||
context: Context; | ||
code?: string; | ||
column?: number; | ||
line?: number; | ||
}): ResolveResult | Promise<ResolveResult>; | ||
@@ -50,0 +56,0 @@ resolveMimeType?(context: Context): ResolveMimeTypeResult | Promise<ResolveMimeTypeResult>; |
import { Plugin } from '../Plugin'; | ||
export declare type ResolveImport = (source: string) => string | undefined | Promise<string | undefined>; | ||
export declare type ResolveImport = (source: string, code: string, line: number, column: number) => string | undefined | Promise<string | undefined>; | ||
export declare function transformImports(code: string, filePath: string, resolveImport: ResolveImport): Promise<string>; | ||
export declare function transformModuleImportsPlugin(plugins: Plugin[], rootDir: string): Plugin; | ||
//# sourceMappingURL=transformModuleImportsPlugin.d.ts.map |
@@ -22,3 +22,3 @@ "use strict"; | ||
*/ | ||
async function resolveConcatenatedImport(importSpecifier, resolveImport) { | ||
async function resolveConcatenatedImport(importSpecifier, resolveImport, code, line, column) { | ||
let pathToResolve = importSpecifier; | ||
@@ -44,3 +44,3 @@ let pathToAppend = ''; | ||
const packagePath = `${pathToResolve}/package.json`; | ||
const resolvedPackage = await resolveImport(packagePath); | ||
const resolvedPackage = await resolveImport(packagePath, code, line, column); | ||
if (!resolvedPackage) { | ||
@@ -52,3 +52,3 @@ throw new Error(`Could not resolve conatenated dynamic import, could not find ${packagePath}`); | ||
} | ||
async function maybeResolveImport(importSpecifier, concatenatedString, resolveImport) { | ||
async function maybeResolveImport(importSpecifier, concatenatedString, resolveImport, code, line, column) { | ||
var _a, _b; | ||
@@ -59,3 +59,3 @@ let resolvedImportFilePath; | ||
try { | ||
resolvedImportFilePath = (_a = (await resolveConcatenatedImport(importSpecifier, resolveImport))) !== null && _a !== void 0 ? _a : importSpecifier; | ||
resolvedImportFilePath = (_a = (await resolveConcatenatedImport(importSpecifier, resolveImport, code, line, column))) !== null && _a !== void 0 ? _a : importSpecifier; | ||
} | ||
@@ -67,3 +67,3 @@ catch (error) { | ||
else { | ||
resolvedImportFilePath = (_b = (await resolveImport(importSpecifier))) !== null && _b !== void 0 ? _b : importSpecifier; | ||
resolvedImportFilePath = (_b = (await resolveImport(importSpecifier, code, line, column))) !== null && _b !== void 0 ? _b : importSpecifier; | ||
} | ||
@@ -90,3 +90,6 @@ return resolvedImportFilePath; | ||
const importSpecifier = code.substring(start, end); | ||
const resolvedImport = await maybeResolveImport(importSpecifier, false, resolveImport); | ||
const lines = code.slice(0, end).split('\n'); | ||
const line = lines.length; | ||
const column = lines[lines.length - 1].indexOf(importSpecifier); | ||
const resolvedImport = await maybeResolveImport(importSpecifier, false, resolveImport, code, line, column); | ||
resolvedSource += `${code.substring(lastIndex, start)}${resolvedImport}`; | ||
@@ -103,4 +106,7 @@ lastIndex = end; | ||
const concatenatedString = stringSymbol === `\`` || importSpecifier.includes("'") || importSpecifier.includes('"'); | ||
const lines = code.slice(0, dynamicStart).split('\n'); | ||
const line = lines.length; | ||
const column = lines[lines.length - 1].indexOf('import(') || 0; | ||
const resolvedImport = isStringLiteral | ||
? await maybeResolveImport(importSpecifier, concatenatedString, resolveImport) | ||
? await maybeResolveImport(importSpecifier, concatenatedString, resolveImport, code, line, column) | ||
: importSpecifier; | ||
@@ -119,6 +125,6 @@ resolvedSource += `${code.substring(lastIndex, dynamicStart)}${resolvedImport}`; | ||
const filePath = path_1.default.join(rootDir, utils_1.toFilePath(context.path)); | ||
async function resolveImport(source) { | ||
async function resolveImport(source, code, column, line) { | ||
var _a; | ||
for (const plugin of resolvePlugins) { | ||
const resolved = await ((_a = plugin.resolveImport) === null || _a === void 0 ? void 0 : _a.call(plugin, { source, context })); | ||
const resolved = await ((_a = plugin.resolveImport) === null || _a === void 0 ? void 0 : _a.call(plugin, { source, context, code, column, line })); | ||
if (typeof resolved === 'string') { | ||
@@ -132,7 +138,12 @@ return resolved; | ||
} | ||
async function transformImport(source) { | ||
async function transformImport(source, code, column, line) { | ||
var _a, _b; | ||
let resolvedImport = (_a = (await resolveImport(source))) !== null && _a !== void 0 ? _a : source; | ||
let resolvedImport = (_a = (await resolveImport(source, code, column, line))) !== null && _a !== void 0 ? _a : source; | ||
for (const plugin of resolvePlugins) { | ||
const resolved = await ((_b = plugin.transformImport) === null || _b === void 0 ? void 0 : _b.call(plugin, { source: resolvedImport, context })); | ||
const resolved = await ((_b = plugin.transformImport) === null || _b === void 0 ? void 0 : _b.call(plugin, { | ||
source: resolvedImport, | ||
context, | ||
column, | ||
line, | ||
})); | ||
if (typeof resolved === 'string') { | ||
@@ -139,0 +150,0 @@ resolvedImport = resolved; |
{ | ||
"name": "@web/dev-server-core", | ||
"version": "0.2.5", | ||
"version": "0.2.6", | ||
"publishConfig": { | ||
@@ -5,0 +5,0 @@ "access": "public" |
@@ -7,5 +7,3 @@ import { FSWatcher } from 'chokidar'; | ||
import { getResponseBody, RequestCancelledError } from '../utils'; | ||
import { PluginSyntaxError } from '../logger/PluginSyntaxError'; | ||
import { Logger } from '../logger/Logger'; | ||
import { PluginError } from '../logger/PluginError'; | ||
@@ -86,3 +84,3 @@ /** | ||
if (error instanceof PluginSyntaxError) { | ||
if (error.name === 'PluginSyntaxError') { | ||
logger.logSyntaxError(error); | ||
@@ -92,3 +90,3 @@ return; | ||
if (error instanceof PluginError) { | ||
if (error.name === 'PluginError') { | ||
logger.error(error.message); | ||
@@ -95,0 +93,0 @@ return; |
@@ -40,2 +40,5 @@ import { FSWatcher } from 'chokidar'; | ||
context: Context; | ||
code?: string; | ||
column?: number; | ||
line?: number; | ||
}): ResolveResult | Promise<ResolveResult>; | ||
@@ -45,4 +48,7 @@ transformImport?(args: { | ||
context: Context; | ||
code?: string; | ||
column?: number; | ||
line?: number; | ||
}): ResolveResult | Promise<ResolveResult>; | ||
resolveMimeType?(context: Context): ResolveMimeTypeResult | Promise<ResolveMimeTypeResult>; | ||
} |
@@ -12,3 +12,8 @@ /* eslint-disable @typescript-eslint/ban-ts-comment */ | ||
export type ResolveImport = (source: string) => string | undefined | Promise<string | undefined>; | ||
export type ResolveImport = ( | ||
source: string, | ||
code: string, | ||
line: number, | ||
column: number, | ||
) => string | undefined | Promise<string | undefined>; | ||
@@ -35,2 +40,5 @@ interface ParsedImport { | ||
resolveImport: ResolveImport, | ||
code: string, | ||
line: number, | ||
column: number, | ||
): Promise<string> { | ||
@@ -60,3 +68,3 @@ let pathToResolve = importSpecifier; | ||
const packagePath = `${pathToResolve}/package.json`; | ||
const resolvedPackage = await resolveImport(packagePath); | ||
const resolvedPackage = await resolveImport(packagePath, code, line, column); | ||
if (!resolvedPackage) { | ||
@@ -74,2 +82,5 @@ throw new Error(`Could not resolve conatenated dynamic import, could not find ${packagePath}`); | ||
resolveImport: ResolveImport, | ||
code: string, | ||
line: number, | ||
column: number, | ||
) { | ||
@@ -82,3 +93,4 @@ let resolvedImportFilePath; | ||
resolvedImportFilePath = | ||
(await resolveConcatenatedImport(importSpecifier, resolveImport)) ?? importSpecifier; | ||
(await resolveConcatenatedImport(importSpecifier, resolveImport, code, line, column)) ?? | ||
importSpecifier; | ||
} catch (error) { | ||
@@ -88,3 +100,4 @@ return importSpecifier; | ||
} else { | ||
resolvedImportFilePath = (await resolveImport(importSpecifier)) ?? importSpecifier; | ||
resolvedImportFilePath = | ||
(await resolveImport(importSpecifier, code, line, column)) ?? importSpecifier; | ||
} | ||
@@ -123,5 +136,17 @@ return resolvedImportFilePath; | ||
// static import | ||
const importSpecifier = code.substring(start, end); | ||
const resolvedImport = await maybeResolveImport(importSpecifier, false, resolveImport); | ||
const lines = code.slice(0, end).split('\n'); | ||
const line = lines.length; | ||
const column = lines[lines.length - 1].indexOf(importSpecifier); | ||
const resolvedImport = await maybeResolveImport( | ||
importSpecifier, | ||
false, | ||
resolveImport, | ||
code, | ||
line, | ||
column, | ||
); | ||
resolvedSource += `${code.substring(lastIndex, start)}${resolvedImport}`; | ||
@@ -139,4 +164,16 @@ lastIndex = end; | ||
stringSymbol === `\`` || importSpecifier.includes("'") || importSpecifier.includes('"'); | ||
const lines = code.slice(0, dynamicStart).split('\n'); | ||
const line = lines.length; | ||
const column = lines[lines.length - 1].indexOf('import(') || 0; | ||
const resolvedImport = isStringLiteral | ||
? await maybeResolveImport(importSpecifier, concatenatedString, resolveImport) | ||
? await maybeResolveImport( | ||
importSpecifier, | ||
concatenatedString, | ||
resolveImport, | ||
code, | ||
line, | ||
column, | ||
) | ||
: importSpecifier; | ||
@@ -164,5 +201,5 @@ | ||
async function resolveImport(source: string) { | ||
async function resolveImport(source: string, code: string, column: number, line: number) { | ||
for (const plugin of resolvePlugins) { | ||
const resolved = await plugin.resolveImport?.({ source, context }); | ||
const resolved = await plugin.resolveImport?.({ source, context, code, column, line }); | ||
if (typeof resolved === 'string') { | ||
@@ -178,6 +215,11 @@ return resolved; | ||
async function transformImport(source: string) { | ||
let resolvedImport = (await resolveImport(source)) ?? source; | ||
async function transformImport(source: string, code: string, column: number, line: number) { | ||
let resolvedImport = (await resolveImport(source, code, column, line)) ?? source; | ||
for (const plugin of resolvePlugins) { | ||
const resolved = await plugin.transformImport?.({ source: resolvedImport, context }); | ||
const resolved = await plugin.transformImport?.({ | ||
source: resolvedImport, | ||
context, | ||
column, | ||
line, | ||
}); | ||
if (typeof resolved === 'string') { | ||
@@ -184,0 +226,0 @@ resolvedImport = resolved; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
249424
4664