Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid including MSW in the bundle - option 2 #78

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions public/test-umd.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<html>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this file to test the UMD bundle

<head>
<title>Test</title>
</head>

<script src="https://unpkg.com/msw/lib/iife/index.js"></script>
<script src="../dist/fakerest.umd.cjs"></script>
<script>
const data = {
authors: [
{ id: 0, first_name: 'Leo', last_name: 'Tolstoi' },
{ id: 1, first_name: 'Jane', last_name: 'Austen' },
],
books: [
{ id: 0, author_id: 0, title: 'Anna Karenina' },
{ id: 1, author_id: 0, title: 'War and Peace' },
{ id: 2, author_id: 1, title: 'Pride and Prejudice' },
{ id: 3, author_id: 1, title: 'Sense and Sensibility' },
],
settings: {
language: 'english',
preferred_format: 'hardback',
},
};
const handler = FakeRest.getMswHandler({
baseUrl: 'http://localhost:3000',
data,
});
const worker = MockServiceWorker.setupWorker(handler);
worker.start({
quiet: true, // Instruct MSW to not log requests in the console
onUnhandledRequest: 'bypass', // Instruct MSW to ignore requests we don't handle
}).then(() => {
console.log('MSW worker started');
fetch('http://localhost:3000/authors').then((response) => {
return response.json();
}).then((data) => {
console.log('Received authors:', data);
});
});
</script>
</html>
3 changes: 2 additions & 1 deletion vite.config.min.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ export default defineConfig({
rollupOptions: {
// make sure to externalize deps that shouldn't be bundled
// into your library
external: ['lodash'],
external: ['lodash', 'msw'],
output: {
// Provide global variables to use in the UMD build
// for externalized deps
globals: {
lodash: '_',
msw: 'MockServiceWorker',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

},
},
},
Expand Down
3 changes: 2 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ export default defineConfig({
rollupOptions: {
// make sure to externalize deps that shouldn't be bundled
// into your library
external: ['lodash'],
external: ['lodash', 'msw'],
output: {
// Provide global variables to use in the UMD build
// for externalized deps
globals: {
lodash: '_',
msw: 'MockServiceWorker',
},
},
},
Expand Down
Loading