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.

Laravel Mix doesn't seem to transpile ES6 when importing packages

See original GitHub issue
  • Laravel Mix Version: 1.0.7
  • Node Version: 8.1.3
  • NPM Version: 5.1.0
  • OS: macOS 10.12.6 Beta

Description:

Laravel Mix doesn’t seem to transpile ES6 when importing packages

Steps To Reproduce:

Installed laravel mix according to documentation for a standalone project.

created the standard directory structure (src/app.js, src/app.scss, created a folder dist) and added a custom file as myPackage.js.

src/app.js now contains the following:

import { initTabSwitcher } from './myPackage.js';

initTabSwitcher('.tabs a');

Running npm run dev and npm run production works fine now.

To the Problem

If i put myPackage.js into my own git repository (as I want to share the functions across projects), install it via npm ("myPackage": "git+ssh://git@myServer.com:myUser/myPackage.git") and import it this way:

import { initTabSwitcher } from 'myPackage';

initTabSwitcher('.tabs a');

I get this error upon running npm run production:

Unexpected token: name (matchesFn) [./~/myPackage/src/app.js:3,0][/app.js:97,6]

npm run dev runs absolutely fine, but does not minify anything.

I also tried installing uglifyJS by adding "uglify-js": "git+https://github.com/mishoo/UglifyJS2.git#harmony" into my devDependencies. It didn’t change anything.

Is Laravel Mix not applying transpilation to imported modules? Am I doing something wrong or is this a bug?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:10
  • Comments:15 (1 by maintainers)

github_iconTop GitHub Comments

39reactions
OwenMelbzcommented, Aug 14, 2017

For anybody else who wants a work around as @domstubbs said you can change the exclusions

Currently this is a fully working webpack.mix.js example that may be useful for people

const { mix } = require('laravel-mix');

mix.js('resources/assets/js/app.js', 'public/js')
   .sass('resources/assets/sass/app.scss', 'public/css');

if (mix.inProduction()) {

    mix.version();

    mix.webpackConfig({
        module: {
            rules: [{
                test: /\.js?$/,
                exclude: /(bower_components)/,
                use: [{
                    loader: 'babel-loader',
                    options: mix.config.babel()
                }]
            }]
        }
    });
}


4reactions
domstubbscommented, Jul 11, 2017

The default Babel rules exclude the node_modules directory.

I’ve posted a sample config override here which has sorted this for me. I’m still excluding node_modules in general, my only exception is the foundation-sites package, which currently requires transpiling.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Laravel Mix doesn't seem to transpile ES6 when importing ...
Agreed. I've got a situation where enabling mix.sourceMaps() breaks some dependencies code in Safari (in a non-production build only — though ...
Read more >
Laravel Mix not transpiling vendor.js to es5 - Stack Overflow
Laravel Mix does not seem to transpile vendor.js and manifest.js to ES5. It fails on iPhone Safari and IE 11.
Read more >
Laravel Mix & Es6 - WordPress.com
Laravel mix default config already comes with presets: ['env'] which covers all the babel-presets but some packages still use the deprecated ...
Read more >
Laracasts Forum
Laravel mix doesn't seem to combine classes into single module ... I installed required packages and config mix file, and run npm run...
Read more >
Laravel Mix and ES2015+ Javascript - Foster Made
Configure Babel to Include Polyfills ... Whether or not you need to import it into your entry JS file depends on which value...
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