question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

node_modules using new node.js features won't bundle/uglify properly

See original GitHub issue

Description

(Below is more or less a copy of my comment from babel/babel-brunch#27 I’m posting a proper issue here, because seeing as that issue was closed before I commented, it may fall upon deaf ears (for being unnoticed).)

I’m bundling a dependency, credit-card for the browser which relies upon certain JS features that need to be transpiled (by something, anything) in order for it to work in all the browsers I’m trying to support. It uses things like const and Object.assign, which I use babel to polyfill.

For instance, credit-card has one dependency, reach that uses template strings.

These features generally are ones that are enabled by default in the recent versions of node.js, but obviously do not work in all browsers.

This is a growing trend with npm module authors because bundlers such as browserify and webpack will process these types of modules through their transform pipelines by default.

These dependencies do not come with dist versions.

Lots of packages are written in ES6 with the idea of modularity in mind, such as RxJS 5. It makes it (theoretically) possible (with bundlers such as rollup) to only import the functions one needs so as not to pull in the entire package and bloat the compiled code.

To work around these problem cases, I’ve had to internalize these modules into my project, which is a pain, because I use flow in all of my project’s modules and rely heavily on the automatic type inference it provides for stuff that’s imported from node_modules. However, if one imports a local module (not in node_modules), the type inference is not so automatic and requires you to provide some type annotations – which is what I have to do to these internalized-workaround modules.

Enabling brunch (or babel-brunch as the case may be) to process, perhaps a specific set of modules contained within node_modules through the compilation steps, would eliminate many headaches.

What happens

  • When I run brunch build, the build succeeds.
  • When I run brunch build --production, the build fails.
  • In either case, the resulting JS does not work on all browsers.

Environment

  1. Brunch: 2.6.6
  2. Node: v5.10.1
  3. NPM: 3.8.6
  4. Operating system: Arch Linux 4.4.5-1-ARCH x86_64

package.json contents

{
  "name": "brunch-test-local-imports",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "build": "brunch build",
    "prod": "brunch build --production"
  },
  "dependencies": {
    "credit-card": "^2.0.0"
  },
  "devDependencies": {
    "babel-brunch": "^6.0.2",
    "babel-preset-es2015": "^6.6.0",
    "brunch": "^2.6.6",
    "javascript-brunch": "^2.0.0",
    "uglify-js-brunch": "^2.0.1"
  }
}

brunch config contents

module.exports = {
    files: {
        javascripts: {
            joinTo: {
                'vendor.js': /^(?!app)/,
                'app.js': /^app/
            }
        },
        stylesheets: { joinTo: 'app.css' }
    },
    modules: {
        autoRequire: { 'app.js': ['main'] }
    },
    plugins: {
        babel: Object.assign({}, require('./babelrc.json'), {

        })
    }
};

