ui5-tooling-transpile
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -6,2 +6,13 @@ # Change Log | ||
## [0.2.1](https://github.com/ui5-community/ui5-ecosystem-showcase/compare/ui5-tooling-transpile@0.2.0...ui5-tooling-transpile@0.2.1) (2022-12-01) | ||
### Bug Fixes | ||
* **ui5-tooling-transpile:** fix error when no ts file matches for js request ([faea158](https://github.com/ui5-community/ui5-ecosystem-showcase/commit/faea158d66dffee1d924c15e6442e1dd634f3945)), closes [#650](https://github.com/ui5-community/ui5-ecosystem-showcase/issues/650) | ||
# 0.2.0 (2022-08-17) | ||
@@ -8,0 +19,0 @@ |
@@ -1,6 +0,6 @@ | ||
const babel = require("@babel/core"); | ||
const log = require("@ui5/logger").getLogger("server:custommiddleware:ui5-tooling-transpile"); | ||
const parseurl = require("parseurl"); | ||
const babel = require("@babel/core") | ||
const log = require("@ui5/logger").getLogger("server:custommiddleware:ui5-tooling-transpile") | ||
const parseurl = require("parseurl") | ||
const merge = require("lodash.merge") | ||
const { createBabelConfig, normalizeLineFeeds } = require("./util"); | ||
const { createBabelConfig, normalizeLineFeeds } = require("./util") | ||
@@ -24,75 +24,71 @@ /** | ||
*/ | ||
module.exports = function ({ | ||
resources, | ||
options, | ||
middlewareUtil | ||
}) { | ||
module.exports = function ({ resources, options /*, middlewareUtil */ }) { | ||
const config = options.configuration || {} | ||
const config = options.configuration || {}; | ||
const babelConfig = createBabelConfig({ configuration: config, isMiddleware: true }) | ||
const babelConfig = createBabelConfig({configuration: config, isMiddleware: true}); | ||
let filePatternConfig = config.filePattern | ||
let filePatternConfig = config.filePattern; | ||
if (!filePatternConfig) { | ||
filePatternConfig = config.transpileTypeScript ? ".ts" : ".js" | ||
} | ||
if (!filePatternConfig) { | ||
filePatternConfig = config.transpileTypeScript ? ".ts" : ".js"; | ||
} | ||
return async (req, res, next) => { | ||
if ( | ||
req.path && | ||
req.path.endsWith(".js") && | ||
!req.path.includes("resources/") && | ||
!(config.excludePatterns || []).some((pattern) => req.path.includes(pattern)) | ||
) { | ||
const pathname = parseurl(req).pathname | ||
const pathWithPattern = pathname.replace(".js", filePatternConfig) | ||
log.info(`---> ${pathWithPattern}`) | ||
return async (req, res, next) => { | ||
try { | ||
// grab the file via @ui5/fs.AbstractReader API | ||
const matchedResources = await resources.rootProject.byGlob(pathWithPattern) | ||
config.debug && log.info(`handling ${req.path}...`) | ||
if ( | ||
req.path && | ||
req.path.endsWith(".js") && | ||
!req.path.includes("resources/") && | ||
!(config.excludePatterns || []).some((pattern) => req.path.includes(pattern)) | ||
) { | ||
if (!matchedResources || !matchedResources.length) { | ||
const fileNotFoundError = new Error(`No match found for ${req.path}`) | ||
fileNotFoundError.code = 404 | ||
fileNotFoundError.file = pathWithPattern | ||
throw fileNotFoundError | ||
} | ||
const pathname = parseurl(req).pathname; | ||
const pathWithPattern = pathname.replace(".js", filePatternConfig); | ||
log.info(`---> ${pathWithPattern}`); | ||
// prefer js over other extensions, otherwise grab first possible path | ||
const resource = matchedResources.find((r) => r.getPath() === pathname) || matchedResources[0] | ||
if (matchedResources.length > 1) { | ||
log.warn( | ||
`found more than 1 file for given pattern (${filePatternConfig}): ${matchedResources | ||
.map((r) => r.getPath()) | ||
.join(", ")} ` | ||
) | ||
log.info(`using: ${resource.getPath()}`) | ||
} | ||
try { | ||
// read file into string | ||
const source = await resource.getString() | ||
// grab the file via @ui5/fs.AbstractReader API | ||
const matchedResources = await resources.rootProject.byGlob(pathWithPattern); | ||
config.debug && log.info(`handling ${req.path}...`); | ||
config.debug ? log.info(`...${pathWithPattern} transpiled!`) : null | ||
const babelConfigForFile = merge({}, babelConfig, { | ||
filename: pathWithPattern // necessary for source map <-> source assoc | ||
}) | ||
let result = await babel.transformAsync(source, babelConfigForFile) | ||
if (!matchedResources || !matchedResources.length) { | ||
fileNotFoundError.file = pathWithPattern; | ||
throw fileNotFoundError; | ||
} | ||
// prefer js over other extensions, otherwise grab first possible path | ||
const resource = matchedResources.find((r) => r.getPath() === pathname) || matchedResources[0] | ||
if (matchedResources.length > 1) { | ||
log.warn(`found more than 1 file for given pattern (${filePatternConfig}): ${matchedResources.map((r) => r.getPath()).join(", ")} `); | ||
log.info(`using: ${resource.getPath()}`); | ||
} | ||
// read file into string | ||
const source = await resource.getString(); | ||
config.debug ? log.info(`...${pathWithPattern} transpiled!`) : null | ||
const babelConfigForFile = merge({}, babelConfig, { | ||
filename: pathWithPattern // necessary for source map <-> source assoc | ||
}) | ||
let result = await babel.transformAsync(source, babelConfigForFile); | ||
// send out transpiled source + source map | ||
res.type(".js"); | ||
res.end(normalizeLineFeeds(result.code)); | ||
} catch(err) { | ||
if (err.code === 404) { | ||
log.warn(`...file not found: ${err.file}!`) | ||
} else { | ||
log.error(err) | ||
} | ||
next(); | ||
} | ||
} | ||
else { | ||
next(); | ||
// send out transpiled source + source map | ||
res.type(".js") | ||
res.end(normalizeLineFeeds(result.code)) | ||
} catch (err) { | ||
if (err.code === 404) { | ||
log.warn(`...file not found: ${err.file}!`) | ||
} else { | ||
log.error(err) | ||
} | ||
next() | ||
} | ||
} else { | ||
next() | ||
} | ||
} | ||
} | ||
} |
{ | ||
"name": "ui5-tooling-transpile", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "UI5 tooling extensions to transpile code", | ||
@@ -28,3 +28,3 @@ "author": "Jorge Martins, Peter Muessig", | ||
}, | ||
"gitHead": "cfa56d1239b575f404cdfd8b2bc2a85b02b197e9" | ||
"gitHead": "90ea798a9777c57d30950ea1f0cf0b2eeb19af6b" | ||
} |
30336
215