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

Let .loadPlugins return pundle instance #61

Closed
amio opened this issue Jul 29, 2016 · 5 comments
Closed

Let .loadPlugins return pundle instance #61

amio opened this issue Jul 29, 2016 · 5 comments
Labels

Comments

@amio
Copy link

amio commented Jul 29, 2016

This is a discussion about api: what about let .loadPlugins(and .compile(), .generate(), etc.) return promise(which resolves a pundle instance), so we can use it like this:

import Pundle from 'pundle'

// create pundle instance
new Pundle({
  entry: ['index.js'],
  rootDirectory: process.cwd(),
  pathType: 'filePath',
  moduleDirectories: ['node_modules'],
})

// load plugins
.loadPlugins([
  [require.resolve('babel-pundle'), {
    config: {
      presets: ['steelbrain']
    }
  }],
  require.resolve('pundle-some-magical-plugin'),
])

// compile
.then(function(pundle) {
  return pundle.compile()
})

// config loaders
.then(function(pundle) {
  pundle.loadLoaders([
    { extensions: ['.coffee'], loader: require('pundle-coffee') },
    { extensions: ['.less'], loader: require('pundle-less') },
  ])
  return pundle.generate({ sourceMap: true })
})

// output
.then(function(generated) {
  FS.writeFileSync('./bundle.js', `${generated.contents}\n//# sourceMappingURL=bundle.js.map`)
  FS.writeFileSync('./bundle.js.map', generated.sourceMap)
}).catch(function(error) {
  console.error('error', error)
})

This might looks a bit more simpler & consistent than the original example IMHO, what do you think? @steelbrain

@steelbrain
Copy link
Owner

steelbrain commented Jul 29, 2016

Sounds rather appealing. The one problem that'll rise up is that while we could make .loadPlugins act like that, we won't be able to do the same with .loadLoaders because it returns the names of loaders overwritten and at some point in the future might be async.

Here's what already works with slight modifications to code :)

import Pundle from 'pundle'

// create pundle instance
const pundle = new Pundle({
  entry: ['index.js'],
  rootDirectory: process.cwd(),
  pathType: 'filePath',
  moduleDirectories: ['node_modules'],
})

// load plugins
pundle.loadPlugins([
  [require.resolve('babel-pundle'), {
    config: {
      presets: ['steelbrain']
    }
  }],
  require.resolve('pundle-some-magical-plugin'),
])

// compile
.then(function() {
  return pundle.compile()
})

// config loaders
.then(function() {
  pundle.loadLoaders([
    { extensions: ['.coffee'], loader: require('pundle-coffee') },
    { extensions: ['.less'], loader: require('pundle-less') },
  ])
  return pundle.generate({ sourceMap: true })
})

// output
.then(function(generated) {
  FS.writeFileSync('./bundle.js', `${generated.contents}\n//# sourceMappingURL=bundle.js.map`)
  FS.writeFileSync('./bundle.js.map', generated.sourceMap)
}).catch(function(error) {
  console.error('error', error)
})

@amio
Copy link
Author

amio commented Jul 29, 2016

Fast reply! A few more question (I'm fresh & excite about this project, pardon me I haven't dig enough into the code):

  • Is the name of loaders overwritten meant to be used in some situation? It seems hasn't been used in the example.
  • What's the difference between .compile() and .generate()?
  • Should I compile before loadLoaders? Or the order is irrelevant?

@steelbrain
Copy link
Owner

steelbrain commented Jul 29, 2016

At this point I'm honestly not sure what the name of the overwritten loaders would do but I had to find a way to tell people that they overwrote something. We could look into improving that.

Pundle has two stages of module compilation

  1. Reading & Processing the modules
  2. Generating the output

The .compile() function basically does the first one while the .generate() one generates the output from already read and processed modules.

You shouldn't compile before loaders are loaded because then the extensions those loaders register will not be understood and will likely result in an error :)

@amio
Copy link
Author

amio commented Jul 30, 2016

Thanks!

I forgot to say, I really love the use of Promise chains, it makes the build script flows like a stream, clean & intuitive, good work!

For the overwritten loaders, I think a console warning would be good :)

@steelbrain
Copy link
Owner

Fixed in Pundle v2Alpha1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants