Skip to content
This repository has been archived by the owner on Mar 28, 2023. It is now read-only.

Commit

Permalink
Merge branch 'main' of github.com:useparcel/tape into main
Browse files Browse the repository at this point in the history
  • Loading branch information
avigoldman committed Dec 8, 2020
2 parents 4b87b57 + 72f09e2 commit c189923
Show file tree
Hide file tree
Showing 16 changed files with 91 additions and 54 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"packages": ["packages/*"],
"version": "0.1.1"
"version": "0.2.2"
}
2 changes: 1 addition & 1 deletion packages/plugin-css-inline/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/plugin-css-inline/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@useparcel/tape-css-inline",
"version": "0.1.0",
"version": "0.2.0",
"main": "dist/index.js",
"author": "Avi Goldman <avi@useparcel.com>",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-css/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/plugin-css/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@useparcel/tape-css",
"version": "0.1.0",
"version": "0.2.0",
"main": "dist/index.js",
"author": "Avi Goldman <avi@useparcel.com>",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-html-minify/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/plugin-html-minify/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@useparcel/tape-html-minify",
"version": "0.1.0",
"version": "0.2.0",
"main": "dist/index.js",
"author": "Avi Goldman <avi@useparcel.com>",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-html-prettify/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/plugin-html-prettify/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@useparcel/tape-html-prettify",
"version": "0.1.0",
"version": "0.2.0",
"main": "dist/index.js",
"author": "Avi Goldman <avi@useparcel.com>",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-html/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/plugin-html/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@useparcel/tape-html",
"version": "0.1.1",
"version": "0.2.0",
"main": "dist/index.js",
"author": "Avi Goldman <avi@useparcel.com>",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-sass/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/plugin-sass/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@useparcel/tape-sass",
"version": "0.1.0",
"version": "0.2.0",
"description": "",
"main": "dist/index.js",
"author": "Avi Goldman <avi@useparcel.com>",
Expand Down
111 changes: 74 additions & 37 deletions packages/tape/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
isMap,
isPlainObject,
isArray,
} from "lodash";
} from "lodash";
import { DepGraph as Graph } from "dependency-graph";
import { extname, dirname, basename, resolve as resolvePath } from "path";
import md5 from "md5";
Expand All @@ -31,6 +31,8 @@ class Tape {
#cache = new Map();
#emitter = mitt();
#idToPathMap = {};
pendingUpdates = [];
isCompiling = false;

constructor({ plugins = [], entry, files } = {}) {
if (!entry) {
Expand All @@ -54,52 +56,58 @@ class Tape {
mapValues(cloneDeep(files), this.#fileDefaults.bind(this)),
"id"
);

this.#emitter.on(
"processPendingUpdates",
this.#processPendingUpdates.bind(this)
);
}

update({ entry, plugins, files = {} } = {}) {
#processPendingUpdates() {
let updatedIds = [];

if (entry && entry !== this.entry) {
validatePath(entry);
this.entry = entry;
updatedIds.push(this.#pathToId(entry));
}
for (let { entry, plugins, files } of this.pendingUpdates) {
if (entry && entry !== this.entry) {
this.entry = entry;
updatedIds.push(this.#pathToId(entry));
}

if (plugins) {
this.plugins = loadPlugins(plugins);
if (plugins) {
this.plugins = loadPlugins(plugins);

const context = this.#cache.get("context");
// Mark everything as updated if we change plugins
if (context) {
updatedIds.push(...context.graph.overallOrder());
const context = this.#cache.get("context");
// Mark everything as updated if we change plugins
if (context) {
updatedIds.push(...context.graph.overallOrder());
}
}
}

for (const [path, file] of Object.entries(files)) {
validatePath(path);
validateGivenFile(file);
const id = this.#pathToId(path);
// new file
if (!has(this.files, id)) {
this.files[id] = this.#fileDefaults(file, path);
updatedIds.push(id);
}
// delete file
else if (isFalsy(file)) {
delete this.files[id];
updatedIds.push(id);
}
// update file
else if (file.content !== this.files[id].content) {
this.files[id].content = file.content;
updatedIds.push(id);
}
// no change
else {
// do nothing
for (const [path, file] of Object.entries(files)) {
const id = this.#pathToId(path);
// new file
if (!has(this.files, id)) {
this.files[id] = this.#fileDefaults(file, path);
updatedIds.push(id);
}
// delete file
else if (isFalsy(file)) {
delete this.files[id];
updatedIds.push(id);
}
// update file
else if (file.content !== this.files[id].content) {
this.files[id].content = file.content;
updatedIds.push(id);
}
// no change
else {
// do nothing
}
}
}

this.pendingUpdates = [];

/**
* no updates, so skip the event
*/
Expand All @@ -110,8 +118,30 @@ class Tape {
this.#emitter.emit("update", updatedIds);
}

update({ entry, plugins, files = {} } = {}) {
if (entry && entry !== this.entry) {
validatePath(entry);
}

if (plugins) {
// TODO: validate plugins here (not in `loadPlugins`)
}

for (const [path, file] of Object.entries(files)) {
validatePath(path);
validateGivenFile(file);
}

this.pendingUpdates.push({ entry, plugins, files });

if (this.isCompiling === false) {
this.#emitter.emit("processPendingUpdates");
}
}

async build() {
const context = { env: "production" };
this.isCompiling = true;
const results = await this.#compile(context);

const report = generateReporter();
Expand All @@ -120,6 +150,9 @@ class Tape {
report,
});

this.isCompiling = false;
this.#emitter.emit("processPendingUpdates");

return pick(
{
...results,
Expand All @@ -137,6 +170,7 @@ class Tape {
const onChangeAsset = this.#onChangeAsset.bind(this);
const idToPath = this.#idToPath.bind(this);
const pathToId = this.#pathToId.bind(this);
const instance = this;

/**
* Triggers a compliation using cache
Expand All @@ -150,6 +184,7 @@ class Tape {
) {
updatedIds = [...updatedIds];
const startedAt = Date.now();
instance.isCompiling = true;
emitter.emit(start, { startedAt });

const context = cache.get("context");
Expand Down Expand Up @@ -230,10 +265,12 @@ class Tape {
error,
});
}

instance.isCompiling = false;
emitter.emit("processPendingUpdates");
}

const onUpdate = (updatedIds) => triggerCompile(updatedIds);

emitter.on("update", onUpdate);

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/tape/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions packages/tape/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@useparcel/tape",
"version": "0.1.1",
"version": "0.2.2",
"description": "A browser-based HTML build tool",
"main": "dist/index.js",
"author": "Avi Goldman <avi@useparcel.com>",
Expand All @@ -12,8 +12,8 @@
"directory": "packages/tape"
},
"dependencies": {
"@useparcel/tape-css": "^0.1.0",
"@useparcel/tape-html": "^0.1.1",
"@useparcel/tape-css": "^0.2.0",
"@useparcel/tape-html": "^0.2.0",
"dependency-graph": "^0.9.0",
"lodash": "^4.17.20",
"md5": "^2.3.0",
Expand Down

0 comments on commit c189923

Please sign in to comment.