Skip to content

document globalOptions#365

Merged
danez merged 2 commits intobabel:masterfrom
anilanar:patch-1
Mar 4, 2017
Merged

document globalOptions#365
danez merged 2 commits intobabel:masterfrom
anilanar:patch-1

Conversation

@anilanar
Copy link
Contributor

@anilanar anilanar commented Jan 30, 2017

Fixes #364

globalOptions is useful when a babel plugin needs non-serializable options (such as functions, callbacks), thus it's worth mentioning in the README.

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)

  • docs update

`globalOptions` is useful when a babel plugin needs non-serializable options (such as functions, callbacks), thus it's worth mentioning in the README.
@@ -1,6 +1,6 @@
# babel-loader
[![NPM Status](https://img.shields.io/npm/v/babel-loader.svg?style=flat)](https://www.npmjs.com/package/babel-loader)
[![Build Status](https://travis-ci.org/babel/babel-loader.svg?branch=master)](https://travis-ci.org/babel/babel-loader)
Copy link
Contributor Author

@anilanar anilanar Jan 30, 2017

Choose a reason for hiding this comment

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

My editor automatically deletes trailing spaces, but I guess that's desirable for everyone so I'm keeping that change.

@danez
Copy link
Member

danez commented Jan 31, 2017

global options is not supported as of webpack 2, so I would not add it to the documentation anymore.
You can always use the query syntax:

module: {
  loaders: [
    {
      test: /\.js$/,
      exclude: /(node_modules|bower_components)/,
      loader: 'babel-loader',
      query: {
        presets: ['es2015']
      }
    }
  ]
}

@anilanar
Copy link
Contributor Author

anilanar commented Jan 31, 2017

@danez As I've mentioned:

globalOptions is useful when a babel plugin needs non-serializable options (such as functions, callbacks)

For example, I have some babel plugins to collect (and not transform) occurrences of a particular expression type in all of my javascript files and process collected expressions. One of those plugins collect translate(x) expressions where translate has a binding to a particular module. Webpack's own parser is not so great at doing that, and webpack parser kicks in after all transpiling is complete. So it's desirable to do it using a babel plugin.

Tricky part is how to send collected expressions to the outside world. For that, I pass a function callback as an option to the babel plugin. The only way of achieving that using babel-loader is by using global options.

For Webpack2, I'm hoping that rules.options is not stringified using JSON.stringify before it's passed to the loader. If it is (in which case functions cannot be used as an option), then webpack2 introduces LoaderOptionsPlugin to handle global options. If there is a webpack2 documentation for babel-loader I can update them accordingly.

@danez
Copy link
Member

danez commented Jan 31, 2017

For webpack2 the rules.options is directly forwarded https://github.com/webpack/loader-runner/blob/master/lib/LoaderRunner.js#L346

The Readme for babel-loader is for both webpack 1 & 2 right now, but we want to release a new major version soon for webpack 2 only.

@danez
Copy link
Member

danez commented Jan 31, 2017

Could you add a note that this form of defining options is deprecated in webpack2 and one should use rules.options (https://webpack.js.org/configuration/module/#rule-options-rule-query) there?

@codecov
Copy link

codecov bot commented Mar 4, 2017

Codecov Report

Merging #365 into master will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##           master     #365   +/-   ##
=======================================
  Coverage   80.92%   80.92%           
=======================================
  Files           6        6           
  Lines         152      152           
  Branches       33       33           
=======================================
  Hits          123      123           
  Misses         13       13           
  Partials       16       16

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e9e3702...bd876cf. Read the comment docs.

@danez danez merged commit 2fbcc01 into babel:master Mar 4, 2017
@danez
Copy link
Member

danez commented Mar 4, 2017

Thank you, I added the note about webpack 2.

danez added a commit that referenced this pull request Mar 6, 2017
* master:
  added metadata passing from babel to webpack (#398)
  document globalOptions (#365)
  Docs: change babel-preset-es2015 to babel-preset-env (#404) [skip ci]

# Conflicts:
#	README.md
#	package.json
#	src/index.js
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.

Documentation for globalOptions

2 participants