Laravel Mix - 是否需要链接来确保执行顺序?

Laravel Mix - Is Chaining Required to Ensure Execution Order?

TLDR;

是否必须链接 Laravel 混合方法来维护执行顺序?是否有任何异步方法会阻止使用以下非链接模式,mix.scripts(); mix.js(); mix.sass();?

我进行的几次测试 运行 建议我不需要链接。

一个例子

由于我们的 Laravel 应用程序的设置方式,我们需要不止一个 Laravel 混合设置。我们不是复制粘贴 webpack.mix.js 文件并在每个文件中到处修改几行,而是着眼于创建一个传递给单个 webpack.mix.js 文件的配置对象。在此文件中,我们将检查是否已配置各种内容,如果已配置,运行 适当的 Mix 方法。下面是一个伪代码示例。

if ( config.js ) {
  mix.js( config.js.src, config.js.dist );
}

if ( config.sass ) {
  mix.sass( config.sass.src, config.sass.dist );
}

if ( config.concat ) {

  if ( config.concat.styles ) {

    // Could be more than one set of files that need to be combined, so array.
    config.concat.styles.map( ( files ) => {
      mix.styles( files.src, files.dist );
    }
  }

  if ( config.concat.scripts ) {

    // Could be more than one set of files that need to be combined, so array.
    config.concat.scripts.map( ( files ) => {
      mix.scripts( files.src, files.dist );
    }
  }

}

目前,我们的代码更像您在网络上看到的大多数示例。

mix
  .options()
  .webpackConfig()
  .styles()
  .styles()
  .scripts()
  .js()
  .sass();

The few tests I've run suggest I do not need to chain.

你完全正确!

Laravel Mix is written in JavaScript and makes use of Method Chaining.

您可以使用 OnlinePythonTutor 可视化代码执行。

如果您查看下面的代码,您会发现您不一定需要链接方法来维护执行顺序。

class Person {
  setName(name) {
    this.name = name
    return this
  }
  setAge(age) {
    this.age = age
    return this
  }
}

var p = new Person()
p.setName("Alice").setAge(42) // Set name before age
p.setName("Bob") // Set name
p.setAge(42) // Set age

你可以想象这段代码here

laravel-mix 抽象出webpack的配置,动态生成webpack config.

API implementation is done using Builder pattern with a fluent or chainable interface的组织。 这使得生成特定配置时只需调用必须执行的步骤。

您需要确保 webpack.mix.js 模块中的代码 can be properly imported

您需要注意自定义任务的顺序,例如 copy, copyDirectory, combine, version. In v5.0.0, custom tasks are run without any bearing on their asynchronous nature. However there is coming changes to see to it that they are run sequentially

其他API方法可以按任意顺序调用。