2016-07-13 17:03:14 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2016-12-29 14:58:26 +08:00
|
|
|
"use strict";
|
2016-07-13 17:03:14 +08:00
|
|
|
|
2016-12-29 14:58:26 +08:00
|
|
|
class Entrypoint {
|
|
|
|
constructor(name) {
|
|
|
|
this.name = name;
|
|
|
|
this.chunks = [];
|
2018-01-11 00:08:56 +08:00
|
|
|
this.runtimeChunk = undefined;
|
2016-12-29 14:58:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unshiftChunk(chunk) {
|
2017-11-06 23:41:26 +08:00
|
|
|
const oldIdx = this.chunks.indexOf(chunk);
|
|
|
|
if(oldIdx > 0) {
|
|
|
|
this.chunks.splice(oldIdx, 1);
|
|
|
|
this.chunks.unshift(chunk);
|
|
|
|
} else if(oldIdx < 0 && chunk.addEntrypoint(this)) {
|
|
|
|
this.chunks.unshift(chunk);
|
|
|
|
}
|
2016-12-29 14:58:26 +08:00
|
|
|
}
|
2016-07-13 17:03:14 +08:00
|
|
|
|
2016-12-29 14:58:26 +08:00
|
|
|
insertChunk(chunk, before) {
|
2017-04-23 00:59:15 +08:00
|
|
|
const oldIdx = this.chunks.indexOf(chunk);
|
2016-12-29 14:58:26 +08:00
|
|
|
const idx = this.chunks.indexOf(before);
|
2017-04-23 00:59:15 +08:00
|
|
|
if(idx < 0) {
|
2016-12-29 14:58:26 +08:00
|
|
|
throw new Error("before chunk not found");
|
|
|
|
}
|
2017-04-23 00:59:15 +08:00
|
|
|
if(oldIdx >= 0 && oldIdx > idx) {
|
|
|
|
this.chunks.splice(oldIdx, 1);
|
|
|
|
this.chunks.splice(idx, 0, chunk);
|
|
|
|
} else if(oldIdx < 0) {
|
|
|
|
this.chunks.splice(idx, 0, chunk);
|
2017-11-06 23:41:26 +08:00
|
|
|
chunk.addEntrypoint(this);
|
2017-04-23 00:59:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-09 16:40:30 +08:00
|
|
|
remove() {
|
|
|
|
for(const chunk of this.chunks) {
|
|
|
|
chunk.removeEntrypoint(this);
|
|
|
|
}
|
|
|
|
this.chunks.length = 0;
|
|
|
|
}
|
|
|
|
|
2016-12-29 14:58:26 +08:00
|
|
|
getFiles() {
|
2017-11-06 23:41:26 +08:00
|
|
|
const files = new Set();
|
2016-12-01 12:37:14 +08:00
|
|
|
|
2016-12-29 14:58:26 +08:00
|
|
|
for(let chunkIdx = 0; chunkIdx < this.chunks.length; chunkIdx++) {
|
|
|
|
for(let fileIdx = 0; fileIdx < this.chunks[chunkIdx].files.length; fileIdx++) {
|
2017-11-06 23:41:26 +08:00
|
|
|
files.add(this.chunks[chunkIdx].files[fileIdx]);
|
2016-11-29 08:38:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-06 23:41:26 +08:00
|
|
|
return Array.from(files);
|
2016-12-29 14:58:26 +08:00
|
|
|
}
|
2017-04-23 00:59:15 +08:00
|
|
|
|
2018-01-11 00:08:56 +08:00
|
|
|
setRuntimeChunk(chunk) {
|
|
|
|
this.runtimeChunk = chunk;
|
|
|
|
}
|
|
|
|
|
2017-04-23 00:59:15 +08:00
|
|
|
getRuntimeChunk() {
|
2018-01-11 00:08:56 +08:00
|
|
|
return this.runtimeChunk || this.chunks[0];
|
2017-04-23 00:59:15 +08:00
|
|
|
}
|
2016-11-29 08:38:17 +08:00
|
|
|
}
|
2016-12-29 14:58:26 +08:00
|
|
|
|
|
|
|
module.exports = Entrypoint;
|