Skip to content
This repository was archived by the owner on Oct 9, 2020. It is now read-only.
This repository was archived by the owner on Oct 9, 2020. It is now read-only.

CSSPluginBase.prototype.instantiate does not check if it is running in a browser. #111

@mikol-styra

Description

@mikol-styra

See: systemjs/plugin-less#6

When I try to use plugin-less in conjunction with server-side rendering, I get the following error:

err  (SystemJS) document is not defined
  ReferenceError: document is not defined
      at SystemJSNodeLoader.CSSPluginBase.instantiate (/path/to/jspm_packages/github/systemjs/plugin-css@0.1.30/css-plugin-base.js:64:24)
  Error loading /path/to/style.less!/path/to/jspm_packages/github/systemjs/plugin-less@0.1.2/less.js as "./style.less!" from /path/to/style.js

The root cause of the problem is CSSPluginBase.prototype.instantiate(), which attempts to inject a style element into the DOM without document being defined:

CSSPluginBase.prototype.instantiate = function(load) {
  if (this.builder)
    return;

  var style = document.createElement('style');
  style.type = 'text/css';
  style.innerHTML = load.metadata.style;
  document.head.appendChild(style);
};

Explicitly checking if document is defined in CSSPluginBase.prototype.instantiate() seems appropriate and works for me in practice:

  if (this.builder || typeof document === 'undefined')
    return;

But maybe that is not desirable for some reason?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions