Skip to content

Commit 55ec257

Browse files
committed
feat: handle nonce/csp
1 parent 042258b commit 55ec257

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

package/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export const createWebComponent = ({
1717
asyncInitialization = () => Promise.resolve(),
1818
loaderAttribute = 'data-web-component-loader',
1919
hideSlotContentUntilMounted = false,
20+
nonce
2021
}) => {
2122
if (!rootComponent) {
2223
console.warn('No root component provided. Please provide a root component to create a web component.')
@@ -59,7 +60,8 @@ export const createWebComponent = ({
5960
replaceRootWithHostInCssFramework,
6061
asyncInitialization,
6162
loaderAttribute,
62-
hideSlotContentUntilMounted
63+
hideSlotContentUntilMounted,
64+
nonce
6365
}, ).then((customElementConfig) => {
6466
customElements.define(
6567
elementName,

package/src/web-component-util.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ export const defineCustomElement = ({
4040
replaceRootWithHostInCssFramework,
4141
asyncInitialization,
4242
loaderAttribute,
43-
hideSlotContentUntilMounted
43+
hideSlotContentUntilMounted,
44+
nonce
4445
}) =>
4546
{
4647
const customElementDefiner = disableShadowDOM ? VueDefineCustomElementPatch : VueDefineCustomElement
@@ -50,6 +51,7 @@ export const defineCustomElement = ({
5051
: cssFrameworkStyles;
5152
const customElementConfig = customElementDefiner({
5253
styles: [modifiedCssFrameworkStyles],
54+
nonce,
5355
props: {
5456
...rootComponent.props,
5557
modelValue: { type: [String, Number, Boolean, Array, Object] } // v-model support
@@ -82,6 +84,7 @@ export const defineCustomElement = ({
8284
if (styles?.length) {
8385
this.__style = document.createElement('style')
8486
this.__style.innerText = styles.join().replace(/\n/g, '')
87+
if (nonce) this.__style.setAttribute('nonce', nonce);
8588
nearestElement(this.$el).append(this.__style)
8689
}
8790
}

package/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export interface CreateWebComponentOptions {
1919
asyncInitialization?: () => Promise<any>
2020
loaderAttribute?: string
2121
hideSlotContentUntilMounted?: boolean
22+
nonce?: string
2223
}
2324

2425
export function createWebComponent(options: CreateWebComponentOptions): void

0 commit comments

Comments
 (0)