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

Lifecycle-based Plugin Support #240

Closed
wants to merge 5 commits into from
Closed

Lifecycle-based Plugin Support #240

wants to merge 5 commits into from

Conversation

ulivz
Copy link
Member

@ulivz ulivz commented Apr 24, 2018

Background

We have discussed a lot about the plugin: the long discussion & vote, and these awesome PRs: #196 and #216,

But when I start to develop plugin at many different situations, I find that the really appropriate plugin mechanism must delay the timing of execution. i.e. we need to support life cycle to let plugin developers to be able to do what they want to do at the right moment.

In order to better describe the PR, I drew these two pictures.


vuepress dev    


vuepress build    

As shown above, and as we discussed earlier, if we just execute all the plugins at the end of the prepare, then once a plugin has written some files to outDir, then you will never see it, which is too bad.

So for now, a plugin would be like this:

module.exports = async function vuepressPluginCustomDomain({ domain } /** only extra plugin options**/) {
  this

   // Execute when prepare is finished, if it's in `build`, the ourDir has been emptied.
   // You do mix extra data and options here before APP launched.
  .ready(() => {
    console.log('Used vuepress-plugin-custom-domain')
  })

  // Execute at the first end of webpack compilation.
  // You can generate extra files here.
  .compiled(async ({ outDir }) => {
    if (process.env.NODE_ENV !== 'production') {
      return
    }
    await fs.ensureDir(outDir)
    await fs.writeFile(path.resolve(outDir, 'CHAME'), domain, 'utf-8')
    console.log('Generated CHAME file.')
  })

   // Execute at each update, only for `dev` 
  .updated(() => {
    console.log('Updated')
  })
}

What are the benefits of doing so? for example, if we make the internal log at a default hook handler when we detect there is no other hook handlers, you can override it via plugins, so there would born a separate very handsome plugin.

Of course, It's still worth discussing how to make it better, feel free to give your thoughts.

Thanks~

@ulivz ulivz requested a review from yyx990803 April 24, 2018 15:50
@mdaffin
Copy link
Contributor

mdaffin commented Apr 24, 2018

Nice work.

One missing bit is to decide if we want to load the theme as a plugin (if say index.js exists in the theme root) or otherwise allow themes to add to the list of plugins to load. But this could be done as a future pull request.

@TomPichaud TomPichaud mentioned this pull request Apr 25, 2018
6 tasks
return
}
await fs.ensureDir(outDir)
await fs.writeFile(path.resolve(outDir, 'CHAME'), domain, 'utf-8')
Copy link
Member

Choose a reason for hiding this comment

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

'CNAME' maybe

This was referenced May 1, 2018
@ulivz ulivz force-pushed the master branch 3 times, most recently from 14d9013 to c7c0cd9 Compare June 8, 2018 19:11
@ulivz ulivz force-pushed the master branch 5 times, most recently from c992e38 to c2eaff3 Compare July 12, 2018 18:01
@ulivz ulivz force-pushed the master branch 4 times, most recently from 0c3bc3a to cf1e6fc Compare July 28, 2018 08:12
@ulivz ulivz closed this Aug 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants