ui5-tooling-transpile
Advanced tools
Comparing version 0.3.7 to 0.4.0
@@ -6,2 +6,8 @@ # Change Log | ||
# [0.4.0](https://github.com/ui5-community/ui5-ecosystem-showcase/compare/ui5-tooling-transpile@0.3.7...ui5-tooling-transpile@0.4.0) (2023-03-14) | ||
### Features | ||
- **ui5-tooling-transpile:** generate d.ts and filter ts files ([#693](https://github.com/ui5-community/ui5-ecosystem-showcase/issues/693)) ([56fc521](https://github.com/ui5-community/ui5-ecosystem-showcase/commit/56fc521f3078f0fc87e1c7f950a58701f241f7a7)) | ||
## [0.3.7](https://github.com/ui5-community/ui5-ecosystem-showcase/compare/ui5-tooling-transpile@0.3.6...ui5-tooling-transpile@0.3.7) (2023-02-21) | ||
@@ -8,0 +14,0 @@ |
@@ -47,6 +47,6 @@ const babel = require("@babel/core"); | ||
const pathWithFilePattern = pathname.replace(".js", filePatternConfig); | ||
config.verbose && log.info(`Lookup resource ${pathWithFilePattern}`); | ||
config.debug && log.verbose(`Lookup resource ${pathWithFilePattern}`); | ||
const matchedResources = await reader.byGlob(pathWithFilePattern); | ||
config.verbose && log.info(` --> Found ${matchedResources?.length || 0} match(es)!`); | ||
config.debug && log.verbose(` --> Found ${matchedResources?.length || 0} match(es)!`); | ||
@@ -53,0 +53,0 @@ const resource = matchedResources?.[0]; |
158
lib/task.js
@@ -5,2 +5,3 @@ const log = require("@ui5/logger").getLogger("builder:customtask:ui5-tooling-transpile"); | ||
const path = require("path"); | ||
const fs = require("fs"); | ||
const babel = require("@babel/core"); | ||
@@ -22,6 +23,6 @@ | ||
*/ | ||
module.exports = async function ({ workspace, dependencies, /* taskUtil,*/ options }) { | ||
module.exports = async function ({ workspace /*, dependencies*/, taskUtil, options }) { | ||
const config = options?.configuration || {}; | ||
config.includes = config.includes || config.includePatterns; | ||
config.excludes = config.excludes || config.excludePatterns; | ||
config.includes = config.includes || config.includePatterns || []; | ||
config.excludes = config.excludes || config.excludePatterns || []; | ||
@@ -37,8 +38,5 @@ const babelConfig = await createBabelConfig({ configuration: config, isMiddleware: false }); | ||
const allResources = await workspace.byGlob(`/**/*${filePatternConfig}`); | ||
if (config.transpileDependencies) { | ||
// TODO: does transpileDependencies make sense for JavaScript files? | ||
const depsResources = await dependencies.byGlob(`/**/*${filePatternConfig}`); | ||
allResources.push(...depsResources); | ||
} | ||
// transpile the TypeScript resources and collect the code | ||
const sourcesMap = {}; | ||
await Promise.all( | ||
@@ -50,35 +48,48 @@ allResources.map(async (resource) => { | ||
if ( | ||
!(config.excludes || []).some((pattern) => resourcePath.includes(pattern)) || | ||
(config.includes || []).some((pattern) => resourcePath.includes(pattern)) | ||
!config.excludes.some((pattern) => resourcePath.includes(pattern)) || | ||
config.includes.some((pattern) => resourcePath.includes(pattern)) | ||
) { | ||
const filePath = resourcePath.replace(new RegExp("\\.[^.]+$"), ".js"); | ||
// read file into string | ||
// read source file | ||
const source = await resource.getString(); | ||
// transpile the source | ||
config.debug && log.info(`Transpiling resource ${resource.getPath()}`); | ||
const result = await babel.transformAsync( | ||
source, | ||
Object.assign({}, babelConfig, { | ||
filename: resource.getPath() // necessary for source map <-> source assoc | ||
}) | ||
); | ||
// store the ts source code in the sources map | ||
if (config.transpileTypeScript) { | ||
sourcesMap[resourcePath] = source; | ||
} | ||
resource.setString(normalizeLineFeeds(result.code)); | ||
resource.setPath(filePath); | ||
workspace.write(resource); | ||
// we ignore d.ts files for transpiling | ||
if (!resourcePath.endsWith(".d.ts")) { | ||
// mark source for omit from build result | ||
taskUtil.setTag(resource, taskUtil.STANDARD_TAGS.OmitFromBuildResult, true); | ||
// create SourceMap resource if available | ||
if (result.map) { | ||
//add the transpiled file name | ||
result.map.file = path.basename(filePath); | ||
config.debug && log.info(` + sourcemap ${filePath}.map`); | ||
// transpile the source | ||
config.debug && log.info(`Transpiling resource ${resourcePath}`); | ||
const result = await babel.transformAsync( | ||
source, | ||
Object.assign({}, babelConfig, { | ||
filename: resourcePath // necessary for source map <-> source assoc | ||
}) | ||
); | ||
const resourceMap = resourceFactory.createResource({ | ||
path: `${filePath}.map`, | ||
string: JSON.stringify(result.map) | ||
// create the ts file in the workspace | ||
const transpiledResource = resourceFactory.createResource({ | ||
path: filePath, | ||
string: normalizeLineFeeds(result.code) | ||
}); | ||
workspace.write(transpiledResource); | ||
workspace.write(resourceMap); | ||
// create sourcemap resource if available | ||
if (result.map) { | ||
result.map.file = path.basename(filePath); | ||
config.debug && log.info(` + sourcemap ${filePath}.map`); | ||
const resourceMap = resourceFactory.createResource({ | ||
path: `${filePath}.map`, | ||
string: JSON.stringify(result.map) | ||
}); | ||
workspace.write(resourceMap); | ||
} | ||
} | ||
@@ -88,2 +99,87 @@ } | ||
); | ||
// generate the dts files for the ts files | ||
if (config.transpileTypeScript) { | ||
// determine if the project is a library and enable the DTS generation by default | ||
// TODO: UI5 Tooling 3.0 allows to access the project with the TaskUtil | ||
// https://sap.github.io/ui5-tooling/v3/api/@ui5_project_build_helpers_TaskUtil.html#~ProjectInterface | ||
// from here we could derive the project type instead of guessing via file existence | ||
const libraryResources = await workspace.byGlob(`/resources/${options.projectNamespace}/*library*`); | ||
if (libraryResources.length > 0 && config.generateDts === undefined) { | ||
config.debug && log.info(`Enabling d.ts generation by default for library projects!`); | ||
config.generateDts = true; | ||
} | ||
// generate the dts files for the ts files | ||
if (config.generateDts) { | ||
try { | ||
// dynamically require typescript | ||
const ts = require("typescript"); | ||
// options to generate d.ts files only | ||
const options = { | ||
allowJs: true, | ||
declaration: true, | ||
emitDeclarationOnly: true | ||
//traceResolution: true, | ||
}; | ||
// Create a Program with an in-memory emit | ||
const host = ts.createCompilerHost(options); | ||
(host.getCurrentDirectory = () => ""), | ||
(host.fileExists = (file) => !!sourcesMap[file] || fs.existsSync(file)); | ||
host.readFile = (file) => sourcesMap[file] || fs.readFileSync(file, "utf-8"); | ||
host.writeFile = function (fileName, content) { | ||
config.debug && log.info(`Generating d.ts for resource ${fileName}`); | ||
const dtsFile = resourceFactory.createResource({ | ||
path: `${fileName}`, | ||
string: content | ||
}); | ||
workspace.write(dtsFile); | ||
}; | ||
host.resolveModuleNames = function (moduleNames, containingFile) { | ||
const resolvedModules = []; | ||
for (const moduleName of moduleNames) { | ||
// try to use standard resolution | ||
const result = ts.resolveModuleName(moduleName, containingFile, options, { | ||
fileExists: host.fileExists, | ||
readFile: host.readFile | ||
}); | ||
if (result.resolvedModule) { | ||
// module resolved, store this info | ||
resolvedModules.push(result.resolvedModule); | ||
} else { | ||
// for all other modules, mark them as external library imports | ||
// => most probably these are the UI5 modules with the UI5 namespace | ||
const resolvedFileName = `/resources/${moduleName}.ts`; | ||
resolvedModules.push({ | ||
resolvedFileName, | ||
extension: ".ts", | ||
isExternalLibraryImport: !sourcesMap[resolvedFileName] | ||
}); | ||
} | ||
} | ||
return resolvedModules; | ||
}; | ||
// prepare and emit the d.ts files | ||
const program = ts.createProgram(Object.keys(sourcesMap), options, host); | ||
//const program = ts.createProgram(Object.keys(sourcesMap).filter((s) => !s.endsWith("d.ts")), options, host); | ||
const result = program.emit(); | ||
// error diagnostics | ||
if (result.emitSkipped) { | ||
log.error( | ||
`The following errors occured during d.ts generation: \n${ts.formatDiagnostics( | ||
result.diagnostics, | ||
host | ||
)}` | ||
); | ||
} | ||
} catch (e) { | ||
// typescript dependency should be available, otherwise we can't generate the dts files | ||
log.warn(`Generating d.ts failed! Reason: ${e}`); | ||
} | ||
} | ||
} | ||
}; |
@@ -119,3 +119,2 @@ const log = require("@ui5/logger").getLogger("builder:customtask:ui5-tooling-transpile"); | ||
babelConfig.presets.push("transform-ui5", "@babel/preset-typescript"); | ||
babelConfig.ignore = ["**/*.d.ts"]; | ||
} else { | ||
@@ -122,0 +121,0 @@ // the default babel configuration |
{ | ||
"name": "ui5-tooling-transpile", | ||
"version": "0.3.7", | ||
"version": "0.4.0", | ||
"description": "UI5 tooling extensions to transpile code", | ||
@@ -27,3 +27,3 @@ "author": "Jorge Martins, Peter Muessig", | ||
}, | ||
"gitHead": "0ac3dffdb9c673d1f7c962e94304e83f6f547c87" | ||
"gitHead": "40ea591ddd709a39be8b7942458faa558f126fa8" | ||
} |
@@ -43,2 +43,5 @@ # UI5 Tooling Extensions for JS/TS transpiling | ||
- generateDts: `true|false` | ||
if option is enabled, the tooling extension will generate the d.ts files (for projects of type `library` this option is considered as `true` (enabled) by default and for other projects such as `application` this option is considered as `false` (disabled) by default) | ||
## Usage | ||
@@ -45,0 +48,0 @@ |
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
40493
364
125
3