【问题标题】:Get Laravel Mix to transpile JS from different path获取 Laravel Mix 以从不同路径转换 JS
【发布时间】:2019-02-11 21:14:45
【问题描述】:

我正在使用 Laravel Mix,它很好地简化了使用 WebPack 和 Babel 将高级 JS 功能转换为旧版浏览器的过程——除了! (我在这里要疯了)......它只对我有用,如果我把 JS 转译到他们推荐的目录中。

推荐的基本“webpack.mix.js”设置是mix.js(['resources/js/app.js'], 'public/js/app.js'); 其中第一个参数可以是单个文件或要转译的文件数组。

默认的resources/js/app.js需要几个标准模块,比如vue、lodash、axios。效果很好。然后在app.js 中,我想从vendor/xxxx/assets/js/xx.js 获取一些额外的资源——但我得到一个错误:Unknown plugin "transform-object-rest-spread" specified——这意味着它找不到绝对已安装的 babel 插件。这与路径有关。

如果我将 xx.js 复制到 resources/js 然后在 app.js 中要求它,没问题。

我使用mix.webpackConfig 添加模块路径vendor/xxxx/assets/js,我使用别名设置'xx$': path.resolve(__dirname,"vendor/xxxx/assets/js/xx.js",并尝试在.babelrc 和webpackConfig 选项对象中弄乱babel 选项。

最终 webpack 配置的一个版本如下 - 相信我,我玩过很多变体,我知道这不是一个正确的版本,但是在导入/需要/转译任何文件之外的任何文件都没有任何效果node_modules (vue) 或 /resources/js (app.js,也可能需要该目录中的其他文件)。任何想法都会很棒!

{ context: 'C:\\www\\Laravels\\lsbb-5-3\\laravel', entry: { '/mixed/js/es6': [ 'C:\\www\\Laravels\\lsbb-5-3\\laravel\\resources\\js\\app.js', 'C:\\www\\Laravels\\lsbb-5-3\\laravel\\vendor\\xxxx\\js\\xx.js' ] }, output: { path: 'C:\\www\\Laravels\\lsbb-5-3\\laravel\\public', filename: '[name].js', chunkFilename: '[name].js', publicPath: '/' }, module: { rules: [ { test: /\.html$/, loaders: [ 'html-loader' ] }, { test: /(\.(png|jpe?g|gif)$|^((?!font).)*\.svg$)/, loaders: [ { loader: 'file-loader', options: { name: [Function: name], publicPath: '/' } }, { loader: 'img-loader', options: { enabled: true, gifsicle: {}, mozjpeg: {}, optipng: {}, svgo: {} } } ] }, { test: /(\.(woff2?|ttf|eot|otf)$|font.*\.svg$)/, loader: 'file-loader', options: { name: [Function: name], publicPath: '/' } }, { test: /\.(cur|ani)$/, loader: 'file-loader', options: { name: '[name].[ext]?[hash]', publicPath: '/' } }, { test: /\.jsx?$/, exclude: /(node_modules|bower_components)/, use: [ { loader: 'babel-loader', options: { cacheDirectory: true, presets: [ [ 'env', { modules: false, targets: { browsers: [ '> 2%' ], uglify: true } } ] ], plugins: [ 'transform-object-rest-spread', [ 'transform-runtime', { polyfill: false, helpers: false } ], [ 'transform-object-rest-spread' ] ] } } ] }, { test: /\.css$/, loaders: [ 'style-loader', 'css-loader' ] }, { test: /\.s[ac]ss$/, exclude: [], loaders: [ 'style-loader', 'css-loader', 'sass-loader' ] }, { test: /\.less$/, exclude: [], loaders: [ 'style-loader', 'css-loader', 'less-loader' ] } ] }, plugins: [ FriendlyErrorsWebpackPlugin { compilationSuccessInfo: {}, onErrors: undefined, shouldClearConsole: true, formatters: [ [Function: format], [Function: format], [Function: format] ], transformers: [ [Function: transform], [Function: transform], [Function: transform] ] }, DefinePlugin { definitions: { 'process.env': { NODE_ENV: '"development"' } } }, LoaderOptionsPlugin { options: { minimize: false, options: { context: 'C:\\www\\Laravels\\lsbb-5-3\\laravel\\node_modules\\laravel-mix\\src\\builder', output: { path: './' } }, test: { test: [Function: test] } } }, ManifestPlugin {}, CustomTasksPlugin {}, BuildCallbackPlugin { callback: [Function] }, { options: { title: 'Laravel Mix', alwaysNotify: true, hint: undefined, contentImage: 'C:\\www\\Laravels\\lsbb-5-3\\laravel\\node_modules\\laravel-mix\\icons\\laravel.png' }, lastBuildSucceeded: false, isFirstBuild: true } ], stats: { hash: false, version: false, timings: false, children: false, errorDetails: false, chunks: false, modules: false, reasons: false, source: false, publicPath: false }, performance: { hints: false }, devtool: 'eval-source-map', devServer: { headers: { 'Access-Control-Allow-Origin': '*' }, contentBase: 'C:\\www\\Laravels\\lsbb-5-3\\laravel\\public', historyApiFallback: true, noInfo: true, compress: true, quiet: true }, resolve: { extensions: [ '*', '.js', '.jsx', '.vue' ], alias: { 'vue$': 'vue/dist/vue.common.js', 'xx$': 'vendor/xxxx/js/xx.js', modules: [ 'node_modules', 'vendor/xxxx/js' ] } }

【问题讨论】:

  • sweetalert import swal from 'sweetalert2'; 的示例,lodash.tonumber import toNumber from 'lodash.tonumber'; 的示例
  • 是的,但这些通常在node_modules 内 - 我对此没有任何问题。我的问题是“需要”该目录之外的东西。也许问题是使用“import”语法而不是“require”?
  • 只使用相对路径,../../somefile.js
  • 是的,我试过了——webpack 发现文件没问题,在它上面运行 babel 进行转译,但是 babel 找不到自己的插件:Unknown plugin "transform-object-rest-spread" specified——但是如果要转译的文件在 node_modules 中,babel DOES 找到那个插件 - 顺便说一下,它也在 node_modules 中......

标签: laravel webpack laravel-mix


【解决方案1】:

啊!经过几天几夜,我终于想通了!有问题的文件有点像我在几个项目中使用的库,所以我只是将目录符号链接到项目中。多年来,我一直在这样做,并且使用各种构建和项目工具都没有问题,所以我没有考虑过。

但事实证明,Babel(或 WebPack 中的 Babel)在尝试跟踪符号链接时会中断!

所以我不能说我找到了解决方案,但我找到了合适的解决方法。我构建过程的第一步只是将文件从源目录复制到项目目录中。这很恶心,但实际上对我的目的来说效果很好——就是在一个位置编辑库,这样我就不会得到不同的版本。所以我可以编辑源代码并构建和瞧!

如果你有这个问题并且想把它做对,应该有额外的插件来帮助 babel 解决这些路径问题,但是一旦我弄清楚问题是什么,我就不会被打扰。祝其他人好运!

【讨论】:

    【解决方案2】:

    Github 上找到的解决方案是将其添加到您的混音文件中:

    mix.webpackConfig({ resolve: { symlinks: false } })
    

    ?

    【讨论】:

      猜你喜欢
      • 2019-01-26
      • 2018-07-29
      • 2020-07-03
      • 2017-10-04
      • 2022-01-25
      • 2017-09-07
      • 1970-01-01
      • 2014-10-05
      • 2022-08-22
      相关资源
      最近更新 更多