Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

moment conflict #68

Closed
tamascsaba opened this issue Oct 12, 2016 · 9 comments
Closed

moment conflict #68

tamascsaba opened this issue Oct 12, 2016 · 9 comments

Comments

@tamascsaba
Copy link

tamascsaba commented Oct 12, 2016

When I use moment with rollup-plugin-typescript, throw an error:

import * as moment from "moment";
moment();
bundle dev started ...
Error: Cannot call a namespace ('moment')

I finded a workaround, but is is not compatible with my editor, underline with red line
https://forum.ionicframework.com/t/error-from-rollup-when-importing-moment-js-with-rc0/64902

import  moment from "moment";

moment

Module moment has no default export.

@ghetolay
Copy link

My current workaround is using a custom replace plugin like so :

plugins: [
  {
    name: 'replace moment imports',
    transform: code =>
      ({
        code: code.replace(/import\s*\*\s*as\s*moment/g, 'import moment'),
        map: { mappings: '' }
      })
  },
  typescript()
]

@aluanhaddad
Copy link

@tamascsaba That is because the namespace import form, import * as namespace does not import the call signatures of the target.
If the following works at runtime, but gives you language service errors, such as in an IDE,

import  moment from "moment";

Then add "allowSyntheticDefaultImports": true to your tsconfig.json "compilerOptions"

@tamascsaba
Copy link
Author

tamascsaba commented Nov 11, 2016

thx @aluanhaddad && @ghetolay :)

@MarkPerryBV
Copy link

@ghetolay 's rollup plugin worked for me to resolve the issues I was getting.

@Mariusz-v7
Copy link

Mariusz-v7 commented Oct 1, 2017

@aluanhaddad

import moment from "moment";

solves the issue but it breaks SystemJS which results in the following message:

moment_1.default is not a function

Do you know how to configure SystemJS to work with this kind of import?

@aluanhaddad
Copy link

aluanhaddad commented Oct 2, 2017

Yes but which reversion of SystemJS are you running? it should be working by default no pun intended. Mapping module.exports onto exports.default is the standard SystemJS behavior aligning with the ES Module spec and the Node.js plans for adopting a subset of it. I'm curious what your workflow is using SystemJS with rollup-plugin-typescript.

SystemJS.config({
  packages: {
    "npm:moment": {
      format: "cjs"
    }
   }
  } 
});

@aluanhaddad
Copy link

aluanhaddad commented Jan 29, 2018

For those interested, TypeScript now provides the default synthesis, similar to Babel, not just the interop awareness that is enabled by --allowSyntheticDefaultImports, but the generation of interop code for legacy module formats.

It is behind a new flag --esModuleInterop and is available in typescript@next.

@Yogu
Copy link

Yogu commented Sep 11, 2018

That is because the namespace import form, import * as namespace does not import the call signatures of the target.

Why is this workaround working, then? Seems like a bug in rollup. TypeScript allows to merge function declarations with namespaces, and webpack/ts-node don't have a problem with this. I don't like enabling --allowSyntheticDefaultImports just to be able to use these modules with rollup.

@zanona
Copy link

zanona commented Nov 7, 2018

It feels like having to change the codebase which would then break validation in order to accommodate post-processing is a bit of a let-down. Perhaps this can be reconsidered and @ghetolay's workaround could perhaps be added as part of this plugin? -If that's possible at all
As of now, I believe that's the only solution that looks clean enough and doesn't interfere with the development process to something other than what you would expect when compiling typescript.

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

No branches or pull requests

7 participants