Last active
March 9, 2020 19:53
-
-
Save LinusBorg/6d9919904ca395b418c8a86648ca656b to your computer and use it in GitHub Desktop.
Compiler Module for vue-loader to remove data-test attributes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* | |
* Usage: | |
* compilerModules: [ | |
* createDataAttributeRemover(['test', 'test-data']) | |
* ] | |
* | |
*/ | |
function createDataAttributeRemover(attributes = []) { | |
return { | |
preTransformNode(astEl) { | |
if (process.env.NODE_ENV === 'production') { | |
const { attrsMap, attrsList } = astEl | |
attributes.forEach(attr => { | |
const dataAttr = `data-${attr}` | |
if (attrsMap[dataAttr]) { | |
delete attrsMap[dataAttr] | |
const index = attrsList.findIndex(x => x.name == dataAttr) | |
attrsList.splice(index, 1) | |
} | |
}) | |
} | |
return astEl | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment