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

[Feature request] Support ?lazy for vite and webpack or at least ?inline for webpack #1351

Open
Mister-Hope opened this issue Jun 8, 2023 · 0 comments
Assignees

Comments

@Mister-Hope
Copy link
Member

Clear and concise description of the problem

Currently all styles are extracted to a single file then loaded at page entry. However, some components maybe async, e.g.:

export default defineAsyncComponent({
  loadingComponent: LoadingIcon,
  loader: () => Promise.all([
    import('lib'),
    import('lib/style.css'),
   ]).then([lib]) => defineComponent({
      // actual component
   })),
});

// or something like while calling it with defineAsyncComponent
// component.ts
import 'lib/styles.css';

export default defineComponent({
  setup: () => {
    onMounted(() => {
      import('lib').then([lib]) =>{
        // do something 
     });
   },
});

// client.ts
export default {
  enhance: ({app}) => {
    app.component('component', defineAsyncComponent(()=>import("./component.ts"))):
  },
}

In this case, lib/style.css should better be loaded with the component, i.e., the time entering a page that has that component.

With a lot of async components with styles, the main CSS can be at large size, while a large part of style declarations unused in page.

Suggested solution

Add support for ?lazy to mark a css, and left it without being extracted, this could be difficult as we must develop vite plugin and webpack loader.

export default defineAsyncComponent({
  loadingComponent: LoadingIcon,
  loader: () => Promise.all([
    import('lib'),
    import('lib/style.css?lazy'), // won't be extracted
   ]).then([lib]) => defineComponent({
      // actual component
   })),
});

// or something like while calling it with defineAsyncComponent
// component.ts
import 'lib/styles.css?lazy'; // won't be extracted

export default defineComponent({
  setup: () => {
    onMounted(() => {
      import('lib').then([lib]) =>{
        // do something 
     });
   },
});

// client.ts
export default {
  enhance: ({app}) => {
    app.component('component', defineAsyncComponent(()=>import("./component.ts"))):
  },
}

Alternative

Support ?inline modifier for webpack lite vite does, so we can import style content and load them with functions like useStyleTag from @vueuse, e.g.:

export default defineAsyncComponent({
  loadingComponent: LoadingIcon,
  loader: () => Promise.all([
    import('lib'),
    import('lib/style.css?inline'),
   ]).then([lib,{default: style ]) => defineComponent({
      // actual component
      setup(){
        useStyleTag(style, { id: 'ComponentName' });
        // ...
      },
   })),
});

Additional context

No response

@Mister-Hope Mister-Hope changed the title [Feature request] [Feature Request] Support ?lazy for vite and webpack or at least ?inline for webpack [Feature request] Support ?lazy for vite and webpack or at least ?inline for webpack Jun 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants