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

browserify field flags to switch behavior based on dev/prod/... #1706

Closed
serapath opened this issue Mar 29, 2017 · 5 comments
Closed

browserify field flags to switch behavior based on dev/prod/... #1706

serapath opened this issue Mar 29, 2017 · 5 comments

Comments

@serapath
Copy link
Contributor

Browserify has the awesome feature, that every dependency can specify it's own transforms in a "browserify" field in it's package.json 👍

My issue:

  • While I'm developing I run watchify and don't need minification or extensive testing or transpilation of modern es6 code and beyond and I want my pipelin to be fast.
  • When I'm building for production, I want minification and transpilation es6toes5 for legacy browser support and a separate source maps file and so on...

Idea:
Is it or would it be possible to switch the behavior of a module's "browserify pipeline" as defined in it's "browserify field" based on environment variables or alternatively based on spec?

I would be willing to try to come up with an implementation and a pull request - but i first want to make sure if this would be acceptable and if so - what would be the best way :-)

@MellowMelon
Copy link
Collaborator

To ignore the transform/browserify fields of other package.json files, pass transformKey: false when constructing the browserify object.

This was very recently added and apparently not documented. Check the last commit that changed this repository's index.js. That commit also has a test you can look at.

@serapath
Copy link
Contributor Author

serapath commented Apr 1, 2017

I don't want to ignore the transform of browserify fields of other package.json files. It's awesome and i like it.

I just would like to customize the transforms listed in each projects package.json based on environment variables, e.g. production, development, staging, ...

For example, in "production" i want to do minification or polyfilling or a lot of more things that i might need in order to support old browsers and improve performance, etc...

In "development" i dont care to minify or to polyfill, because i'm developing on a modern browser. So i'd like to skip a lot of transforms to speed up the build process, because i want to live-reload on every file save and that should be fast.

babel for example seems to support something like env options https://babeljs.io/docs/usage/babelrc/

Otherwise, I am just wondering if such a feature would fit with browserify and if not if maybe things can be done differently or in other places, so that there is no need for those flags.

Sorry, scratch the above - didn't get your answer the first time - please see below

@serapath
Copy link
Contributor Author

serapath commented Apr 4, 2017

Oh great. I re-read what you were saying, so basically a package.json could look like:

{


  "browserify": {
    "transform": [
      ["babelify", {
        "presets": ["es2015"]
      }]
    ],
    "production": [
      ["babelify", {
        "plugins": [ "yo-yoify" ],
        "presets": ["es2015"]
      }],
      ["uglifyify"]
    ]
  },
  "scripts": {
    "start": "browserify index.js > bundle.js",
    "build": "browserify --transformKey=browserify --transformKey=production index.js > bundle.js"
  }


}

I'll try that.

Update2:
Ok, it's not working from the command line, so I'll make a pull request to add the feature.

I also think it's kind of crazy to add --transformKey twice to get the correct array, so i'll assume, somebody who uses the flag from the command line will always nest his custom keys under "browserify", so the following package.json would work...

{


  "browserify": {
    "transform": [
      ["babelify", {
        "presets": ["es2015"]
      }]
    ],
    "production": [
      ["babelify", {
        "plugins": [ "yo-yoify" ],
        "presets": ["es2015"]
      }],
      ["uglifyify"]
    ]
  },
  "scripts": {
    "start": "browserify index.js > bundle.js",
    "dev": "browserify --transformKey=transform index.js > bundle.js # same as `npm start`",
    "build": "browserify --transformKey=production index.js > bundle.js"
  }


}

If this is cool, I'd like to make a pull request :-)

@serapath
Copy link
Contributor Author

serapath commented Apr 4, 2017

Here you go #1709 - changed the proposal from above slightly to use:

  • browserify index.js --transform-key=production > bundle.js
    This is probably better than camelcase...

Maybe it would be even better to name it --mode or short -m instead, because --transform-key is kind of long.

@serapath
Copy link
Contributor Author

serapath commented Apr 5, 2017

ok got merged. perfect :-)

@serapath serapath closed this as completed Apr 5, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants