Skip to content

Commit

Permalink
fix tests and code
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Jun 28, 2018
1 parent 6478fb9 commit 5c4ffd5
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 30 deletions.
4 changes: 2 additions & 2 deletions lib/web/JsonpMainTemplatePlugin.js
Expand Up @@ -162,7 +162,7 @@ class JsonpMainTemplatePlugin {
"script.src = jsonpScriptSrc(chunkId);",
crossOriginLoading
? Template.asString([
"if (script.src.indexOf(window.location.origin)) {",
"if (script.src.indexOf(window.location.origin + '/') !== 0) {",
Template.indent(
`script.crossOrigin = ${JSON.stringify(crossOriginLoading)};`
),
Expand Down Expand Up @@ -224,7 +224,7 @@ class JsonpMainTemplatePlugin {
"link.href = jsonpScriptSrc(chunkId);",
crossOriginLoading
? Template.asString([
"if (link.href.indexOf(window.location.origin)) {",
"if (link.href.indexOf(window.location.origin + '/') !== 0) {",
Template.indent(
`link.crossOrigin = ${JSON.stringify(crossOriginLoading)};`
),
Expand Down
9 changes: 8 additions & 1 deletion test/ConfigTestCases.test.js
Expand Up @@ -178,7 +178,14 @@ describe("ConfigTestCases", () => {
expect: expect,
setTimeout: setTimeout,
clearTimeout: clearTimeout,
document: new FakeDocument()
document: new FakeDocument(),
location: {
href: "https://test.cases/path/index.html",
origin: "https://test.cases",
toString() {
return "https://test.cases/path/index.html";
}
}
};

function _require(currentDirectory, module) {
Expand Down
23 changes: 0 additions & 23 deletions test/cases/crossorigin/set-crossorigin/index.js

This file was deleted.

67 changes: 67 additions & 0 deletions test/configCases/crossorigin/set-crossorigin/index.js
@@ -0,0 +1,67 @@
it("should load script without crossorigin attribute (default)", function() {
const promise = import("./empty?a" /* webpackChunkName: "crossorigin-default" */);

var script = document.head._children.pop();
__non_webpack_require__("./crossorigin-default.web.js");
expect(script.src).toBe("https://test.cases/path/crossorigin-default.web.js");
expect(script.crossOrigin).toBe(undefined);

return promise;
});

it("should load script without crossorigin attribute (relative)", function() {
var originalValue = __webpack_public_path__;
__webpack_public_path__ = "../";
const promise = import("./empty?b" /* webpackChunkName: "crossorigin-relative" */);
__webpack_public_path__ = originalValue;

var script = document.head._children.pop();
__non_webpack_require__("./crossorigin-relative.web.js");
expect(script.src).toBe("https://test.cases/crossorigin-relative.web.js");
expect(script.crossOrigin).toBe(undefined);

return promise;
});

it("should load script without crossorigin attribute (server relative)", function() {
var originalValue = __webpack_public_path__;
__webpack_public_path__ = "/server/";
const promise = import("./empty?c" /* webpackChunkName: "crossorigin-server-relative" */);
__webpack_public_path__ = originalValue;

var script = document.head._children.pop();
__non_webpack_require__("./crossorigin-server-relative.web.js");
expect(script.src).toBe("https://test.cases/server/crossorigin-server-relative.web.js");
expect(script.crossOrigin).toBe(undefined);

return promise;
});

it("should load script without crossorigin attribute (same origin)", function() {
var originalValue = __webpack_public_path__;
__webpack_public_path__ = "https://test.cases/";
const promise = import("./empty?d" /* webpackChunkName: "crossorigin-same-origin" */);
__webpack_public_path__ = originalValue;

var script = document.head._children.pop();
__non_webpack_require__("./crossorigin-same-origin.web.js");
expect(script.src).toBe("https://test.cases/crossorigin-same-origin.web.js");
expect(script.crossOrigin).toBe(undefined);

return promise;
});

it("should load script with crossorigin attribute anonymous (different origin)", function() {
var originalValue = __webpack_public_path__;
__webpack_public_path__ = "https://example.com/";
const promise = import("./empty?e" /* webpackChunkName: "crossorigin-different-origin" */);
__webpack_public_path__ = originalValue;


var script = document.head._children.pop();
__non_webpack_require__("./crossorigin-different-origin.web.js");
expect(script.src).toBe("https://example.com/crossorigin-different-origin.web.js");
expect(script.crossOrigin).toBe("anonymous");

return promise;
});
13 changes: 13 additions & 0 deletions test/configCases/crossorigin/set-crossorigin/webpack.config.js
@@ -0,0 +1,13 @@
module.exports = {
target: "web",
output: {
chunkFilename: "[name].web.js",
crossOriginLoading: "anonymous"
},
performance: {
hints: false
},
optimization: {
minimize: false
}
};
2 changes: 1 addition & 1 deletion test/configCases/split-chunks/runtime-chunk/a.js
Expand Up @@ -2,7 +2,7 @@ it("should be able to load the split chunk on demand", () => {
const promise = import(/* webpackChunkName: "shared" */ "./shared");

const script = document.head._children[0];
expect(script.src).toBe("dep~b~shared.js");
expect(script.src).toBe("https://test.cases/path/dep~b~shared.js");

__non_webpack_require__("./dep~b~shared.js");

Expand Down
4 changes: 1 addition & 3 deletions test/configCases/web/prefetch-preload/index.js
Expand Up @@ -5,18 +5,16 @@ let oldPublicPath;
beforeEach(() => {
oldNonce = __webpack_nonce__;
oldPublicPath = __webpack_public_path__;
global.location = {origin: "https://example.com"};
});

afterEach(() => {
__webpack_nonce__ = oldNonce;
__webpack_public_path__ = oldPublicPath;
delete global.location;
});

it("should prefetch and preload child chunks on chunk load", () => {
__webpack_nonce__ = "nonce";
__webpack_public_path__ = "/public/path/";
__webpack_public_path__ = "https://example.com/public/path/";

let link, script;

Expand Down
38 changes: 38 additions & 0 deletions test/helpers/FakeDocument.js
Expand Up @@ -20,6 +20,8 @@ class FakeElement {
this._type = type;
this._children = [];
this._attributes = Object.create(null);
this._src = undefined;
this._href = undefined;
}

appendChild(node) {
Expand All @@ -33,4 +35,40 @@ class FakeElement {
getAttribute(name) {
return this._attributes[name];
}

_toRealUrl(value) {
if (/^\//.test(value)) {
return `https://test.cases${value}`;
} else if (/^\.\.\//.test(value)) {
return `https://test.cases${value.substr(2)}`;
} else if (/^\.\//.test(value)) {
return `https://test.cases/path${value.substr(1)}`;
} else if (/^\w+:\/\//.test(value)) {
return value;
} else if (/^\/\//.test(value)) {
return `https:${value}`;
} else {
return `https://test.cases/path/${value}`;
}
}

set src(value) {
if (this._type === "script") {
this._src = this._toRealUrl(value);
}
}

get src() {
return this._src;
}

set href(value) {
if (this._type === "link") {
this._href = this._toRealUrl(value);
}
}

get href() {
return this._href;
}
}

0 comments on commit 5c4ffd5

Please sign in to comment.