Laravel mix transpile dependency

2019-07-31 10:40发布

I'm trying to use the vuex-module-decorators library in a laravel mix project (using typescript). But I keep getting the error Uncaught TypeError: Class constructor VuexModule cannot be invoked without 'new'. This seems to be a known issue and can be resolved by adding transpileDependencies: ['vuex-module-decorators'] to my vue.config.js file which will tell babel to transpile the package.

Since I'm using laravel mix, adding a vue.config.js file doesn't do anything. And I can't figure out how to tell laravel mix to transpile the vuex-module-decorators dependency.

I tried adding { test: /\.js$/, loaders: ['babel-loader'] }, to the webpack config in webpack.mix.js (also with explicitly including the dependency) but it doesn't work.

So how can I tell laravel mix to transpile the vuex-module-decorators dependency (to es5)?

If it helps, here's my webpack.mix.js file:

mix
  .ts('resources/ts/app.ts', 'public/js')
  .stylus('resources/stylus/app.styl', 'public/css');

1条回答
Viruses.
2楼-- · 2019-07-31 11:14

I was able to point babel-loader at all .js files and that worked:

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

However your compile times will be reduced if use a more specific test to target only the modules you need to transpile:

    test: /node_modules\/(vuex-module-decorators|vuex-persist)\/.+\.js$/,
查看更多
登录 后发表回答