(NOTE: .babelrc is a symlink to babelrc.json so that I can import it into the config.

Other useful files, when present (bower.json etc.)

.babelrc

{
    "presets": ["es2015"]
}

Output of brunch build --production -d

  brunch:config Trying to load brunch-config +0ms
  brunch:plugins Loaded plugins: babel-brunch, javascript-brunch, uglify-js-brunch +256ms
  brunch:watch add package.json +11ms
  brunch:watch add brunch-config.js +1ms
  brunch:watch add app/main.js +3ms
  brunch:list Reading app/main.js +0ms
  brunch:file Init app/main.js: isntModule=false isWrapped=true +1ms
  brunch:pipeline Compiling app/main.js @ BabelCompiler +2ms
  brunch:pipeline Compiling app/main.js @ JavaScriptCompiler +93ms
  brunch:watch add app/assets/index.html +14ms
  brunch:asset Init app/assets/index.html directory=app/assets/ destPath=public/index.html rel=index.html +0ms
  brunch:asset Copied app/assets/index.html +5ms
  brunch:list Reading node_modules/credit-card/lib/index.js +4ms
  brunch:modules Wrapping main.js @ commonjs +0ms
  brunch:list Compiled app/main.js +1ms
  brunch:file Init node_modules/credit-card/lib/index.js: isntModule=true isWrapped=true +1ms
  brunch:list Reading node_modules/reach/lib/index.js +15ms
  brunch:modules Not wrapping (is vendor file) node_modules/credit-card/lib/index.js +0ms
  brunch:list Compiled node_modules/credit-card/lib/index.js +2ms
  brunch:file Init node_modules/reach/lib/index.js: isntModule=true isWrapped=true +0ms
  brunch:modules Not wrapping (is vendor file) node_modules/reach/lib/index.js +4ms
  brunch:list Compiled node_modules/reach/lib/index.js +0ms
  brunch:write Writing 2/2 files +67ms
  brunch:generate Concatenating [app/main.js] => public/app.js +1ms
  brunch:generate Concatenating [node_modules/credit-card/lib/index.js, node_modules/reach/lib/index.js] => public/vendor.js +2ms
  brunch:generate Optimizing public/app.js @ UglifyJSOptimizer +4ms
  brunch:generate Optimizing public/vendor.js @ UglifyJSOptimizer +62ms
  brunch:generate Writing public/app.js +7ms
18 Apr 20:40:02 - error: JS minification failed on public/vendor.js: Unexpected token: name (_defaults) (line: 156, col: 4, pos: 3951)

Error
  at new JS_Parse_Error (<anonymous>:1526:18)
  at js_error (<anonymous>:1534:11)
  at croak (<anonymous>:2026:9)
  at token_error (<anonymous>:2034:9)
  at unexpected (<anonymous>:2040:9)
  at semicolon (<anonymous>:2060:56)
  at simple_statement (<anonymous>:2240:73)
  at eval (<anonymous>:2113:19)
  at eval (<anonymous>:2073:24)
  at block_ (<anonymous>:2353:20)
  at eval (<anonymous>:2326:25)
  at function_ (<anonymous>:2331:15)
  at expr_atom (<anonymous>:2546:24)
  at maybe_unary (<anonymous>:2716:19)
  at expr_ops (<anonymous>:2751:24)
  at maybe_conditional (<anonymous>:2756:20)
  at maybe_assign (<anonymous>:2780:20)
  at expression (<anonymous>:2799:20)
  at expr_atom (<anonymous>:2532:26)
  at maybe_unary (<anonymous>:2716:19)
  at expr_ops (<anonymous>:2751:24)
  at maybe_conditional (<anonymous>:2756:20)
  at maybe_assign (<anonymous>:2780:20)
  at expression (<anonymous>:2799:20)
  at simple_statement (<anonymous>:2240:55)
  at eval (<anonymous>:2125:24)
  at eval (<anonymous>:2073:24)
  at block_ (<anonymous>:2353:20)
  at eval (<anonymous>:2326:25)
  at function_ (<anonymous>:2331:15)
  at expr_atom (<anonymous>:2546:24)
  at maybe_unary (<anonymous>:2716:19)
  at expr_ops (<anonymous>:2751:24)
  at maybe_conditional (<anonymous>:2756:20)
  at maybe_assign (<anonymous>:2780:20)
  at expression (<anonymous>:2799:20)
  at expr_list (<anonymous>:2565:24)
  at subscripts (<anonymous>:2699:30)
  at subscripts (<anonymous>:2676:20)
  at expr_atom (<anonymous>:2552:20)
  at maybe_unary (<anonymous>:2716:19)
  at expr_ops (<anonymous>:2751:24)
  at maybe_conditional (<anonymous>:2756:20)
  at maybe_assign (<anonymous>:2780:20)
  at expression (<anonymous>:2799:20)
  at simple_statement (<anonymous>:2240:55)
  at eval (<anonymous>:2113:19)
  at eval (<anonymous>:2073:24)
  at block_ (<anonymous>:2353:20)
  at eval (<anonymous>:2326:25)
  at function_ (<anonymous>:2331:15)
  at expr_atom (<anonymous>:2546:24)
  at maybe_unary (<anonymous>:2716:19)
  at expr_ops (<anonymous>:2751:24)
  at maybe_conditional (<anonymous>:2756:20)
  at maybe_assign (<anonymous>:2780:20)
  at expression (<anonymous>:2799:20)
  at expr_atom (<anonymous>:2532:26)
  at maybe_unary (<anonymous>:2716:19)
  at expr_ops (<anonymous>:2751:24)
  at maybe_conditional (<anonymous>:2756:20)
  at maybe_assign (<anonymous>:2780:20)
  at expression (<anonymous>:2799:20)
  at simple_statement (<anonymous>:2240:55)
  at eval (<anonymous>:2125:24)
  at eval (<anonymous>:2073:24)
  at eval (<anonymous>:2827:23)
  at Object.parse (<anonymous>:2837:7)
  at /home/dave/src/babel-brunch-es6-modules-test/node_modules/uglify-js/tools/node.js:64:33
  at Array.forEach (native)
  at Object.exports.minify (/home/dave/src/babel-brunch-es6-modules-test/node_modules/uglify-js/tools/node.js:59:15)
  at UglifyJSOptimizer.optimize (/home/dave/src/babel-brunch-es6-modules-test/node_modules/uglify-js-brunch/index.js:40:26)
  at Object.work (/home/dave/src/babel-brunch-es6-modules-test/node_modules/brunch/lib/fs_utils/generate.js:144:8)
  at exports.processJob (/home/dave/src/babel-brunch-es6-modules-test/node_modules/brunch/lib/workers/index.js:20:16)
  at Object.keys.filter.files.forEach.processJob.then (/home/dave/src/babel-brunch-es6-modules-test/node_modules/brunch/lib/fs_utils/generate.js:167:10)

Demo App

https://github.com/xdave/babel-brunch-es6-modules-test

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:19 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
goshacmdcommented, Apr 25, 2016

https://github.com/brunch/brunch/pull/1318 (and related PRs) should allow to run node_modules/ through babel now. Other js compilers are also fine as long as the source has .js extension.

0reactions
cxw42commented, Sep 21, 2018

Note for others who find this issue: per the changelog, as of 2.8, to run babel-brunch on node_modules, include

npm: {
    compilers: ['babel-brunch'],
},

in your Brunch config.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to correctly build NestJS app for production with ...
node_modules is not bundled, no. This should be possible with webpack though. I assume you want to remove the source code and only...
Read more >
How to Bundle JavaScript With Rollup — Step-by-Step Tutorial
Author your app or library using ES2015 modules, then efficiently bundle them up into a single file for use in browsers and Node.js....
Read more >
uglify-js - npm
UglifyJS is a JavaScript parser, minifier, compressor and beautifier toolkit. Note: uglify-js supports JavaScript and most language features in ...
Read more >
Improve your client-side-javascript workflow more by using ES6
In a previous article we presented a simple workflow to improve your client-side (javascript) workflow by including a bunch of tools from the ......
Read more >
How To Use Node.js Modules with npm and package.json
As you create more complex Node.js projects, managing your metadata and dependencies with the package.json file will provide you with more ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found