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

ui-router-react fails with Rollup #33

Closed
mfreeman-xtivia opened this issue Mar 18, 2017 · 7 comments
Closed

ui-router-react fails with Rollup #33

mfreeman-xtivia opened this issue Mar 18, 2017 · 7 comments

Comments

@mfreeman-xtivia
Copy link

When attempting to bundle a React app that uses ui-router-react I get a zillion complaints from Rollup along the lines of

Error: Cannot call a namespace ('classNames')

Based on similar errors I see documented for Rollup...

When importing a module's namespace using * as foo you get a namespace object which isn't callable. You should import default export instead. 

import express from 'express';

const app = express();
@elboman
Copy link
Member

elboman commented Mar 20, 2017

Hi,
I haven't tried Rollup yet, but it looks like it doesn't like the import * as something from 'something' syntax.

Unfortunately the only way to fix it is to change the way we import classnames in the library, unless you are using typescript in you application. In that case you simply need to add synthetic default imports in your tsconfig.json:

"allowSyntheticDefaultImports": true,

Sorry for the inconvenience, I'll change the import for next version and do some tests with a Rollup setup ;)

@christopherthielen
Copy link
Member

Interesting. We're using Rollup to bundle angular-ui-router which should be nearly the same, right?

@mfreeman-xtivia
Copy link
Author

Don't know as I am not using that one at the moment---I did have success using Rollup to bundle ui-router for angular 2, both for JIT and AOT mode. But no joy for the React version and Rollup.

@christopherthielen
Copy link
Member

christopherthielen commented Mar 21, 2017

Tried bundling ui-router-react, got the error:

(79): lib-esm/reactViews.js🚨   Cannot call a namespace ('classNames')
lib-esm/components/UISref.js (49:23)
47:             onClick: this.handleClick,
48:             href: this.context['router'].stateService.href(to, params, options),
49:             className: classNames(this.props.className, childrenProps.className)
                           ^
50:         });
51:         return cloneElement(this.props.children, props);

rollup.config.js:

import nodeResolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import uglify from 'rollup-plugin-uglify';
import progress from 'rollup-plugin-progress';
import sourcemaps from 'rollup-plugin-sourcemaps';
import visualizer from 'rollup-plugin-visualizer';

var pkg = require('./package.json');
var banner =
`/**
 * ${pkg.description}
 * @version v${pkg.version}
 * @link ${pkg.homepage}
 * @license MIT License, http://www.opensource.org/licenses/MIT
 */`;

var uglifyOpts = { output: {} };
// retain multiline comment with @license
uglifyOpts.output.comments = (node, comment) =>
comment.type === 'comment2' && /@license/i.test(comment.value);

var plugins = [
  nodeResolve({jsnext: true}),
  commonjs(),
  progress(),
  sourcemaps(),
  uglify(uglifyOpts),
  visualizer({ sourcemap: true }),
];

const CONFIG = {
  sourceMap: true,
  format: 'umd',
  exports: 'named',
  plugins: plugins,
  banner: banner,
  moduleName: 'ui-router-react',
  entry: 'lib-esm/index.js',
  dest: '_bundles/ui-router-react.min.js',
  external: ['react', 'react-dom'],
};

export default CONFIG;

@christopherthielen
Copy link
Member

@elboman See #34

@christopherthielen
Copy link
Member

christopherthielen commented Mar 23, 2017

Here's an issue reported for "classnames" + typescript + synthetic default + webpack

microsoft/TypeScript#7518 (comment)

TypeScript has no runtime support for synthetic defaults, it simply allows for the fact they may be present at runtime. It is the module loader (SystemJS in this case) that creates the synthetic default.

It appears that this might be a webpack thing?

@elboman
Copy link
Member

elboman commented Mar 24, 2017

So, I'm trying to find out the problem.

It looks like TS wants the import * as X from Y syntax to handle modules which don't have a default import. I never used this syntax before using typescript (unless I wanted to include all exports in a single object) because babel changes the way default export are transpiled (microsoft/TypeScript#11057).

On the other hand, rollup doesn't like that syntax (as pointed out here rollup/rollup#670) hence the problem.

A solution would probably be to add babel and let it transpile the export in the modules, but I don't like it as we don't need babel.

I guess the "magic" solution works for now, and we'll change it accordingly once we'll find a better solution.

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