@module-federation/typescript
Advanced tools
Comparing version 0.2.1 to 0.2.2
{ | ||
"name": "@module-federation/typescript", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"description": "Webpack plugin to stream typescript for module federation apps/components", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -22,4 +22,4 @@ const ts = require("typescript"); | ||
const distPath = | ||
get(this.options, "devServer.static.directory") || | ||
get(this.options, "output.path") || | ||
get(this.compilerOptions, "devServer.static.directory") || | ||
get(this.compilerOptions, "output.path") || | ||
"dist"; | ||
@@ -99,22 +99,44 @@ | ||
extractTypes() { | ||
const normalizedFileNames = []; | ||
getExtension(rootDir, entry) { | ||
// Check path exists and it's a directory | ||
if (!fs.existsSync(rootDir) || !fs.lstatSync(rootDir).isDirectory()) { | ||
throw new Error('rootDir must be a directory'); | ||
} | ||
const fileNames = Object.values(this.exposedComponents); | ||
let filename; | ||
fileNames.forEach((componentFilePath) => { | ||
const ext = path.extname(componentFilePath); | ||
try { | ||
// Try to resolve exposed component using index | ||
const files = fs.readdirSync(path.join(rootDir, entry)); | ||
// TODO: Resolve the file ext automatically if not provided in the ModuleFederation Config | ||
if ([".ts", ".tsx"].includes(ext)) { | ||
const normalizedPath = path.resolve(process.cwd(), componentFilePath); | ||
filename = files.find(file => file.split('.')[0] === 'index'); | ||
normalizedFileNames.push(normalizedPath); | ||
} else { | ||
throw new Error( | ||
`Can not determine file extension, please include file extension for the file ${componentFilePath}` | ||
); | ||
} | ||
}); | ||
return `${entry}/${filename}`; | ||
} | ||
catch (err) { | ||
const files = fs.readdirSync(rootDir); | ||
// Handle case where directory contains similar filenames | ||
// or where a filename like `Component.base.tsx` is used | ||
filename = files.find(file => { | ||
const baseFile = path.basename(file, path.extname(file)); | ||
const baseEntry = path.basename(entry, path.extname(entry)); | ||
return baseFile === baseEntry; | ||
}); | ||
return filename; | ||
} | ||
} | ||
extractTypes() { | ||
const normalizedFileNames = Object.values(this.exposedComponents) | ||
.map(exposed => { | ||
const [ rootDir, entry ] = exposed.split(/\/(?=[^/]+$)/); | ||
const ext = this.getExtension(rootDir, entry); | ||
return path.resolve(process.cwd(), rootDir, ext); | ||
}) | ||
.filter(entry => /\.tsx?$/.test(entry)); | ||
const host = ts.createCompilerHost(this.tsCompilerOptions); | ||
@@ -121,0 +143,0 @@ const originalWriteFileFn = host.writeFile; |
7680
144