私はWebpack-dev-serverを使ってウェブサイトのフロントエンドを開発しました。WebpackのフォントとテンプレートがpublicPathを尊重しない - ローダーに何が問題なの?
今、私の生産サーバの/static
フォルダからwebpack bundleを提供したいと思います。
output.publicPath = "static/"
マイJavaScriptとCSSのバンドルは今適切にそれぞれ/static/app.<hash>.js
と/static/app.<hash>.css
の場所から提供されている(そう、HtmlWebpackPlugin
とExtractTextWebpackPlugin
は罰金答え):次のようにその終わりのために私はWebPACKの構成を変更しました。
しかし、htmlのテンプレートとフォントは奇妙な動作をして、publicPath
の設定を無視または誤解しているようです。
Htmlテンプレートは、static/
という接頭辞なしで参照されます。 https://example.com/components/header/header.html
。
フォントは、プレフィックスDOUBLEで参照されます。 https://example.com/static/static/fontawesome-webfont-db812d8.woff2
。
物理マシン上のこれらのアセットの場所については、CommonJSモジュールとしてJavaScriptバンドル(app.<hash>.js
)のインラインhtmlテンプレートを使用しています。私は自分のサイトの/static
フォルダに別々のファイルとしてフォントを入れ、正しいURLの下でも利用可能ですhttps://example.com/static/fontawesome-webfont-db812d8.woff2
。
がここにあります私の生産のためのwebpack.config.js
いっぱい:
const webpack = require('webpack');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const environmentsFile = path.join(__dirname, "/environments.json")
const nodeModulesPath = path.join(__dirname, "/node_modules");
const bowerComponentsPath = path.join(__dirname, "/bower_components")
const webpackConfig = {
entry: {
app: ["app.js"]
},
output: {
path: path.join(__dirname, "..", "static"),
publicPath: "/static",
filename: "app.[hash:7].js"
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin(),
new webpack.ResolverPlugin(
new webpack.ResolverPlugin.DirectoryDescriptionFilePlugin("bower.json", ["main"])
), // bower; see: https://github.com/webpack/docs/wiki/usage-with-bower
new ExtractTextPlugin("app.[hash:7].css"),
new HtmlWebpackPlugin({
inject: "body",
template: "app/index.html",
filename: "index.html"
}),
],
resolve: {
root: [path.join(__dirname, "/app"), nodeModulesPath, bowerComponentsPath]
},
noParse: [nodeModulesPath, bowerComponentsPath],
module: {
preLoaders: [
{
test: /\.js$/,
exclude: [nodeModulesPath, bowerComponentsPath],
loaders: [`[email protected]@&file=${environmentsFile}`]
}
],
loaders: [
{
test: /[\/]angular\.js$/,
exclude: [nodeModulesPath],
loader: "exports?angular"
},
{
test: /\.js$/,
exclude: [nodeModulesPath, bowerComponentsPath],
loaders: ["ng-annotate", "babel?presets[]=es2015&cacheDirectory=true"]
},
{
test: /\.s?css$/,
loader: ExtractTextPlugin.extract("style", "css?name=[name]-[hash:7].[ext]!postcss-loader!" + `sass?includePaths[]=${path.join(__dirname, "/app")}`)
},
{
test: /\.json$/,
loader: "json"
},
{
test: /\.(ttf|eot|svg|otf)(\?\S+)?$/,
loader: "file?name=[name]-[hash:7].[ext]"
},
{
test: /\.(png)$/,
loader: "url?limit=8192&name=[name]-[hash:7].[ext]&mimetype=image/png"
},
{
test: /\.(gif)$/,
loader: "url?limit=8192&name=[name]-[hash:7].[ext]&mimetype=image/gif"
},
{
test: /\.(jpg)$/,
loader: "url?limit=8192&name=[name]-[hash:7].[ext]&mimetype=image/jpeg"
},
{
test: /\.woff(2)?(\?\S+)?$/,
loader: "url?name=[name]-[hash:7].[ext]&limit=8192&minetype=application/font-woff"
},
{
test: /\.html$/,
exclude: `${path.join(__dirname, "/app/index.html")}`,
loaders: [`ngtemplate?relativeTo=${__dirname}`, "html"] //html?attrs[]=div:ng-include
}
]
},
postcss: function() {
return [require('autoprefixer')];
},
devServer: {
contentBase: "./app",
noInfo: false,
hot: true,
inline: true,
historyApiFallback: true
}
};
module.exports = webpackConfig;
したがって、バグが比較的悪い文書化されfileLoader
とngtemplateLoder
、に関連しているので、私はここに助けを求めています。
両方のローダーはパラメータ 'prefix'を持っています:' ngtemplate?prefix = static /&... ' –
@BobSpongeこんにちは、Bob Sponge!私はあなたに今解決しようとしています。しかし、私は理解していない、全くテンプレートの問題は何か。テンプレートはjavascriptにあり、javascriptはそれらを読み込もうとしています。だから、URLはまったく重要ではないはずです。 –
はい、そうです。インラインテンプレートではURLは関係ありません。 –