thgreasi / babel-plugin-system-import-transformer

import() & System.import() to UMD pattern transformer plugin for Babel
MIT License
72 stars 6 forks source link

ReferenceError: self is not defined #9

Closed ghost closed 8 years ago

ghost commented 8 years ago

Hi @thgreasi I use this plugin for my react universal app with webpack2, but when I am running dev building I encounter an error it says :

var _systemImportTransformerGlobalIdentifier = typeof window !== 'undefined' ? window : self; ^ ReferenceError: self is not defined

I have no idea what's wrong with it, do you know how to fix it?

Here's my routes with System.import :

import React from 'react';
import { Route, IndexRoute } from 'react-router';
import App from './containers/App';

if (module.hot) {
  // HMR falls over when a Route uses a dynamic component resolution
  // property (i.e. getComponent or getComponents).  As a workaround for any
  // of your components that are resolved dynamically please require them below.
  require('./containers/Home');
  require('./containers/UserInfo');
  require('./containers/NotFound');
}

const errorLoading = err => {
  console.error(`==> 😭  Dynamic page loading failed: ${err}`);
};

const resolveIndexComponent = (nextState, cb) => {
  System.import('./containers/Home')
    .then(module => cb(null, module.default))
    .catch(errorLoading);
};

const resolveUserInfoComponent = (nextState, cb) => {
  System.import('./containers/UserInfo')
    .then(module => cb(null, module.default))
    .catch(errorLoading);
};

const resolveNotFoundComponent = (nextState, cb) => {
  System.import('./containers/NotFound')
    .then(module => cb(null, module.default))
    .catch(errorLoading);
};

export default (
  <Route path="/" component={App}>
    <IndexRoute getComponent={resolveIndexComponent} />
    <Route path="UserInfo/:id" getComponent={resolveUserInfoComponent} />
    <Route path="*" getComponent={resolveNotFoundComponent} />
  </Route>
);
thgreasi commented 8 years ago

Working on it.

thgreasi commented 8 years ago

What build tool are you using and on what context is it falling? (eg: inside a serviceWorker ?)

thgreasi commented 8 years ago

This should be fixed with 85127b8d902eecfbfabd37ddcb6b57e7c97647b0 (currently in master) which also checks for the global variable and finally fallbacks to an {} so that nothing breaks. Please give it a try (clone, run npm build and place it in your node_modules folder) and we can cut a release if it works or you.

thgreasi commented 8 years ago

This should be fixed in v2.3.0 that I just released. Feel free to re-open. Any feedback will be welcome,