Ошибка StealJS: перекрестные запросы не поддерживаются для схем протоколов

У меня возникает такая ошибка:

XMLHttpRequest cannot load file:///Users/mshin/workspace/spirent/trinity/trinity-link/public/node_modules/can/view/stache/system.js. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

home.js:

import can from 'can';
import template from './home.stache!';

export default can.Component.extend({
  tag: 'home',
  template: template
});

Конфиг сборки в Gruntfile:

"steal-build": {
  default: {
    options: {
      system: {
        config: "package.json!npm",
        bundlesPath: "production"
      },
      buildOptions: {
        minify: true,
        sourceMaps: true
      }
    }
  }
}

Я не знаю, почему я получаю это сообщение об ошибке в производственном режиме. В среде разработки работает нормально. Кроме того, это нормально в тестовой среде. Я запускаю тест QUnit с испытуемым.

home_test.js:

import QUnit from 'steal-qunit';
import ViewModel from 'components/home/home';
import can from 'can';
import stache from 'can/view/stache/';
import $ from 'jquery';

var template = can.stache('<home></home>'),
    $component;

QUnit.module('home component', {
  setup: function() {
    $('#trinity-js-tests').html(template());
    $component = $('#trinity-js-tests').find('home');
  }
});

QUnit.test('title tag', function() {
  QUnit.ok($component.find('h1'), 'Page shows title with <h1> tag');
});

QUnit.test('title content', function() {
  QUnit.strictEqual($component.find('h1').text(), 'This is homepage.', 'Title is "This is homepage."');
});

Однако, если я изменю home.js следующим образом:

import can from 'can';

export default can.Component.extend({
  tag: 'home',
  template: can.view('components/home/home.stache)
});

Он отлично работает в среде производства и разработки, но в тесте последний тестовый пример дает сбой.

1) QUnit "public/tests/test.html" on PhantomJS 1.9.8 / Mac OS X 0.0.0: <a href="http://localhost:3996/public/tests/test.html?__token=k8l88m"></a>  home component title content Title is "This is homepage.":
     Error: Expected This is homepage. but was
      at http://localhost:3996/public/tests/test.html?__token=k8l88m:1340
      at http://localhost:3996/public/tests/test.html?__token=k8l88m:1907
      at :33
      at http://localhost:3996/public/tests/test.html?__token=k8l88m:895
      at http://localhost:3996/public/tests/test.html?__token=k8l88m:1024
      at process (http://localhost:3996/public/tests/test.html?__token=k8l88m:583)
      at begin (http://localhost:3996/public/tests/test.html?__token=k8l88m:628)
      at http://localhost:3996/public/tests/test.html?__token=k8l88m:644

person Mason Shin    schedule 26.06.2015    source источник


Ответы (2)


Похоже, вы загружаете его из протокола file://, это правильно? Если да, то это, вероятно, ваша проблема.

person Matthew Phillips    schedule 26.06.2015
comment
Тогда как я могу загрузить файл с протоколом «http» в производственном режиме? - person Mason Shin; 26.06.2015
comment
Я не знаю, почему can/view/stache/system.js загружается по протоколу file://, а не по имени каталога приложения..... - person Mason Shin; 26.06.2015
comment
Нахожу причину и решение. Я не импортировал файл can/view/stache/system.js в основной файл. Я хотя файл can/view/stache/stache.js импортирую все, но это не так. Поэтому я добавил import 'can/view/stache/system'; в основной файл javascript. Спасибо. - person Mason Shin; 26.06.2015

Я нашел то, что пропустил в коде.

main.js

import $ from 'jquery';
import _ from 'lodash';
import stache from 'can/view/stache/';
import can from 'can';
import 'components/route/';
import 'less/trinity.less!';

Я не импортировал can/view/stache/system в него. Чтобы использовать системный плагин в can.stache, мне нужно импортировать определенный файл в основной файл javascript.

Итак, файл должен быть

import $ from 'jquery';
import _ from 'lodash';
import stache from 'can/view/stache/';
import 'can/view/stache/system';   // this is the added line
import can from 'can';
import 'components/route/';
import 'less/trinity.less!';
person Mason Shin    schedule 26.06.2015