webpack 编译文件夹中的所有文件

Nie*_*eck 5 javascript webpack laravel-5.4

所以我使用 Laravel 5.4,我使用 webpack 在 1 个大 js 文件中编译多个 .js 文件。

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

// Compile all CSS file from the theme
mix.styles([
   'resources/assets/theme/css/bootstrap.min.css',
   'resources/assets/theme/css/main.css',
   'resources/assets/theme/css/plugins.css',
   'resources/assets/theme/css/themes.css',
   'resources/assets/theme/css/themes/emerald.css',
   'resources/assets/theme/css/font-awesome.min.css',
], 'public/css/theme.css');



// Compile all JS file from the theme
mix.scripts([
   'resources/assets/theme/js/bootstrap.min.js',
   'resources/assets/theme/js/app.js',
   'resources/assets/theme/js/modernizr.js',
   'resources/assets/theme/js/plugins.js',
], 'public/js/theme.js');
Run Code Online (Sandbox Code Playgroud)

这是我webpack.mix.js要做的(CSS 也是如此)。但我想得到类似的东西:resources/assets/theme/js/*从文件夹中获取所有文件。所以当我在webpack自动找到的文件夹中新建一个js文件,运行命令时编译。

有人知道怎么做吗?

谢谢你的帮助。

ref*_*ity 7

如果有人希望代码将目录中的所有 sass/less/js 文件编译到具有相同文件名的不同目录,您可以使用以下命令:

// webpack.mix.js

let fs = require('fs');

let getFiles = function (dir) {
    // get all 'files' in this directory
    // filter directories
    return fs.readdirSync(dir).filter(file => {
        return fs.statSync(`${dir}/${file}`).isFile();
    });
};

getFiles('directory').forEach(function (filepath) {
    mix.js('directory/' + filepath, 'js');
});
Run Code Online (Sandbox Code Playgroud)


Asu*_*sur 6

mix.scripts()正如创建者在此问题中所确认的那样,实际上允许使用该方法使用通配符。所以你的电话应该是这样的:

mix.scripts(
   'resources/assets/theme/js/*.js',
   'public/js/theme.js');
Run Code Online (Sandbox Code Playgroud)

我认为它对样式的作用相同,因为它们使用相同的方法来组合文件。

希望这对你有帮助。