Skip to content

Commit 341070d

Browse files
committed
Add enabled option
1 parent 3114989 commit 341070d

File tree

5 files changed

+138
-14
lines changed

5 files changed

+138
-14
lines changed

README.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ It will be rendered like this:
2626
<!-- Start component: src/components/MyComponent.vue -->
2727
<div>
2828
My component file
29-
29+
3030
<!-- Start component: src/components/SubComponent.vue -->
3131
<div>Sub component</div>
3232
<!-- End component: src/components/SubComponent.vue -->
@@ -51,18 +51,22 @@ npm install vue-component-debug --save-dev
5151
To enable it, add the `VueComponentDebug` plugin to your Vue application. This can be done in your main entry file (e.g., `main.js` or `main.ts`):
5252

5353
```javascript
54-
import { createApp } from 'vue'
55-
import VueComponentDebug from 'vue-component-debug'
56-
import App from './App.vue'
54+
import { createApp } from 'vue';
55+
import VueComponentDebug from 'vue-component-debug';
56+
import App from './App.vue';
5757

58-
const app = createApp(App)
58+
const app = createApp(App);
5959

60-
app.use(VueComponentDebug)
60+
app.use(VueComponentDebug);
6161

62-
app.mount('#app')
62+
app.mount('#app');
6363
```
6464

65-
Comments will only be added in development mode.
65+
By default, comments will always be outputted while in development mode and removed in production mode. However, you're welcome to override this behavior by passing an `enabled` option to the plugin:
66+
67+
```javascript
68+
app.use(VueComponentDebug, { enabled: false });
69+
```
6670

6771
## Development
6872

@@ -78,4 +82,4 @@ npm run build
7882

7983
# Preview production build
8084
npm run preview
81-
```
85+
```

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { createComponentDebugMixin } from './mixins/componentDebug.js';
33
export { createComponentDebugMixin };
44

55
export default {
6-
install(app = {}) {
7-
const debugMixin = createComponentDebugMixin();
6+
install(app = {}, options = {}) {
7+
const debugMixin = createComponentDebugMixin(options);
88
app.mixin(debugMixin);
99
},
1010
};

src/mixins/componentDebug.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
export function createComponentDebugMixin() {
1+
export function createComponentDebugMixin(options = {}) {
2+
const { enabled = process.env.NODE_ENV === 'development' } = options;
3+
24
return {
35
mounted() {
4-
if (process.env.NODE_ENV !== 'development') {
6+
if (!enabled) {
57
return;
68
}
79

@@ -14,7 +16,7 @@ export function createComponentDebugMixin() {
1416
this.$el.parentNode?.insertBefore(endComment, this.$el.nextSibling);
1517
},
1618
beforeUnmount() {
17-
if (process.env.NODE_ENV !== 'development') {
19+
if (!enabled) {
1820
return;
1921
}
2022

tests/debugMixin.test.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,4 +229,74 @@ describe('createComponentDebugMixin', () => {
229229

230230
document.body.removeChild(container);
231231
});
232+
233+
it('respects enabled option when set to false', () => {
234+
process.env.NODE_ENV = 'development';
235+
236+
const TestComponent = {
237+
name: 'TestComponent',
238+
__file: 'src/components/TestComponent.vue',
239+
mixins: [createComponentDebugMixin({ enabled: false })],
240+
template: '<div>Test Content</div>',
241+
};
242+
243+
const wrapper = mount(TestComponent, {
244+
attachTo: document.body,
245+
});
246+
247+
const element = wrapper.element;
248+
249+
// Should not have comment siblings when disabled
250+
expect(element.previousSibling).toBeNull();
251+
expect(element.nextSibling).toBeNull();
252+
253+
wrapper.unmount();
254+
});
255+
256+
it('respects enabled option when set to true in production', () => {
257+
process.env.NODE_ENV = 'production';
258+
259+
const TestComponent = {
260+
name: 'TestComponent',
261+
__file: 'src/components/TestComponent.vue',
262+
mixins: [createComponentDebugMixin({ enabled: true })],
263+
template: '<div>Test Content</div>',
264+
};
265+
266+
const wrapper = mount(TestComponent, {
267+
attachTo: document.body,
268+
});
269+
270+
const element = wrapper.element;
271+
272+
// Should have comments even in production when explicitly enabled
273+
expect(element.previousSibling?.nodeType).toBe(Node.COMMENT_NODE);
274+
expect(element.nextSibling?.nodeType).toBe(Node.COMMENT_NODE);
275+
expect(element.previousSibling.nodeValue).toBe(' Start component: src/components/TestComponent.vue ');
276+
277+
wrapper.unmount();
278+
});
279+
280+
it('defaults to development mode behavior when no enabled option provided', () => {
281+
process.env.NODE_ENV = 'development';
282+
283+
const TestComponent = {
284+
name: 'TestComponent',
285+
__file: 'src/components/TestComponent.vue',
286+
mixins: [createComponentDebugMixin()],
287+
template: '<div>Test Content</div>',
288+
};
289+
290+
const wrapper = mount(TestComponent, {
291+
attachTo: document.body,
292+
});
293+
294+
const element = wrapper.element;
295+
296+
// Should have comments in development by default
297+
expect(element.previousSibling?.nodeType).toBe(Node.COMMENT_NODE);
298+
expect(element.nextSibling?.nodeType).toBe(Node.COMMENT_NODE);
299+
300+
wrapper.unmount();
301+
});
232302
});

tests/plugin.test.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,52 @@ describe('VueComponentDebug Plugin', () => {
186186

187187
wrapper.unmount();
188188
});
189+
190+
it('accepts enabled option and passes it to mixin', () => {
191+
process.env.NODE_ENV = 'development';
192+
193+
const TestComponent = {
194+
name: 'TestComponent',
195+
template: '<div>Test</div>',
196+
};
197+
198+
const wrapper = mount(TestComponent, {
199+
attachTo: document.body,
200+
global: {
201+
plugins: [[VueComponentDebug, { enabled: false }]],
202+
},
203+
});
204+
205+
const element = wrapper.element;
206+
207+
// Should not have comments when disabled via options
208+
expect(element.previousSibling).toBeNull();
209+
expect(element.nextSibling).toBeNull();
210+
211+
wrapper.unmount();
212+
});
213+
214+
it('can force enable in production mode via options', () => {
215+
process.env.NODE_ENV = 'production';
216+
217+
const TestComponent = {
218+
name: 'TestComponent',
219+
template: '<div>Test</div>',
220+
};
221+
222+
const wrapper = mount(TestComponent, {
223+
attachTo: document.body,
224+
global: {
225+
plugins: [[VueComponentDebug, { enabled: true }]],
226+
},
227+
});
228+
229+
const element = wrapper.element;
230+
231+
// Should have comments even in production when explicitly enabled
232+
expect(element.previousSibling?.nodeType).toBe(Node.COMMENT_NODE);
233+
expect(element.nextSibling?.nodeType).toBe(Node.COMMENT_NODE);
234+
235+
wrapper.unmount();
236+
});
189237
});

0 commit comments

Comments
 (0)