Skip to content

Commit 3ab1de5

Browse files
committed
Use compiled directives where available
1 parent 15e3c73 commit 3ab1de5

3 files changed

Lines changed: 23 additions & 6 deletions

File tree

src/directives.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { onAttributeRemoved } from "./mutation";
22
import { elementBoundEffect, isReactive } from "./reactivity";
3-
import { applyModifiers } from "./modifiers";
3+
import { applyModifiers, parseModifier } from "./modifiers";
44
import { getClosestController, evaluateControllerProperty } from "./controller";
5+
import { options } from "./lifecycle";
56

67
let directiveHandlers = {};
78
let isDeferringHandlers = false;
@@ -19,7 +20,16 @@ export function directiveExists(name) {
1920
}
2021

2122
export function directives(el, attributes) {
22-
const directives = Array.from(attributes).filter(isDirectiveAttribute).map(toParsedDirectives);
23+
let directives = [];
24+
25+
if (el.__stimulusX_directives) {
26+
console.log("using compiled directives");
27+
directives = el.__stimulusX_directives;
28+
} else {
29+
directives = Array.from(attributes).filter(isDirectiveAttribute).map(toParsedDirectives);
30+
if (options.compileDirectives) el.__stimulusX_directives = directives;
31+
}
32+
2333
return directives
2434
.flat()
2535
.filter((d) => d)
@@ -135,6 +145,8 @@ function toParsedDirectives({ name, value }) {
135145
modifiers.push("not");
136146
}
137147

148+
modifiers = modifiers.map((m) => parseModifier(m));
149+
138150
const identifierMatch = valueExpression.match(/^([a-zA-Z0-9\-_]+)#/);
139151
if (!identifierMatch) {
140152
console.warn(`Invalid binding descriptor ${bindingExpression}`);

src/lifecycle.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,19 @@ import { deferHandlingDirectives, directives } from "./directives";
1414

1515
const defaultOptions = {
1616
optIn: false,
17+
compileDirectives: true,
1718
};
1819

1920
let markerCount = 1;
2021
let application = null;
22+
let options = defaultOptions;
2123

2224
export function init(app, opts = {}) {
23-
const { optIn } = Object.assign({}, defaultOptions, opts);
25+
options = Object.assign({}, defaultOptions, opts);
2426
application = app;
2527

28+
const { optIn } = options;
29+
2630
// Override controller registration to insert a reactive subclass instead of the original
2731
application.register = function (identifier, ControllerClass) {
2832
let controllerConstructor;
@@ -75,6 +79,7 @@ export function destroyTree(root) {
7579
walk(root, (el) => {
7680
cleanupElement(el);
7781
cleanupAttributes(el);
82+
delete el.__stimulusX_directives;
7883
delete el.__stimulusX_marker;
7984
});
8085
}
@@ -123,4 +128,4 @@ function handleValueAttributes(el, attrs) {
123128
}
124129
}
125130

126-
export { application };
131+
export { application, options };

src/modifiers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export function modifier(name, handler) {
99

1010
export function applyModifiers(value, modifiers = []) {
1111
return modifiers.reduce((value, modifier) => {
12-
const { name, args } = parseModifierNameAndArguments(modifier);
12+
const { name, args } = modifier;
1313
if (modifierExists(name)) {
1414
return applyModifier(value, name, args);
1515
} else {
@@ -31,7 +31,7 @@ function getModifier(name) {
3131
return modifierHandlers.find((modifier) => modifier.name === name);
3232
}
3333

34-
function parseModifierNameAndArguments(modifier) {
34+
export function parseModifier(modifier) {
3535
const matches = modifier.match(/^([^\(]+)(?=\((?=(.*)\)$)|$)/);
3636

3737
if (matches && typeof matches[2] !== "undefined") {

0 commit comments

Comments
 (0)