diff --git a/index.js b/index.js index a1bded5..dd37845 100644 --- a/index.js +++ b/index.js @@ -186,6 +186,48 @@ module.exports = class DependencyStream extends Readable { const basedir = key.slice(0, key.lastIndexOf('/') + 1) const all = [] + if (deps.importsAttributes) { + for (const attrInput of deps.importsAttributes) { + const attrOutput = await this._resolveModule(attrInput, basedir) + const data = await this.drive.get(attrOutput) + if (data === null) throw new Error('Key not found: ' + key) + + const source = b4a.toString(data) + let imports = {} + try { + const obj = JSON.parse(source) + if (obj === null || typeof obj !== 'object' || Array.isArray(obj)) { + throw new Error(`Invalid import attribute json file: ${key}`) + } + if ('imports' in obj) imports = obj.imports + else imports = obj + } catch (err) { + const jsonErr = new Error(`Invalid import attribute json file: ${key}`) + jsonErr.code = 'INVALID_JSON' + throw jsonErr + } + + const modules = Object.values(imports) + const resolvedModules = [] + for (const item of modules) { + try { + const res = await this._resolveModule(item, basedir) + resolvedModules.push(res) + } catch (err) { + const resolveErr = new Error(`Failed to resolve module ${item}`) + resolveErr.code = 'MODULE_NOT_FOUND' + throw resolveErr + } + } + + for (const resolvedModule of resolvedModules) { + if (!result.resolutions.some(item => item.input === resolvedModule)) { + result.resolutions.push({ isImport: false, position: null, input: resolvedModule, output: null }) + } + } + } + } + for (const res of result.resolutions) { if (isAddonPolyfill(res.input)) { result.addons.push({