【问题标题】:Sending a parameter from node file to package.json script command将参数从节点文件发送到 package.json 脚本命令
【发布时间】:2021-02-23 10:09:11
【问题描述】:

这是我用节点执行的名为 myFile.js 的文件:

var aa = `npm run build -- --main=src/` + (component === `widget` ? `thisPath/` : ``) + `${component}/myFile.ts`;
execSync(`${aa} `);

这是在一个foreach循环中,'component'的值在每个循环中都会发生变化。

这是我的 package.json 中的构建命令:

"build": "ng build --aot --outputHashing=\"all\" --sourceMap=false --vendorChunk=false --extra-webpack-config elements-webpack.config.js --single-bundle"

这是我的 elements-webpack.config.js 文件:

const path = require('path');
const uuidv1 = require('uuid/v1');

console.log(process.argv);
var pathData = process.argv[10];

module.exports = {
  output: {
    filename: pathData === 'main' ? '[name].[contenthash].js' : '[name].[contenthash].js',
    jsonpFunction: 'myElements-' + uuidv1(),
    library: 'elements'
  },
  externals: {
    "rxjs": "rxjs",
    "@angular/core": "ng.core",
    "@angular/common": "ng.common",
    "@angular/common/http": "ng.common.http",
    "@angular/platform-browser": "ng.platformBrowser",
    "@angular/platform-browser-dynamic": "ng.platformBrowserDynamic",
    "@angular/compiler": "ng.compiler",
    "@angular/elements": "ng.elements",
    "@angular/forms": "ng.forms",
    "@angular/router": "ng.router"
  }
};

我想要做的是将参数从 myFile.js 发送到 package.json 中的命令,以便我在 webpack 配置文件中获取它。参数是“组件”变量的值。

我认为应该是这样的:

var aa = `npm run build -- --main=src/` + (component === `widget` ? `thisPath/` : ``) + `${component}/myFile.ts` + ` ${component}`;

但后来我不知道如何在 package.json 中捕获它。

【问题讨论】:

    标签: npm package.json npm-scripts


    【解决方案1】:

    但是我不知道如何在 package.json 中捕获它。

    您无需在package.json 中进行任何特殊处理即可传递参数。如果调用使用--,那么-- 之后的所有内容都将是添加到命令的参数。

    示例 package.json:

    {
      "name": "temp",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "build": "ls"
      },
      "author": "",
      "license": "MIT"
    }
    

    从命令行:

    $ npm run build
    
    > temp@1.0.0 build
    > ls
    
    package.json
    $
    

    酷。现在,如果我们想将-a 选项传递给ls,我们可以这样做,但只能在-- 之后。

    所以这不起作用:

    $ npm run build -a
    > temp@1.0.0 build
    > ls
    
    package.json
    $
    

    但这有效:

    $ npm run build -- -a
    
    > temp@1.0.0 build
    > ls "-a"
    
    .       ..      package.json
    $
    

    【讨论】:

      猜你喜欢
      • 2012-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-09
      相关资源
      最近更新 更多