@web/dev-server-core
Advanced tools
Comparing version
# @web/dev-server-core | ||
## 0.2.18 | ||
### Patch Changes | ||
- 07edac1: improve handling of dynamic imports | ||
## 0.2.17 | ||
@@ -4,0 +10,0 @@ |
@@ -15,2 +15,3 @@ "use strict"; | ||
const utils_1 = require("../utils"); | ||
const parseDynamicImport_1 = require("./parseDynamicImport"); | ||
const CONCAT_NO_PACKAGE_ERROR = 'Dynamic import with a concatenated string should start with a valid full package name.'; | ||
@@ -26,2 +27,6 @@ /** | ||
let pathToAppend = ''; | ||
if (['/', '../', './'].some(p => pathToResolve.startsWith(p))) { | ||
// don't handle non-bare imports | ||
return pathToResolve; | ||
} | ||
const parts = importSpecifier.split('/'); | ||
@@ -96,15 +101,15 @@ if (importSpecifier.startsWith('@')) { | ||
// dynamic import | ||
const dynamicStart = start + 1; | ||
const dynamicEnd = end - 1; | ||
const importSpecifier = code.substring(dynamicStart, dynamicEnd); | ||
const stringSymbol = code[dynamicStart - 1]; | ||
const isStringLiteral = [`\``, "'", '"'].includes(stringSymbol); | ||
const concatenatedString = stringSymbol === `\`` || importSpecifier.includes("'") || importSpecifier.includes('"'); | ||
const { importString, importSpecifier, stringLiteral, concatenatedString, dynamicStart, dynamicEnd, } = parseDynamicImport_1.parseDynamicImport(code, start, end); | ||
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, code, line, column) | ||
: importSpecifier; | ||
resolvedSource += `${code.substring(lastIndex, dynamicStart)}${resolvedImport}`; | ||
let rewrittenImport; | ||
if (stringLiteral) { | ||
const resolvedImport = await maybeResolveImport(importSpecifier, concatenatedString, resolveImport, code, line, column); | ||
rewrittenImport = `${importString[0]}${resolvedImport}${importString[importString.length - 1]}`; | ||
} | ||
else { | ||
rewrittenImport = importString; | ||
} | ||
resolvedSource += `${code.substring(lastIndex, dynamicStart)}${rewrittenImport}`; | ||
lastIndex = dynamicEnd; | ||
@@ -111,0 +116,0 @@ } |
{ | ||
"name": "@web/dev-server-core", | ||
"version": "0.2.17", | ||
"version": "0.2.18", | ||
"publishConfig": { | ||
@@ -17,2 +17,15 @@ "access": "public" | ||
"main": "dist/index.js", | ||
"exports": { | ||
".": { | ||
"import": "./index.mjs", | ||
"require": "./dist/index.js" | ||
}, | ||
"./dist/dom5": { | ||
"require": "./dist/dom5/index.js" | ||
}, | ||
"./test-helpers": { | ||
"import": "./test-helpers.mjs", | ||
"require": "./dist/test-helpers.js" | ||
} | ||
}, | ||
"engines": { | ||
@@ -28,10 +41,10 @@ "node": ">=10.0.0" | ||
"test": "mocha \"test/**/*.test.{ts,js,mjs,cjs}\" --require ts-node/register --reporter dot", | ||
"test:watch": "mocha \"test/**/*.test.{ts,js,mjs,cjs}\" --require ts-node/register --reporter dot --watch" | ||
"test:watch": "mocha \"test/**/*.test.{ts,js,mjs,cjs}\" --require ts-node/register --reporter dot --watch --watch-files src,test" | ||
}, | ||
"files": [ | ||
".self-signed-dev-server-ssl.cert", | ||
".self-signed-dev-server-ssl.key", | ||
"*.d.ts", | ||
"*.js", | ||
"*.mjs", | ||
".self-signed-dev-server-ssl.cert", | ||
".self-signed-dev-server-ssl.key", | ||
"dist", | ||
@@ -51,5 +64,5 @@ "src" | ||
"@web/parse5-utils": "^1.0.0", | ||
"chokidar": "^3.4.0", | ||
"chokidar": "^3.4.3", | ||
"clone": "^2.1.2", | ||
"es-module-lexer": "^0.3.24", | ||
"es-module-lexer": "^0.3.26", | ||
"get-stream": "^6.0.0", | ||
@@ -63,3 +76,3 @@ "is-stream": "^2.0.0", | ||
"mime-types": "^2.1.27", | ||
"parse5": "^6.0.0", | ||
"parse5": "^6.0.1", | ||
"picomatch": "^2.2.2", | ||
@@ -69,3 +82,3 @@ "ws": "^7.3.1" | ||
"devDependencies": { | ||
"@types/clone": "^0.1.30", | ||
"@types/clone": "^2.1.0", | ||
"@types/lru-cache": "^5.1.0", | ||
@@ -75,19 +88,6 @@ "@types/parse5": "^5.0.3", | ||
"abort-controller": "^3.0.0", | ||
"node-fetch": "v3.0.0-beta.9", | ||
"portfinder": "^1.0.26", | ||
"node-fetch": "3.0.0-beta.9", | ||
"portfinder": "^1.0.28", | ||
"uuid": "^8.2.0" | ||
}, | ||
"exports": { | ||
".": { | ||
"import": "./index.mjs", | ||
"require": "./dist/index.js" | ||
}, | ||
"./dist/dom5": { | ||
"require": "./dist/dom5/index.js" | ||
}, | ||
"./test-helpers": { | ||
"import": "./test-helpers.mjs", | ||
"require": "./dist/test-helpers.js" | ||
} | ||
} | ||
} |
@@ -12,2 +12,3 @@ /* eslint-disable @typescript-eslint/ban-ts-comment */ | ||
import { Logger } from '../logger/Logger'; | ||
import { parseDynamicImport } from './parseDynamicImport'; | ||
@@ -48,2 +49,7 @@ export type ResolveImport = ( | ||
if (['/', '../', './'].some(p => pathToResolve.startsWith(p))) { | ||
// don't handle non-bare imports | ||
return pathToResolve; | ||
} | ||
const parts = importSpecifier.split('/'); | ||
@@ -151,11 +157,11 @@ if (importSpecifier.startsWith('@')) { | ||
// dynamic import | ||
const dynamicStart = start + 1; | ||
const dynamicEnd = end - 1; | ||
const { | ||
importString, | ||
importSpecifier, | ||
stringLiteral, | ||
concatenatedString, | ||
dynamicStart, | ||
dynamicEnd, | ||
} = parseDynamicImport(code, start, end); | ||
const importSpecifier = code.substring(dynamicStart, dynamicEnd); | ||
const stringSymbol = code[dynamicStart - 1]; | ||
const isStringLiteral = [`\``, "'", '"'].includes(stringSymbol); | ||
const concatenatedString = | ||
stringSymbol === `\`` || importSpecifier.includes("'") || importSpecifier.includes('"'); | ||
const lines = code.slice(0, dynamicStart).split('\n'); | ||
@@ -165,14 +171,20 @@ const line = lines.length; | ||
const resolvedImport = isStringLiteral | ||
? await maybeResolveImport( | ||
importSpecifier, | ||
concatenatedString, | ||
resolveImport, | ||
code, | ||
line, | ||
column, | ||
) | ||
: importSpecifier; | ||
let rewrittenImport; | ||
if (stringLiteral) { | ||
const resolvedImport = await maybeResolveImport( | ||
importSpecifier, | ||
concatenatedString, | ||
resolveImport, | ||
code, | ||
line, | ||
column, | ||
); | ||
rewrittenImport = `${importString[0]}${resolvedImport}${ | ||
importString[importString.length - 1] | ||
}`; | ||
} else { | ||
rewrittenImport = importString; | ||
} | ||
resolvedSource += `${code.substring(lastIndex, dynamicStart)}${resolvedImport}`; | ||
resolvedSource += `${code.substring(lastIndex, dynamicStart)}${rewrittenImport}`; | ||
lastIndex = dynamicEnd; | ||
@@ -179,0 +191,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
274372
2.3%176
2.92%5195
2.4%Updated
Updated
Updated