fix tests and code

This commit is contained in:
Tobias Koppers 2018-06-28 11:03:08 +02:00
parent 6478fb9e2a
commit 5c4ffd5b90
9 changed files with 130 additions and 30 deletions

View File

@ -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)};`
),
@ -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)};`
),

View File

@ -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) {

View File

@ -1,23 +0,0 @@
it("should load script without crossorigin attribute", function(done) {
import("./empty?a" /* webpackChunkName: "chunk-with-crossorigin-attr" */);
// if in browser context, test that crossorigin attribute was not added.
if (typeof document !== 'undefined') {
var script = document.querySelector('script[src="js/chunk-with-crossorigin-attr.web.js"]');
script.getAttribute('crossorigin').should.be.exactly(null);
}
done();
});
it("should load script with crossorigin attribute 'anonymous'", function(done) {
var originalValue = __webpack_public_path__;
__webpack_public_path__ = 'https://example.com/';
import("./empty?b" /* webpackChunkName: "chunk-without-crossorigin-attr" */);
__webpack_public_path__ = originalValue;
// if in browser context, test that crossorigin attribute was added.
if (typeof document !== 'undefined') {
var script = document.querySelector('script[src="https://example.com/js/chunk-without-crossorigin-attr.web.js"]');
script.getAttribute('crossorigin').should.be.exactly('anonymous');
}
__webpack_public_path__ = originalValue;
done();
});

View File

@ -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;
});

View File

@ -0,0 +1,13 @@
module.exports = {
target: "web",
output: {
chunkFilename: "[name].web.js",
crossOriginLoading: "anonymous"
},
performance: {
hints: false
},
optimization: {
minimize: false
}
};

View File

@ -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");

View File

@ -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;

View File

@ -20,6 +20,8 @@ class FakeElement {
this._type = type;
this._children = [];
this._attributes = Object.create(null);
this._src = undefined;
this._href = undefined;
}
appendChild(node) {
@ -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;
}
}