-
-
Notifications
You must be signed in to change notification settings - Fork 533
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): support ESM Forge module loading (#3582)
* refactor(core): try...catch dynamicImport This ensures calling dynamicImport will rather result in rejected promise than in unhandled exception. * feat(core): support ESM module packages Replace some require() calls with dynamicImport() to support loading makers/publishers/plugins etc. that are ESM makers * fix(core): limit path resolving in helper Avoid resolving path to file url in dynamicImport to support importing modules by identifiers. * fix(core): re-order dynamicImportMaybe methods Try require first, then import, to avoid .default property for CJS. * fix(core) fix tests in core * refactor(core): merge import error messages Merge error messages for imports to easier debug potential failures. * refactor(core): improve tests Rename require-search test to import-search and improve testing the promise rejection. Co-authored-by: Erick Zhao <erick@hotmail.ca> * generic refactors * fix(core): Move plugins init to promise. This should fix regression caused by bd6ea70. --------- Co-authored-by: Erick Zhao <erick@hotmail.ca> Co-authored-by: Samuel Attard <marshallofsound@electronjs.org> Co-authored-by: Erik Moura <erikian@erikian.dev> Co-authored-by: George Xu <33054982+georgexu99@users.noreply.github.com>
- Loading branch information
1 parent
f4c3834
commit cc0d7d6
Showing
12 changed files
with
101 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export declare function dynamicImport(path: string): Promise<any>; | ||
/** Like {@link dynamicImport()}, except it tries out {@link require()} first. */ | ||
export declare function dynamicImportMaybe(path: string): Promise<any>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,23 @@ | ||
const url = require('url'); | ||
const fs = require('fs'); | ||
|
||
exports.dynamicImport = function dynamicImport(path) { | ||
return import(url.pathToFileURL(path)); | ||
exports.dynamicImport = async function dynamicImport(path) { | ||
try { | ||
return await import(fs.existsSync(path) ? url.pathToFileURL(path) : path); | ||
} catch (error) { | ||
return Promise.reject(error); | ||
} | ||
}; | ||
|
||
exports.dynamicImportMaybe = async function dynamicImportMaybe(path) { | ||
try { | ||
return require(path); | ||
} catch (e1) { | ||
try { | ||
return await exports.dynamicImport(path); | ||
} catch (e2) { | ||
e1.message = '\n1. ' + e1.message + '\n2. ' + e2.message; | ||
throw e1; | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { expect } from 'chai'; | ||
|
||
import findConfig from '../../src/util/forge-config'; | ||
import importSearch from '../../src/util/import-search'; | ||
|
||
describe('import-search', () => { | ||
it('should resolve null if no file exists', async () => { | ||
const resolved = await importSearch(__dirname, ['../../src/util/wizard-secrets']); | ||
expect(resolved).to.equal(null); | ||
}); | ||
|
||
it('should resolve a file if it exists', async () => { | ||
const resolved = await importSearch(__dirname, ['../../src/util/forge-config']); | ||
expect(resolved).to.equal(findConfig); | ||
}); | ||
|
||
it('should throw if file exists but fails to load', async () => { | ||
const promise = importSearch(__dirname, ['../fixture/require-search/throw-error']); | ||
await expect(promise).to.be.rejectedWith('test'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.