简体   繁体   English

Webpack angularjs 复制 angularjs 模板但不包含在 javascript 包中

[英]Webpack angularjs copy angularjs templates but don't include in javascript bundle

What I'm trying to do is configure webpack in a way that would copy all the partial html templates that are required in angular 1.x directives etc. into a separate folder (just like the file-loader does for assets referenced from css).我想要做的是配置 webpack,以将 angular 1.x 指令等中所需的所有部分 html 模板复制到一个单独的文件夹中(就像文件加载器对从 css 引用的资产所做的那样) . I don't want the partial htmls to be included in my javascript bundle but rather have all of them in one folder with hashed names.我不希望将部分 html 包含在我的 javascript 包中,而是将它们全部放在一个带有散列名称的文件夹中。

I tried configuring the file-loader to do it but when I run webpack in watch mode it also copies the those html in the dist folder again to it appending a new hash and essentially makes an infinite loop as every new html that shows up in that folder is copied, thus a new html appears and again and again.我尝试配置文件加载器来执行此操作,但是当我在监视模式下运行 webpack 时,它还会将 dist 文件夹中的那些 html 再次复制到它,并附加一个新的散列,并且本质上是一个无限循环,因为每个新的 html 出现在那个文件夹被复制,因此一个新的 html 一次又一次地出现。 I tried to set an exclude matching pattern for that loader but webpack was still running constantly watching over that folder.我尝试为该加载程序设置exclude匹配模式,但 webpack 仍在不断运行,监视该文件夹。

Here is the excerpt of my webpack.config.js file:这是我的webpack.config.js文件的摘录:

module: {
    loaders: [
        test: /\.html$/,
        exclude: /bundle\/templates/,
        loader: `file-loader?name=bundle/templates/[name]-[hash].[ext]
    ]
}

is there a loader that would help me do what I want without resorting to hacks?有没有一种装载机可以帮助我做我想做的事而无需求助于黑客?

Ok I have found the answer.好的,我找到了答案。 The problem wasn't the file-loader which is configured correctly like this even without the exclude .问题不在于即使没有exclude也像这样正确配置的file-loader What caused the infinite loop was a dynamic require that created a regex that matched those copied assets too... So by setting a proper context for this dynamic require the problem was resolved.导致无限循环的是一个动态需求,它创建了一个正则表达式来匹配那些复制的资产......因此,通过为这个动态需求设置适当的上下文,问题得到了解决。

In the webpack.config file you can use the same way like this in the loader section. 

     { 
        test: /\.html$/,
        exclude: path.resolve(__dirname, 'index.html'),
        loader: 'file-loader',
        options: {
            name: '[name].[ext]',
            outputPath: 'views/'
        }
    },

And in the plugins section of the webpack.config file在 webpack.config 文件的 plugins 部分

 plugins: [ 
    new HtmlWebpackPlugin({
        template: 'index.html',
        inject: true
    }),
    new ExtractTextPlugin('bundle.css')
]

This will copy all the htmls and will be refered from the index.html这将复制所有 html 并将从 index.html 中引用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM