-
-
Notifications
You must be signed in to change notification settings - Fork 160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Dynamic Import when avalible. #121
Comments
So this will require a refactor of all existing exercises? and probably guidance for future exercises on using this? |
Yeah, this would be a fund little project. It would mean that people can develop contributions and still be able to run using the normal yarn/npm process. |
Cool, I'm going on vacation on the 20th and will have some free time to actually do fun little projects, I can tackle this over the break. |
Nice. Go for it! |
Starting on it now. |
@masters3d digging into this and I'm having some issues implementing it. Started at the top level import, changing: import Acronym from './acronym' to const Acronym = import('./acronym.example') || import('./acronym') but now in the test code TS shows the error It's a Promise that has yet to resolve. That's why in the example you linked to above in the 2.4 release notes they show it in an So if we move away from the top level and put it inside the test, we run into an issue because We end up with this: describe('Acronym are produced from', () => {
it('title cased phrases', () => {
return async function getModule() {
const Acronym = await import('./acronym.example') || await import('./acronym')
expect(Acronym.parse('Portable Network Graphics')).toEqual('PNG')
}
})
} but now TS shows this error: It's now a resolved promise but now I'm getting the TS error Wat? If I comment out the Putting this here to see if there's something obvious I'm missing... I'll push up a branch so you can see the code as well. |
TypeScript will not compile if one of the imports isn't available/doesn't exist. This feature (in TS at least) seems to be more for memory/bundle optimization than for contingent importing. Closing this issue. |
thanks for trying @thedevelopnik ! lets leave it open. i’d like to give it a try. |
the next thing to try would be the .then .catch syntax |
Cant have top level await. May need to drop down to node for this feature |
This works but it is not ideal: const fileExists = require('file-exists');
describe('Hello World', async () => {
async function loadModule(){
let example = ''
if (fileExists.sync('hello-world.example.ts')) {
example = '.example'
}
return await import('./hello-world' + example)
.then(module => module.default)
}
it('says hello world with no name', async () => {
expect(HelloWorld.hello()).toEqual('Hello, World!')
})
it('says hello to bob', async () => {
expect(HelloWorld.hello('Bob')).toEqual('Hello, Bob!')
})
it('says hello to sally', async () => {
expect(HelloWorld.hello('Sally')).toEqual('Hello, Sally!')
})
const HelloWorld = await loadModule()
}) |
The next step would be to warp this into a library or function so that we could say. const HelloWorld = importElse('./hello-world.example', './hello-world') |
On the flip-side, We could export conditionally on the user facing stub file (not example). https://stackoverflow.com/a/43591970/3705470 const exportedAPI = shouldUseMock ? mockAPI : realAPI
export default exportedAPI |
2.9 seems to be the version that we want fo this: |
We have top-level track package.json tooling now that works without dynamic import. |
Dynamic import would allow us to be able to import the
exercise.example
but it that doesn't exist then we could just importexercise
. This would help in development.https://github.com/tc39/proposal-dynamic-import
This would be similar to what we already do in the Objective-C track
The text was updated successfully, but these errors were encountered: