最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how webpack replace a string in .ts file? - Stack Overflow

programmeradmin0浏览0评论

I have a angular 2 project originally used to bundle works with systemjs.

now i want use webpack to bundle this project.

But the original code has a lot of such a string. such as

I want to replace all the strings with the absolute directory where the files are located.The original time, prod mode or dev mode, this string will be replaced with a path string.

Now I would like to use a webpack to replace this string before a .ts file is piled. What kind of plugin should I use?

for example: login.module.routing.ts

{ path: 'login', /app/src/login#LoginModule },

I want replace before use webpack pile.

such as : { path: 'login', /root/myproject/app/src/login#LoginModule },

thanks very much !

I have a angular 2 project originally used to bundle works with systemjs.

now i want use webpack to bundle this project.

But the original code has a lot of such a string. such as

I want to replace all the strings with the absolute directory where the files are located.The original time, prod mode or dev mode, this string will be replaced with a path string.

Now I would like to use a webpack to replace this string before a .ts file is piled. What kind of plugin should I use?

for example: login.module.routing.ts

{ path: 'login', /app/src/login#LoginModule },

I want replace before use webpack pile.

such as : { path: 'login', /root/myproject/app/src/login#LoginModule },

thanks very much !

Share Improve this question edited Jun 3, 2017 at 23:37 Jeff Puckett 41.1k19 gold badges124 silver badges174 bronze badges asked May 28, 2017 at 8:45 xuefeng Yangxuefeng Yang 212 silver badges8 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

For this requirement you can use string-replace-webpack-plugin.

Usage Example:

var StringReplacePlugin = require("string-replace-webpack-plugin");
module.exports = {
   module: {
      loaders: [
         // configure replacements for file patterns 
         { 
            test: /index.html$/,
            loader: StringReplacePlugin.replace({
                replacements: [
                    {
                        pattern: /<!-- @secret (\w*?) -->/ig,
                        replacement: function (match, p1, offset, string) {
                            return secrets.web[p1];
                        }
                    }
                ]})
            }
      ]
   },
   plugins: [
      // an instance of the plugin must be present 
      new StringReplacePlugin()
   ]
}

I hope this helps to solve your problem.

发布评论

评论列表(0)

  1. 暂无评论