javascript - How to pass Parameters from Web-pack to Code? - Stack Overflow

I have a gulp task which takes in the parameter location using yargs. And i use webpack to give the ent

I have a gulp task which takes in the parameter location using yargs. And i use webpack to give the entry point to my application

 webpackConfig.entry.push('BootStrapper.ts');

I have a webpack config which has entrypoint to bootstrapper.

module.exports = {
entry: [],
output: {
    path: require("path").resolve('./dist/'),
    filename: 'MyProcess.built.js'
 },
}

I want to pass a variable the location variable to Bootstrapper.ts is there a way to achieve it?

I have a gulp task which takes in the parameter location using yargs. And i use webpack to give the entry point to my application

 webpackConfig.entry.push('BootStrapper.ts');

I have a webpack config which has entrypoint to bootstrapper.

module.exports = {
entry: [],
output: {
    path: require("path").resolve('./dist/'),
    filename: 'MyProcess.built.js'
 },
}

I want to pass a variable the location variable to Bootstrapper.ts is there a way to achieve it?

Share Improve this question edited Jul 16, 2018 at 7:04 Vinny asked Jul 16, 2018 at 6:50 VinnyVinny 8652 gold badges12 silver badges27 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

Yes, there is.

First, adjust your webpack config to receive parameters:

{
  plugins: [
    new webpack.DefinePlugin({
      'process.env.YOUR_UNIQUE_VARIABLE': JSON.stringify(process.env.YOUR_UNIQUE_VARIABLE)
    )
  ]
}

then, in your Bootstrapper.ts you can write something like this:

const buildVariable = process.env.YOUR_UNIQUE_VARIABLE;

If you now run your webpack script (I assume to run a npm script) passing the variable like:

cross-env YOUR_UNIQUE_VARIABLE=value webpack

your code will be piled to:

const buildVariable = 'value';

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745348612a4623690.html

相关推荐

  • javascript - How to pass Parameters from Web-pack to Code? - Stack Overflow

    I have a gulp task which takes in the parameter location using yargs. And i use webpack to give the ent

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信