简体   繁体   English

如何从 laravel mix 迁移到纯 Webpack?

[英]How to migrate from laravel mix to pure Webpack?

Given the webpack.mix.js of a fresh Laravel project:鉴于新webpack.mix.js项目的 webpack.mix.js :

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

/*
 |--------------------------------------------------------------------------
 | Mix Asset Management
 |--------------------------------------------------------------------------
 |
 | Mix provides a clean, fluent API for defining some Webpack build steps
 | for your Laravel application. By default, we are compiling the Sass
 | file for the application as well as bundling up all the JS files.
 |
 */

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

What is the equivalent using just webpack and a webpack.config.js ?仅使用 webpack 和webpack.config.js的等价物是什么? (Im looking to remove laravel mix as a dependency on an existing project.) (我希望删除 laravel mix 作为对现有项目的依赖。)

I did find this default file in the source but it did not help me.我确实在源代码中找到了这个默认文件,但它对我没有帮助。 Is there a way I can see the "compiled/resulting" webpack configuration or a template/starting point that corresponds to laravel mix default settings?有没有办法可以看到“编译/生成”webpack 配置或对应于 laravel 混合默认设置的模板/起点?

You can, but the result is not very satisfactory.可以,但结果不是很满意。

Create a JS script with this:用这个创建一个 JS 脚本:

console.log (JSON.stringify(
    require('./node_modules/laravel-mix/setup/webpack.config.js'), null, 4)
);

and save it in the root folder of your laravel project.并将其保存在 laravel 项目的根文件夹中。 Run it with Node and the output will be the configuration object Laravel Mix receives and inputs to webpack.使用 Node 运行它,output 将是配置 object Laravel 混合接收和输入到 Z424516CA53B4AD4BEF837ED04

However, this file is very long and covers a vast amount of settings, which you wouldn't need if you made your file from scratch.但是,此文件非常长,并且包含大量设置,如果您从头开始制作文件,则不需要这些设置。 Yes, you could try and remove every extra setting you think you can remove without breaking your output, but in the end it's better to learn how Webpack works so you can write better, mode adjusted configs.是的,您可以尝试删除您认为可以在不破坏 output 的情况下删除的所有额外设置,但最后最好了解 Webpack 的工作原理,这样您就可以编写更好的模式调整配置。 At least you can use it to understand how it does certain things.至少你可以用它来理解它是如何做某些事情的。

Just put into webpack.mix.js只需放入 webpack.mix.js

Mix.listen('configReady', function (config) {
  RegExp.prototype.toJSON = RegExp.prototype.toString;
  console.log(JSON.stringify(config));
});

So you will get webpack config from your laravel.mix.因此,您将从 laravel.mix 中获得 webpack 配置。

The file you referenced seems to point exactly to the default configuration.您引用的文件似乎完全指向默认配置。 Why did this not help?为什么这没有帮助?

In order to migrate you could为了迁移你可以

  1. Learn the basics学习基础知识
  2. Extract the dependencies from Laravel mix aǹd add them to your package.json从 Laravel 中提取依赖项并将它们混合并添加到您的 package.json

    • Hint: The dependencies there are your devDependencies提示:那里的dependencies项是你的devDependencies
    • Start by installing npm install --save-dev everything "webpack", "babel" and prefixed with "-loader".首先安装npm install --save-dev一切“webpack”,“babel”并以“-loader”为前缀。
    • If you need Sass and extracted css - npm install --save-dev node-sass sass-loader mini-css-extract-plugin .如果您需要 Sass 并提取 css - npm install --save-dev node-sass sass-loader mini-css-extract-plugin
  3. Minimal example of a webpack config for your mix example from above would be上面的混合示例的 webpack 配置的最小示例是
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

module.exports = {
  entry: './resources/js/app.js',
  output: {
    filename: 'js/[name].js',
    path: path.join(__dirname, 'public')
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: 'css/[name].css'
    })
  ],
  module: {
    rules: [
      {
        test: /\.(sa|sc|c)ss$/,
        use: [
          {
            loader: MiniCssExtractPlugin.loader,
          },
          'css-loader',
          'sass-loader'
        ]
      }
    ]
  }
};
  1. Learn the more advanced basics for your use case为您的用例了解更高级的基础知识

With recent laravel-mix you just need to invoke mix.dump() (in the webpack.mix.js script).使用最近的 laravel-mix,您只需要调用mix.dump() (在webpack.mix.js脚本中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM