Merge branch 'resolve_with_package' of https://github.com/ethereum/remix-project into resolve_with_package

pull/5370/head
filip mertens 2 years ago
commit f1929daa05
  1. 15
      libs/remix-core-plugin/src/lib/compiler-content-imports.ts
  2. 23
      libs/remix-url-resolver/src/resolve.ts

@ -16,7 +16,6 @@ export type ResolvedImport = {
} }
export class CompilerImports extends Plugin { export class CompilerImports extends Plugin {
previouslyHandled: Record<string, ResolvedImport>
urlResolver: any urlResolver: any
constructor () { constructor () {
super(profile) super(profile)
@ -45,7 +44,7 @@ export class CompilerImports extends Plugin {
return {} return {}
} }
}) })
this.previouslyHandled = {} // cache import so we don't make the request at each compilation. this.on('filePanel', 'setWorkspace', () => this.urlResolver.clearCache())
} }
async setToken () { async setToken () {
@ -95,22 +94,12 @@ export class CompilerImports extends Plugin {
if (!cb) cb = () => {} if (!cb) cb = () => {}
const self = this const self = this
if (force) delete this.previouslyHandled[url]
const imported = this.previouslyHandled[url]
if (imported) {
return cb(null, imported.content, imported.cleanUrl, imported.type, url)
}
let resolved let resolved
try { try {
await this.setToken() await this.setToken()
resolved = await this.urlResolver.resolve(url) resolved = await this.urlResolver.resolve(url, [], force)
const { content, cleanUrl, type } = resolved const { content, cleanUrl, type } = resolved
self.previouslyHandled[url] = {
content,
cleanUrl,
type
}
cb(null, content, cleanUrl, type, url) cb(null, content, cleanUrl, type, url)
} catch (e) { } catch (e) {
return cb(new Error('not found ' + url)) return cb(new Error('not found ' + url))

@ -42,6 +42,10 @@ export class RemixURLResolver {
this.protocol = protocol this.protocol = protocol
} }
clearCache () {
this.previouslyHandled = {}
}
/** /**
* Handle an import statement based on github * Handle an import statement based on github
* @param root The root of the github import statement * @param root The root of the github import statement
@ -137,8 +141,17 @@ export class RemixURLResolver {
if (this.getDependencies) { if (this.getDependencies) {
try { try {
const { deps, yarnLock, packageLock } = await this.getDependencies() const { deps, yarnLock, packageLock } = await this.getDependencies()
for (const pkg of Object.keys(deps)) { let matchLength = 0
if (url.startsWith(pkg)) { let pkg
Object.keys(deps).map((dep) => {
const reg = new RegExp(dep, 'g')
const match = url.match(reg)
if (match && match.length > 0 && matchLength < match[0].length) {
matchLength = match[0].length
pkg = dep
}
})
if (pkg) {
let version let version
if (yarnLock) { if (yarnLock) {
// yarn.lock // yarn.lock
@ -157,8 +170,6 @@ export class RemixURLResolver {
version = deps[pkg] version = deps[pkg]
} }
if (version) url = url.replace(pkg, `${pkg}@${version}`) if (version) url = url.replace(pkg, `${pkg}@${version}`)
break
}
} }
} catch (e) { } catch (e) {
console.log(e) console.log(e)
@ -207,9 +218,9 @@ export class RemixURLResolver {
] ]
} }
public async resolve (filePath: string, customHandlers?: Handler[]): Promise<Imported> { public async resolve (filePath: string, customHandlers?: Handler[], force?: boolean): Promise<Imported> {
let imported: Imported = this.previouslyHandled[filePath] let imported: Imported = this.previouslyHandled[filePath]
if (imported) { if (!force && imported) {
return imported return imported
} }
const builtinHandlers: Handler[] = this.getHandlers() const builtinHandlers: Handler[] = this.getHandlers()

Loading…
Cancel
Save