File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import { onAttributeRemoved } from "./mutation" ;
22import { elementBoundEffect , isReactive } from "./reactivity" ;
3- import { applyModifiers } from "./modifiers" ;
3+ import { applyModifiers , parseModifier } from "./modifiers" ;
44import { getClosestController , evaluateControllerProperty } from "./controller" ;
5+ import { options } from "./lifecycle" ;
56
67let directiveHandlers = { } ;
78let isDeferringHandlers = false ;
@@ -19,7 +20,16 @@ export function directiveExists(name) {
1920}
2021
2122export 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 - z A - Z 0 - 9 \- _ ] + ) # / ) ;
139151 if ( ! identifierMatch ) {
140152 console . warn ( `Invalid binding descriptor ${ bindingExpression } ` ) ;
Original file line number Diff line number Diff line change @@ -14,15 +14,19 @@ import { deferHandlingDirectives, directives } from "./directives";
1414
1515const defaultOptions = {
1616 optIn : false ,
17+ compileDirectives : true ,
1718} ;
1819
1920let markerCount = 1 ;
2021let application = null ;
22+ let options = defaultOptions ;
2123
2224export 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 } ;
Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ export function modifier(name, handler) {
99
1010export 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" ) {
You can’t perform that action at this time.
0 commit comments