简体   繁体   English

使用phpStorm将js文件合并为一个

[英]Merge js files into one with phpStorm

Using phpStorm, I would like to merge multiple JavaScript files into one. 使用phpStorm,我想将多个JavaScript文件合并为一个。 I installed the closure compiler and configured the file watcher to minify each JavaScript file. 我安装了闭包编译器并配置了文件观察器来缩小每个JavaScript文件。

Now, I would like to combine all of my JavaScript files into one. 现在,我想将所有JavaScript文件合并为一个。

Here's the architecture of my project (a test project to merge js files) : 这是我的项目的架构(合并js文件的测试项目):

index.php
js(folder) >
    first.js (+first.min.js),
    second.js (+second.min.js),
    third.js (+third.min.js)
cache (folder)
    main.js

I would like to merge (first.min.js, second.min.js, third.min.js) into folder cache > main.js . 我想合并(first.min.js,second.min.js,third.min.js)到文件夹cache > main.js

Ideally, merging all of the files would happen automatically; 理想情况下,合并所有文件会自动发生; I don't want to specify each js file manually. 我不想手动指定每个js文件。

Can someone explain the arguments I must use to configure my filewatcher? 有人可以解释我必须用来配置我的filewatcher的参数吗?

I used npm plugins concat , minifier and walk . 我使用了npm插件concatminifierwalk

Here is the script I made : 这是我制作的剧本:

var walk = require('walk'),
    concat = require('concat'),
    minifier = require('minifier'),
    files = [];

var JS_SOURCES_DIR = 'app/components',
    JS_LAST_FILE = 'app/app.module.js',
    JS_DIR = 'app/',
    JS_FULL_FILE = JS_DIR + 'app.js',
    JS_MINIFIED_FILE = 'app.min.js',
    JS_MINIFIED_FILE_PATH = JS_DIR + JS_MINIFIED_FILE;

var walker = walk.walk(JS_SOURCES_DIR, {followLinks: false});

walker.on('file', (root, stat, next) => {
    var fullpath = root.replace(/\\/g, '/');
    var regex = new RegExp(/.+\.js$/);
    if (stat.name.match(regex)) {
        files.push(fullpath + '/' + stat.name);
    }
    next();
});

walker.on('end', function () {
    files.push(JS_LAST_FILE);
    files.forEach(function (item) {
        console.log(item);
    })
    concat(files, JS_FULL_FILE).then((result) => {
        minifier.minify(JS_FULL_FILE, {output: JS_MINIFIED_FILE_PATH});
        console.log('\n[OK] ' + JS_MINIFIED_FILE + ' sucessfully updated');
    }, function (error) {
        console.log('[ERROR] JS concat failure: ' + error.message);
    });
});

minifier.on('error', function (error) {
    console.log('\n[ERROR] JS minify error: ' + error);
});

First with walker, files are added to var "files". 首先使用walker,将文件添加到var“files”中。 I used JS_LAST_FILE for angularjs concerns, as I build the module and add all the dependencies in that file. 我使用JS_LAST_FILE来处理angularjs问题,因为我构建了模块并在该文件中添加了所有依赖项。 Then files are concatenated to JS_FULL_FILE. 然后将文件连接到JS_FULL_FILE。 Finally JS_FULL_FILE is minified to JS_MINIFIED_FILE. 最后,JS_FULL_FILE被缩小为JS_MINIFIED_FILE。

I do not use a watcher to trigger the concat script when a file is updated. 更新文件时,我不使用观察程序来触发concat脚本。 Instead when I work locally, I don't concatenate files but I simply add them in the head part of the page using a homemade function that uses php scandir(). 相反,当我在本地工作时,我不会连接文件,但我只是使用一个使用php scandir()的自制函数将它们添加到页面的head

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

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