Skip to content
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

bug: 0.16.23, Inconsistent loading using import #1448

Closed
unional opened this issue Jan 19, 2016 · 10 comments
Closed

bug: 0.16.23, Inconsistent loading using import #1448

unional opened this issue Jan 19, 2016 · 10 comments

Comments

@unional
Copy link
Contributor

unional commented Jan 19, 2016

I have a ts package importing domready:
import * as domready from 'domready';

It works fine inside the package. domready is resolved to the function.

When I reference this package, it fails. domready is resolved to { default: Function }.

Tried to trace the issue, seems like one is called by linkDynamicModule() L3109 (working)
and the other is called by link() L2838 (not working).

@frankwallis
Copy link

It looks correct to me that import * as domready from 'domready'; should return { default: Function }. You should use import domready from 'domready'; to get the default export. If TypeScript is not picking up on this then you may have an incorrect 'domready' declaration?

@unional
Copy link
Contributor Author

unional commented Jan 21, 2016

I originally was doing import domready from 'domready', but then in the latest build it failed as it returned undefined. In either case, the behavior should be consistent.

However, after I tried to roll back to 0.16.20 and test things out, it starts to work again.

Will continue to monitor a bit. Will close this issue now and reopen it if needed.

@unional unional closed this as completed Jan 21, 2016
@unional
Copy link
Contributor Author

unional commented Jan 21, 2016

Oh, by the way domready was not doing export default, so it may be __ESModule related?

@frankwallis
Copy link

Yes I do agree there is an issue, but it is hard to pinpoint. It seems that though files are not reliably being detected as commonjs/esm.

This issue is also similar: alexisvincent/systemjs-hot-reloader#42

@unional
Copy link
Contributor Author

unional commented Jan 26, 2016

I am able to reproduce the issue.

It turns out to be related to the module flag.
In config.js:

System.config({
  ...
  typescriptOptions: {
    module: "system"
    ...
  }
  ...
module import domready from `domready' import * as dr from 'domready'
system works fails
commonjs fails works
amd fails works
umd fails works

The error come back is (note I'm using karma-chrome, so the non-error message is not related to karma-phantomjs ariya/phantomjs#12381):

Chrome 47.0.2526 (Windows 8.1 0.0.0) WARN: '%cWarning: a promise was rejected with a non-error: [object String]
    at linkSetFailed (http://localhost:9877/base/jspm_packages/system.src.js?60ced582fc3750b88f77044342c775e5c3c18d0d:675:13)
    at http://localhost:9877/base/jspm_packages/system.src.js?60ced582fc3750b88f77044342c775e5c3c18d0d:571:9
    at doDynamicExecute (http://localhost:9877/base/jspm_packages/system.src.js?60ced582fc3750b88f77044342c775e5c3c18d0d:719:7)
    at link (http://localhost:9877/base/jspm_packages/system.src.js?60ced582fc3750b88f77044342c775e5c3c18d0d:912:20)
    at doLink (http://localhost:9877/base/jspm_packages/system.src.js?60ced582fc3750b88f77044342c775e5c3c18d0d:570:7)
    at updateLinkSetOnLoad (http://localhost:9877/base/jspm_packages/system.src.js?60ced582fc3750b88f77044342c775e5c3c18d0d:618:18)
    at http://localhost:9877/base/jspm_packages/system.src.js?60ced582fc3750b88f77044342c775e5c3c18d0d:430:11

From previous event:
    at createLinkSet (http://localhost:9877/base/jspm_packages/system.src.js?60ced582fc3750b88f77044342c775e5c3c18d0d:516:20)
    at http://localhost:9877/base/jspm_packages/system.src.js?60ced582fc3750b88f77044342c775e5c3c18d0d:483:21
From previous event:
    at loadModule (http://localhost:9877/base/jspm_packages/system.src.js?60ced582fc3750b88f77044342c775e5c3c18d0d:259:12)
    at http://localhost:9877/base/jspm_packages/system.src.js?60ced582fc3750b88f77044342c775e5c3c18d0d:800:11
From previous event:
    at SystemJSLoader.Loader.import (http://localhost:9877/base/jspm_packages/system.src.js?60ced582fc3750b88f77044342c775e5c3c18d0d:791:8)
    at SystemJSLoader.import (http://localhost:9877/base/jspm_packages/system.src.js?60ced582fc3750b88f77044342c775e5c3c18d0d:1661:25)
    at http://localhost:9877/base/node_modules/karma-jspm/src/adapter.js?5817daa21dc8df52825dd23824c41a6dfb0a7240:55:44
    at tryCatchReject (http://localhost:9877/base/jspm_packages/system-polyfills.src.js?be0f4ba76d282ea43052da5d0d22a53cea9237ca:1257:30)
    at runContinuation1 (http://localhost:9877/base/jspm_packages/system-polyfills.src.js?be0f4ba76d282ea43052da5d0d22a53cea9237ca:1216:4)
    at Fulfilled.when (http://localhost:9877/base/jspm_packages/system-polyfills.src.js?be0f4ba76d282ea43052da5d0d22a53cea9237ca:1004:4)
    at Pending.run (http://localhost:9877/base/jspm_packages/system-polyfills.src.js?be0f4ba76d282ea43052da5d0d22a53cea9237ca:895:13)
    at Scheduler._drain (http://localhost:9877/base/jspm_packages/system-polyfills.src.js?be0f4ba76d282ea43052da5d0d22a53cea9237ca:171:19)
    at Scheduler.drain (http://localhost:9877/base/jspm_packages/system-polyfills.src.js?be0f4ba76d282ea43052da5d0d22a53cea9237ca:136:9)
    at MutationObserver.run (http://localhost:9877/base/jspm_packages/system-polyfills.src.js?be0f4ba76d282ea43052da5d0d22a53cea9237ca:342:4)', 'color: darkorange'
Chrome 47.0.2526 (Windows 8.1 0.0.0) WARN: '%cWarning: a promise was rejected with a non-error: [object String]
    at createImportPromise (http://localhost:9877/base/jspm_packages/system.src.js?60ced582fc3750b88f77044342c775e5c3c18d0d:736:43)
    at http://localhost:9877/base/jspm_packages/system.src.js?60ced582fc3750b88f77044342c775e5c3c18d0d:799:47
From previous event:
    at SystemJSLoader.Loader.import (http://localhost:9877/base/jspm_packages/system.src.js?60ced582fc3750b88f77044342c775e5c3c18d0d:791:8)
    at SystemJSLoader.import (http://localhost:9877/base/jspm_packages/system.src.js?60ced582fc3750b88f77044342c775e5c3c18d0d:1661:25)
    at http://localhost:9877/base/node_modules/karma-jspm/src/adapter.js?5817daa21dc8df52825dd23824c41a6dfb0a7240:55:44
    at tryCatchReject (http://localhost:9877/base/jspm_packages/system-polyfills.src.js?be0f4ba76d282ea43052da5d0d22a53cea9237ca:1257:30)
    at runContinuation1 (http://localhost:9877/base/jspm_packages/system-polyfills.src.js?be0f4ba76d282ea43052da5d0d22a53cea9237ca:1216:4)
    at Fulfilled.when (http://localhost:9877/base/jspm_packages/system-polyfills.src.js?be0f4ba76d282ea43052da5d0d22a53cea9237ca:1004:4)
    at Pending.run (http://localhost:9877/base/jspm_packages/system-polyfills.src.js?be0f4ba76d282ea43052da5d0d22a53cea9237ca:895:13)
    at Scheduler._drain (http://localhost:9877/base/jspm_packages/system-polyfills.src.js?be0f4ba76d282ea43052da5d0d22a53cea9237ca:171:19)
    at Scheduler.drain (http://localhost:9877/base/jspm_packages/system-polyfills.src.js?be0f4ba76d282ea43052da5d0d22a53cea9237ca:136:9)
    at MutationObserver.run (http://localhost:9877/base/jspm_packages/system-polyfills.src.js?be0f4ba76d282ea43052da5d0d22a53cea9237ca:342:4)', 'color: darkorange'
Chrome 47.0.2526 (Windows 8.1 0.0.0) ERROR: 'Potentially unhandled rejection [2] Uncaught Uncaught Uncaught TypeError: expecting a function but got [object Undefined]
        Evaluating http://localhost:9877/base/src/bootstrapper.ts
        Evaluating http://localhost:9877/base/src/index.ts
        Evaluating http://localhost:9877/base/spec/bootstrapper.spec.ts
        Error loading http://localhost:9877/base/spec/bootstrapper.spec.ts (WARNING: non-Error used)'

@unional unional reopened this Jan 26, 2016
@frederikschubert
Copy link
Contributor

I see the same behavior with momentjs. The typings suggest to import it as a module with import * as moment from "moment"; but this only worked with commonjs module format.
After switching it to system format as the new version of plugin-typescript suggested I have to import moment as the default export import moment from "moment".

@frankwallis
Copy link

I think this may be related to this issue. There is a new compiler option in 1.8.0 --allowSyntheticDefaultImports which could solve it, see here.

@unional
Copy link
Contributor Author

unional commented Jan 26, 2016

Importing bluebird and domready, where bluebird is "format global" and domready is"format cjs"`.

Correction: I forgot that import B from 'bluebird' and import * as B from 'bluebird' suppose to do two different things.

import * as B from 'bluebird' returns the module export while import B from 'bluebird' returns the default export.

In simplest way, import B from 'bluebird' returns a Promise that you can call with new B(...), while import * as B from 'bluebird' returns a module object and you need new B.default() to access the promise function.

In this sense, commonjs/amd/umd + import * as dr form 'domready' should not returns the domready() function, which it currently does.

Updating the table:

module import dr from `domready' import * as dr from 'domready' import B from 'bluebird' import * as B from 'bluebird'
system dr() { default() } B() { default() }
commonjs/amd/umd undefined dr() undefined B()
tsd N Y N Y

To technically module: "system" is doing the right thing while others are not.
I understand the other issue var m = require('some-es6-module').default, but it seems like the transition to import is still not settled?

@unional
Copy link
Contributor Author

unional commented Jan 26, 2016

Adding references
microsoft/TypeScript#2839
microsoft/TypeScript#4668

@unional
Copy link
Contributor Author

unional commented Jan 28, 2016

Close as it is a duplicate of microsoft/TypeScript#5285

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants