如何在javascript中使用npm脚本

时间:2022-10-26 21:34:53

I need a complete guide or a good reference material to solve the running module commands within javascript file problem.

我需要一个完整的指南或一个很好的参考资料来解决javascript文件问题中运行的模块命令。

Say that I often run:

说我经常跑:

$ npm run webpack-dev-server --progress --colors -- files

How can I run this within a javascript file and execute with

如何在javascript文件中运行并执行

$ node ./script.js

script.js

var webpackDevServer = require('webpack-dev-server');

// need help here
var result = webpackDevServer.execute({
  progress: true,
  colors: true,
}, files);

2 个解决方案

#1


8  

Answer

I do something like this for my Webpack bundles. You can simply use child_process.spawn to execute command-line programs and handle the process in a node script.

我为我的Webpack包做了类似的事情。您可以简单地使用child_process.spawn来执行命令行程序并在节点脚本中处理该过程。

Here's an example:

这是一个例子:

var spawn = require('child_process').spawn

// ...

// Notice how your arguments are in an array of strings
var child = spawn('./node_modules/.bin/webpack-dev-server', [
    '--progress',
    '--colors',
    '<YOUR ENTRY FILE>'
]);

child.stdout.on('data', function (data) {
    process.stdout.write(data);
});

child.stderr.on('data', function (data) {
    process.stdout.write(data);
});

child.on('exit', function (data) {
    process.stdout.write('I\'m done!');
});

You can handle all of the events you like. This is a fairly powerful module that allows you to view the process' PID (child.pid) and even kill the process whenever you choose (child.kill()).

您可以处理所有喜欢的事件。这是一个相当强大的模块,允许您查看进程'PID(child.pid),甚至可以在您选择时终止进程(child.kill())。


Addendum

A neat trick is to throw everything into a Promise. Here's a simplified example of what my version of script.js would look like:

一个巧妙的技巧是把所有东西都扔进Promise。这是我的script.js版本的简化示例:

module.exports = function () {
    return new Promise(function (resolve, reject) {
        var child = spawn('./node_modules/.bin/webpack', [
            '-d'
        ]);

        child.stdout.on('data', function (data) {
            process.stdout.write(data);
        });

        child.on('error', function (data) {
            reject('Webpack errored!');
        });

        child.on('exit', function () {
            resolve('Webpack completed successfully');
        });
    });
}

Using this method, you can include your script.js in other files and make this code synchronous in your build system or whatever. The possibilities are endless!

使用此方法,您可以将script.js包含在其他文件中,并使此代码在构建系统中同步。可能性是无止境!


Edit The child_process.exec also lets you execute command-line programs:

编辑child_process.exec还允许您执行命令行程序:

var exec = require('child_process').exec

// ...

var child = exec('webpack-dev-server --progress --colors <YOUR ENTRY FILES>',
  function(err, stdout, stderr) {
    if (err) throw err;
    else console.log(stdout);
});

#2


0  

Do you need it to be webpack-dev-server? There is an equivalent webpack-dev-middleware for running within node/express:

你需要它是webpack-dev-server吗?在node / express中运行有一个等效的webpack-dev-middleware:

'use strict';
let express = require('express');
let app = new express();

app.use(express.static(`${__dirname}/public`));

let webpackMiddleware = require('webpack-dev-middleware');
let webpack = require('webpack');
let webpackConfig = require('./webpack.config.js');
app.use(webpackMiddleware(webpack(webpackConfig), {}));

app.listen(3000, () => console.log(`Server running on port 3000...`));

https://github.com/webpack/webpack-dev-middleware

#1


8  

Answer

I do something like this for my Webpack bundles. You can simply use child_process.spawn to execute command-line programs and handle the process in a node script.

我为我的Webpack包做了类似的事情。您可以简单地使用child_process.spawn来执行命令行程序并在节点脚本中处理该过程。

Here's an example:

这是一个例子:

var spawn = require('child_process').spawn

// ...

// Notice how your arguments are in an array of strings
var child = spawn('./node_modules/.bin/webpack-dev-server', [
    '--progress',
    '--colors',
    '<YOUR ENTRY FILE>'
]);

child.stdout.on('data', function (data) {
    process.stdout.write(data);
});

child.stderr.on('data', function (data) {
    process.stdout.write(data);
});

child.on('exit', function (data) {
    process.stdout.write('I\'m done!');
});

You can handle all of the events you like. This is a fairly powerful module that allows you to view the process' PID (child.pid) and even kill the process whenever you choose (child.kill()).

您可以处理所有喜欢的事件。这是一个相当强大的模块,允许您查看进程'PID(child.pid),甚至可以在您选择时终止进程(child.kill())。


Addendum

A neat trick is to throw everything into a Promise. Here's a simplified example of what my version of script.js would look like:

一个巧妙的技巧是把所有东西都扔进Promise。这是我的script.js版本的简化示例:

module.exports = function () {
    return new Promise(function (resolve, reject) {
        var child = spawn('./node_modules/.bin/webpack', [
            '-d'
        ]);

        child.stdout.on('data', function (data) {
            process.stdout.write(data);
        });

        child.on('error', function (data) {
            reject('Webpack errored!');
        });

        child.on('exit', function () {
            resolve('Webpack completed successfully');
        });
    });
}

Using this method, you can include your script.js in other files and make this code synchronous in your build system or whatever. The possibilities are endless!

使用此方法,您可以将script.js包含在其他文件中,并使此代码在构建系统中同步。可能性是无止境!


Edit The child_process.exec also lets you execute command-line programs:

编辑child_process.exec还允许您执行命令行程序:

var exec = require('child_process').exec

// ...

var child = exec('webpack-dev-server --progress --colors <YOUR ENTRY FILES>',
  function(err, stdout, stderr) {
    if (err) throw err;
    else console.log(stdout);
});

#2


0  

Do you need it to be webpack-dev-server? There is an equivalent webpack-dev-middleware for running within node/express:

你需要它是webpack-dev-server吗?在node / express中运行有一个等效的webpack-dev-middleware:

'use strict';
let express = require('express');
let app = new express();

app.use(express.static(`${__dirname}/public`));

let webpackMiddleware = require('webpack-dev-middleware');
let webpack = require('webpack');
let webpackConfig = require('./webpack.config.js');
app.use(webpackMiddleware(webpack(webpackConfig), {}));

app.listen(3000, () => console.log(`Server running on port 3000...`));

https://github.com/webpack/webpack-dev-middleware

相关文章