Skip to content

Postprocessor doesn't convert CJS to goog module properly in ES5 mode #162

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

Closed
vikerman opened this issue Jun 14, 2016 · 2 comments
Closed
Assignees

Comments

@vikerman
Copy link
Contributor

vikerman commented Jun 14, 2016

This is a particular case where in code there is an export * before import from the same module

export * from './random';
import {getRandom} from './random';

When TS outputs ES5+CJS mode it looks like

"use strict";
function __export(m) {
    for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
__export(require('./random'));
var random_2 = require('./random');

When the tsickle Postprocessor runs on this, it outputs

goog.module('codelab.main');var module = module || {id: 'codelab/main'};
function __export(m) {
    for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
__export(goog.require('google3.javascript.typescript.examples.codelab.random'));
var random_2 = *;  <------------- Oops!!

The problem is here https://github.com/angular/tsickle/blob/master/src/tsickle.ts#L1179

It outputs working code(by generating two separate goog.require for the export and the import) if the above line is changed to

if (this.moduleVariables.hasOwnProperty(modName) && this.moduleVariables[modNam] !== '*') {
evmar added a commit that referenced this issue Jun 14, 2016
Summary:
This change just moves the ES5 postprocesing logic into
its own file.  This will make subsequent modifications
simpler.

Preliminary refactoring for #162.

Reviewers: vikerman, alexeagle

Reviewed By: alexeagle

Subscribers: alexeagle, typescript-eng

Differential Revision: https://reviews.angular.io/D151
@evmar
Copy link
Contributor

evmar commented Jun 14, 2016

It sounds like, due to #118 , we cannot just emit

var random_2 = goog.require('google3.javascript.typescript.examples.codelab.random');

because that would cause a duplicate import. Still looking...

@evmar
Copy link
Contributor

evmar commented Jun 14, 2016

Fixed in 31f9b42

@evmar evmar closed this as completed Jun 14, 2016
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

2 participants