Skip to content

Commit

Permalink
feat: move taglib apis into compiler source code
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Mar 1, 2021
1 parent 16233f7 commit f7cbb1b
Show file tree
Hide file tree
Showing 30 changed files with 70 additions and 163 deletions.
6 changes: 1 addition & 5 deletions packages/compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
"strip-ansi": "^5.2.0"
},
"devDependencies": {
"@marko/translator-default": "^5.1.21",
"marko": "^5.1.20"
"@marko/translator-default": "^5.1.21"
},
"files": [
"dist",
Expand All @@ -47,9 +46,6 @@
"main": "src/index.js",
"main:dev": "src/index.js",
"main:npm": "dist/index.js",
"peerDependencies": {
"marko": "^5.0.0"
},
"publishConfig": {
"access": "public"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
var nodePath = require("path");
var lassoCachingFS = require("lasso-caching-fs");
var resolveFrom = require("resolve-from").silent;
var taglibLoader = require("../taglib-loader");
var taglibLoader = require("../loader");
var lassoPackageRoot = require("lasso-package-root");
var scanTagsDir = require("../taglib-loader/scanTagsDir");
var DependencyChain = require("../taglib-loader/DependencyChain");
var scanTagsDir = require("../loader/scanTagsDir");
var DependencyChain = require("../loader/DependencyChain");
var hasOwnProperty = Object.prototype.hasOwnProperty;

var findCache = {};
Expand Down
37 changes: 12 additions & 25 deletions packages/compiler/src/taglib/index.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,17 @@
let loader, finder, registeredTaglibs, TaglibLookup;

if (
(process.env.MARKO_DEBUG != null &&
(process.env.MARKO_DEBUG !== "false" || process.env.MARKO_DEBUG !== "0")) ||
process.env.NODE_ENV == null ||
process.env.NODE_ENV === "development" ||
process.env.NODE_ENV === "dev"
) {
loader = require("marko/src/taglib/taglib-loader");
finder = require("marko/src/taglib/taglib-finder");
registeredTaglibs = require("marko/src/taglib/taglib-lookup")
.registeredTaglibs;
TaglibLookup = require("marko/src/taglib/taglib-lookup/TaglibLookup");
} else {
loader = require("marko/dist/taglib/taglib-loader");
finder = require("marko/dist/taglib/taglib-finder");
registeredTaglibs = require("marko/dist/taglib/taglib-lookup")
.registeredTaglibs;
TaglibLookup = require("marko/dist/taglib/taglib-lookup/TaglibLookup");
}
import loader from "./loader";
import finder from "./finder";
import Lookup from "./lookup";

export const excludeDir = finder.excludeDir;
export const excludePackage = finder.excludePackage;

const registeredTaglibs = [];
const loadedTranslatorsTaglibs = new Map();
let lookupCache = Object.create(null);

register(require.resolve("./html/marko.json"), require("./html/marko.json"));
register(require.resolve("./svg/marko.json"), require("./svg/marko.json"));
register(require.resolve("./math/marko.json"), require("./math/marko.json"));
register(require.resolve("./marko-html.json"), require("./marko-html.json"));
register(require.resolve("./marko-svg.json"), require("./marko-svg.json"));
register(require.resolve("./marko-math.json"), require("./marko-math.json"));

export function buildLookup(dirname, translator) {
if (!translator || !Array.isArray(translator.taglibs)) {
Expand All @@ -53,7 +36,7 @@ export function buildLookup(dirname, translator) {
let lookup = lookupCache[cacheKey];

if (!lookup) {
lookup = lookupCache[cacheKey] = new TaglibLookup();
lookup = lookupCache[cacheKey] = new Lookup();
for (const taglib of taglibsForDir) {
lookup.addTaglib(taglib);
if (taglib.imports) {
Expand All @@ -79,6 +62,10 @@ export function clearCaches() {
lookupCache = Object.create(null);
}

// Used by legacy compiler api.
export const _loader = loader;
export const _finder = finder;

function loadTaglib(id, props) {
return loader.loadTaglibFromProps(loader.createTaglib(id), props);
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

var ok = require("assert").ok;
var taglibTypes = require("../taglib-loader/types");
var taglibTypes = require("../loader/types");
var extend = require("raptor-util/extend");
var hasOwnProperty = Object.prototype.hasOwnProperty;

Expand Down
File renamed without changes.
47 changes: 11 additions & 36 deletions packages/marko/src/compiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ var fs = require("fs");
var taglib = require("../taglib");
var defaults = extend({}, globalConfig);

// eslint-disable-next-line no-constant-condition
if ("MARKO_DEBUG") {
complain(
"Using `marko/compiler` has been deprecated, please upgrade to the `@marko/compiler` module."
);
}

var defaultOptionsExportDefinition = {
get: function() {
return globalConfig;
Expand Down Expand Up @@ -132,42 +139,10 @@ exports.compileFileForBrowser = compileFileForBrowser;

exports.configure = configure;

// TODO: resolve these circular dep issues.
Object.defineProperties(exports, {
taglibLookup: {
get() {
return taglib.lookup;
}
},
taglibLoader: {
get() {
return taglib.loader;
}
},
taglibFinder: {
get() {
return taglib.finder;
}
},
buildTaglibLookup: {
get() {
return (dir, translator) => {
if (!translator || !Array.isArray(translator.taglibs)) {
translator = require("@marko/translator-default");
// eslint-disable-next-line no-constant-condition
if ("MARKO_DEBUG") {
complain(
"buildTaglibLookup now requires passing in a transltor as the second argument, eg `buildTaglibLookup(dir, require('@marko/translator-default'))`."
);
}
}

return compiler.taglib.buildLookup(dir, translator);
};
}
}
});

exports.taglibLookup = taglib.lookup;
exports.taglibLoader = taglib.loader;
exports.taglibFinder = taglib.finder;
exports.buildTaglibLookup = taglib.lookup.buildLookup;
exports.clearCaches = function clearCaches() {
compiler._clearDefaults();
taglib.clearCache();
Expand Down
13 changes: 7 additions & 6 deletions packages/marko/src/node-require/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ const fs = require("fs");
const fsReadOptions = { encoding: "utf8" };
const requiredCompilerOptions = { modules: "cjs" };
// eslint-disable-next-line no-constant-condition
const defaultCompilerOptions = { sourceMaps: "MARKO_DEBUG" ? "inline" : false };
const defaultCompilerOptions = {
sourceMaps: "MARKO_DEBUG" ? "inline" : false,
meta: true
};
const MARKO_EXTENSIONS = Symbol("MARKO_EXTENSIONS");

function normalizeExtension(extension) {
Expand All @@ -18,17 +21,16 @@ function normalizeExtension(extension) {

function compile(templatePath, markoCompiler, userCompilerOptions) {
var templateSrc = fs.readFileSync(templatePath, fsReadOptions);
return markoCompiler.compile(
return markoCompiler.compileSync(
templateSrc,
templatePath,
Object.assign(
{},
markoCompiler.defaultOptions,
defaultCompilerOptions,
userCompilerOptions,
requiredCompilerOptions
)
);
).code;
}

function install(options) {
Expand All @@ -39,7 +41,6 @@ function install(options) {
: require.extensions;

var compilerOptions = options.compilerOptions;
require("../compiler").configure(compilerOptions);

var extensions = [];

Expand All @@ -59,7 +60,7 @@ function install(options) {
// Resolve the appropriate compiler relative to the location of the
// marko template file on disk using the "resolve-from" module.
var dirname = path.dirname(filename);
var markoCompilerModulePath = resolveFrom(dirname, "marko/compiler");
var markoCompilerModulePath = resolveFrom(dirname, "@marko/compiler");
var markoCompiler = require(markoCompilerModulePath);

// Now use the appropriate Marko compiler to compile the Marko template
Expand Down
52 changes: 35 additions & 17 deletions packages/marko/src/taglib/index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,50 @@
"use strict";

const taglibFinder = require("./taglib-finder");
const taglibLookup = require("./taglib-lookup");
const taglibLoader = require("./taglib-loader");
const complain = "MARKO_DEBUG" && require("complain");
const compiler = require("@marko/compiler");

function clearCache() {
taglibLookup.clearCache();
taglibFinder.clearCache();
taglibLoader.clearCache();
compiler.taglib.clearCaches();
}

function register(taglibProps, taglibPath) {
const taglib = taglibLoader.createTaglib(taglibPath);
taglibLoader.loadTaglibFromProps(taglib, taglibProps);
taglibLookup.registerTaglib(taglib);
return compiler.taglib.register(taglibPath, taglibProps);
}

function registerFromFile(taglibPath) {
const taglib = taglibLoader.loadTaglibFromFile(taglibPath);
taglibLookup.registerTaglib(taglib);
return register(
compiler.taglib._loader.loadTaglibFromFile(taglibPath),
taglibPath
);
}

exports.clearCache = clearCache;
exports.register = register;
exports.registerFromFile = registerFromFile;
exports.buildLookup = taglibLookup.buildLookup;
exports.excludeDir = taglibFinder.excludeDir;
exports.excludePackage = taglibFinder.excludePackage;
exports.finder = taglibFinder;
exports.lookup = taglibLookup;
exports.loader = taglibLoader;
exports.buildLookup = compiler.taglib.buildLookup;
exports.excludeDir = compiler.taglib.excludeDir;
exports.excludePackage = compiler.taglib.excludePackage;
exports.loader = compiler.taglib._loader;
exports.finder = compiler.taglib._finder;
exports.lookup = {
buildLookup: function(dir, translator) {
if (!translator || !Array.isArray(translator.taglibs)) {
translator = require("@marko/translator-default");
// eslint-disable-next-line no-constant-condition
if ("MARKO_DEBUG") {
complain(
"buildTaglibLookup now requires passing in a transltor as the second argument, eg `buildTaglibLookup(dir, require('@marko/translator-default'))`."
);
}
}

return compiler.taglib.buildLookup(dir, translator);
},
registerTaglib(taglib) {
if (typeof taglib === "string") {
registerFromFile(taglib);
} else {
register(taglib, taglib.id);
}
}
};
70 changes: 0 additions & 70 deletions packages/marko/src/taglib/taglib-lookup/index.js

This file was deleted.

0 comments on commit f7cbb1b

Please sign in to comment.