Skip to content

Commit

Permalink
Feature: Inline Taglib Hooks (#1689)
Browse files Browse the repository at this point in the history
* feat: inline taglib hooks
  • Loading branch information
Ryan Carniato committed Apr 30, 2021
1 parent bb82580 commit 94caebb
Show file tree
Hide file tree
Showing 26 changed files with 118 additions and 712 deletions.
12 changes: 8 additions & 4 deletions packages/babel-utils/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export interface AttributeDefinition {
descriptionMoreURL?: string;
}>;
}
export type PluginDefinition = {
path?: string;
hook: Plugin;
}
export interface TagDefinition {
dir: string;
filePath: string;
Expand Down Expand Up @@ -50,10 +54,10 @@ export interface TagDefinition {
isRepeated: boolean;
openTagOnly: boolean;
targetProperty: string;
codeGeneratorModulePath?: string;
nodeFactoryPath?: string;
transformers?: string[];
migrators?: string[];
translator?: PluginDefinition;
parser?: PluginDefinition;
transformers?: PluginDefinition[];
migrators?: PluginDefinition[];
parseOptions?: {
rootOnly?: boolean,
rawOpenTag?: boolean,
Expand Down
24 changes: 14 additions & 10 deletions packages/compiler/src/babel-plugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { buildLookup } from "../taglib";
import { parseMarko } from "./parser";
import { visitor as migrate } from "./plugins/migrate";
import { visitor as transform } from "./plugins/transform";
import markoModules from "../../modules";
import { MarkoFile } from "./file";
import { curFS, setFS } from "../taglib/fs";
import tryLoadTranslator from "../util/try-load-translator";
Expand Down Expand Up @@ -177,11 +176,12 @@ export function getMarkoFile(code, jsParseOptions, markoOpts) {
file.path.scope.crawl(); // Initialize bindings.

for (const id in taglibLookup.taglibsById) {
const { migratorPath } = taglibLookup.taglibsById[id];
if (migratorPath) {
const mod = markoModules.require(migratorPath);
meta.watchFiles.push(migratorPath);
rootMigrators.push(mod.default || mod);
const { migrators } = taglibLookup.taglibsById[id];
for (const migrator of migrators) {
if (migrator.path) {
meta.watchFiles.push(migrator.path);
}
rootMigrators.push(migrator.hook.default || migrator.hook);
}
}

Expand All @@ -191,10 +191,14 @@ export function getMarkoFile(code, jsParseOptions, markoOpts) {
return file;
}

for (const { path: transformerPath } of taglibLookup.merged.transformers) {
const mod = markoModules.require(transformerPath);
meta.watchFiles.push(transformerPath);
rootTransformers.push(mod.default || mod);
for (const id in taglibLookup.taglibsById) {
const { transformers } = taglibLookup.taglibsById[id];
for (const transformer of transformers) {
if (transformer.path) {
meta.watchFiles.push(transformer.path);
}
rootTransformers.push(transformer.hook.default || transformer.hook);
}
}

traverseAll(file, rootTransformers);
Expand Down
10 changes: 5 additions & 5 deletions packages/compiler/src/babel-plugin/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import parseParams from "./util/parse-params";
import parseVar from "./util/parse-var";
import parseIDShorthand from "./util/parse-id-shorthand";
import parseClassnameShorthand from "./util/parse-classname-shorthand";
import markoModules from "../../modules";
import * as t from "../babel-types";
import {
withLoc,
Expand Down Expand Up @@ -316,11 +315,12 @@ export function parseMarko(file) {
);
}

if (tagDef && tagDef.nodeFactoryPath) {
const module = markoModules.require(tagDef.nodeFactoryPath);
watchFiles.push(tagDef.nodeFactoryPath);
if (tagDef && tagDef.parser) {
if (tagDef.parser.path) {
watchFiles.push(tagDef.parsePath);
}
/* istanbul ignore next */
(module.default || module)(tag, t);
(tagDef.parser.hook.default || tagDef.parser.hook)(tag, t);
}

currentTag = currentTag.parentPath.parentPath || file.path;
Expand Down
12 changes: 6 additions & 6 deletions packages/compiler/src/babel-plugin/plugins/migrate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { getTagDef, getTagDefForTagName } from "@marko/babel-utils";
import * as t from "../../babel-types";
import markoModules from "../../../modules";
import { enter, exit } from "../util/plugin-hooks";

/**
Expand Down Expand Up @@ -41,11 +40,12 @@ function getMigratorsForTag(path) {
if (!migrators) {
migrators = MIGRATOR_CACHE[tagName] = [];
const addMigrators = tagDef => {
if (tagDef && tagDef.migratorPaths) {
for (let i = 0; i < tagDef.migratorPaths.length; i++) {
const migratorPath = tagDef.migratorPaths[i];
watchFiles.push(migratorPath);
migrators.push(markoModules.require(migratorPath));
if (tagDef && tagDef.migrators) {
for (const migrator of tagDef.migrators) {
if (migrator.path) {
watchFiles.push(migrator.path);
}
migrators.push(migrator.hook);
}
}
};
Expand Down
24 changes: 6 additions & 18 deletions packages/compiler/src/babel-plugin/plugins/transform.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { getTagDef, getTagDefForTagName } from "@marko/babel-utils";
import * as t from "../../babel-types";
import markoModules from "../../../modules";
import { enter, exit } from "../util/plugin-hooks";

/**
Expand Down Expand Up @@ -43,10 +42,12 @@ function getTransformersForTag(path) {
if (!transformers) {
transformers = TRANSFORMER_CACHE[tagName] = [];
const addTransformers = tagDef => {
if (tagDef) {
for (const transformerPath in tagDef.transformers) {
watchFiles.push(transformerPath);
transformers.push(tagDef.transformers[transformerPath]);
if (tagDef && tagDef.transformers) {
for (const transformer of tagDef.transformers) {
if (transformer.path) {
watchFiles.push(transformer.path);
}
transformers.push(transformer.hook);
}
}
};
Expand All @@ -56,20 +57,7 @@ function getTransformersForTag(path) {
if (tagName !== "*") {
addTransformers(getTagDefForTagName(file, "*"));
}

for (let i = 0; i < transformers.length; i++) {
transformers[i] = markoModules.require(transformers[i].path);
}

transformers.sort(comparePriority);
}

return transformers;
}

function comparePriority(a, b) {
a = a.priority || 0;
b = b.priority || 0;

return a - b;
}
42 changes: 4 additions & 38 deletions packages/compiler/src/taglib/loader/Tag.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"use strict";
var forEachEntry = require("raptor-util/forEachEntry");
var ok = require("assert").ok;
var path = require("path");
var hasOwnProperty = Object.prototype.hasOwnProperty;
Expand All @@ -11,9 +10,9 @@ class Tag {
this.dir = path.dirname(filePath);
}

this.migratorPaths = [];
this.migrators = [];
this.attributes = {};
this.transformers = {};
this.transformers = [];
this.patternAttributes = [];

// NOTE: We don't set this properties since
Expand All @@ -24,8 +23,8 @@ class Tag {
// this.taglibPath = null;
// this.name = undefined;
// this.renderer = null;
// this.codeGeneratorModulePath = null;
// this.nodeFactoryPath = null;
// this.translatePath = null;
// this.parsePath = null;
// this.template = null;
// this.nestedVariables = null;
// this.importedVariables = null;
Expand All @@ -40,21 +39,6 @@ class Tag {
// this._nodeFactory = undefined;
}

forEachTransformer(callback, thisObj) {
forEachEntry(this.transformers, function (key, transformer) {
callback.call(thisObj, transformer);
});
}
hasTransformers() {
/*jshint unused:false */
for (var k in this.transformers) {
if (hasOwnProperty.call(this.transformers, k)) {
return true;
}
}
return false;
}

addAttribute(attr) {
attr.filePath = this.filePath;

Expand Down Expand Up @@ -106,12 +90,6 @@ class Tag {
return hasOwnProperty.call(this.attributes, attrName);
}

addTransformer(transformer) {
var key = transformer.path;
transformer.taglibId = this.taglibId;
this.transformers[key] = transformer;
}

addNestedTag(nestedTag) {
ok(nestedTag.name, '"nestedTag.name" is required');

Expand All @@ -127,23 +105,11 @@ class Tag {

this.nestedTags[nestedTag.name] = nestedTag;
}
forEachNestedTag(callback, thisObj) {
if (!this.nestedTags) {
return;
}

forEachEntry(this.nestedTags, function (key, nestedTag) {
callback.call(thisObj, nestedTag);
});
}
hasNestedTags() {
return this.nestedTags != null;
}

forEachMigrator(callback, thisObj) {
this.migratorPaths.forEach(callback, thisObj);
}

toJSON() {
return this;
}
Expand Down
14 changes: 1 addition & 13 deletions packages/compiler/src/taglib/loader/Taglib.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"use strict";
var forEachEntry = require("raptor-util/forEachEntry");
var ok = require("assert").ok;
var path = require("path");
var loaders = require("./loaders");
Expand Down Expand Up @@ -32,6 +31,7 @@ class Taglib {
this.filePath = this.path /* deprecated */ = this.id = filePath;
this.dirname = path.dirname(this.filePath);
this.tags = {};
this.migrators = [];
this.transformers = [];
this.attributes = {};
this.patternAttributes = [];
Expand Down Expand Up @@ -72,18 +72,6 @@ class Taglib {
this.tags[tag.name] = tag;
tag.taglibId = this.id || this.path;
}
addTransformer(transformer) {
this.transformers.push(transformer);
}
forEachTag(callback, thisObj) {
forEachEntry(
this.tags,
function (key, tag) {
callback.call(thisObj, tag);
},
this
);
}

addImport(path) {
var importedTaglib = loaders.loadTaglibFromFile(path);
Expand Down
2 changes: 0 additions & 2 deletions packages/compiler/src/taglib/loader/Transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ class Transformer {
this.name = null;
this.tag = null;
this.path = null;
this.priority = null;
this._func = null;
this.properties = {};
}

toString() {
Expand Down
6 changes: 3 additions & 3 deletions packages/compiler/src/taglib/loader/loadAttributes.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
var ok = require("assert").ok;
var forEachEntry = require("raptor-util/forEachEntry");
var loaders = require("./loaders");

module.exports = function loadAttributes(value, parent, dependencyChain) {
ok(parent);
ok(dependencyChain);

forEachEntry(value, (attrName, attrProps) => {
for (const attrName in value) {
const attrProps = value[attrName];
var attr = loaders.loadAttributeFromProps(
attrName,
attrProps,
dependencyChain.append("@" + attrName)
);

parent.addAttribute(attr);
});
}
};

0 comments on commit 94caebb

Please sign in to comment.