0

This is a file named myFile.js I execute with node:

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

This is in a foreach loop, value of 'component' changes in each loop.

And this is the build command in my package.json:

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

And this is my elements-webpack.config.js file:

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"
  }
};

What I want to do is send a parameter from myFile.js to the command in package.json so that I get it in the webpack config file. The parameter is the value of the 'component' variable.

I think it should look like this:

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

But then I dont know how to catch it in package.json.

1 Answer 1

1

But then I dont know how to catch it in package.json.

You don't need to do any special handling in package.json to pass arguments. If the invocation uses --, then everything after -- will be an argument added to the command.

Example package.json:

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

From the command line:

$ npm run build

> [email protected] build
> ls

package.json
$

Cool. Now if we want to pass the -a option to ls, we can do it but only after --.

So this doesn't work:

$ npm run build -a
> [email protected] build
> ls

package.json
$

But this works:

$ npm run build -- -a

> [email protected] build
> ls "-a"

.       ..      package.json
$
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.