Concatenate files with npm as build tool

故事扮演 提交于 2019-12-03 06:01:20

try this

var concat = require('concat')    
concat(['a.css', 'b.css', 'c.css'], 'all.css')

https://www.npmjs.com/package/concat

and don't forget about npm install concat

By command use concat-glob-cli

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "concat": "concat-glob-cli -f path/to/**/*.js -o bundle.js",
    ...
 },

https://www.npmjs.com/package/concat-glob-cli

Yep, concat is gone. I was looking at this as well while moving away from gulp to pure node and found the package to be missing.

As an alternative I am now using buildify. Might be a slight overkill, but it works.

var buildify = require('buildify');
var files = [
    "./node_modules/moduleA/file1.js",
    "./node_modules/moduleB/file2.js",
];

buildify()
    .concat(files)
    .save("./dist/www/scripts/init.min.js");

I am using concat-files

And I noticed there's also concatenate-files

Both are pretty simple.

Also note writing your own is pretty simple too:

var output = files.map((f)=>{
  return fs.readFileSync(f).toString();
}).join(';')

fs.writeFileSync('dist/bundle.js', output)

The concat package is no longer available. I would suggest using concat-with-sourcemaps https://www.npmjs.com/package/concat-with-sourcemaps

var concat = new Concat(true, 'all.js', '\n');
concat.add(null, "// (c) John Doe");
concat.add('file1.js', file1Content);
concat.add('file2.js', file2Content, file2SourceMap);

var concatenatedContent = concat.content;
var sourceMapForContent = concat.sourceMap;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!