gpt4 book ai didi

Webpack 开发服务器 : define WWW root

转载 作者:行者123 更新时间:2023-12-04 19:29:59 26 4
gpt4 key购买 nike

使用 webpack,我将源文件与分发文件分开。

我遇到的一个问题与 output.publicPath 有关,如果我定义一个,生成的 index.html 有错误的 CSS 和 JS 资源路径。我认为问题与extract-text-webpack-plugin有关见:https://github.com/webpack-contrib/extract-text-webpack-plugin/issues/246

现在我已经将代码分为源代码和分发版 index.html可在 http://localhost/target/classes/war 获得.并且每次页面刷新都需要手动输入 URL,因为路由无法识别文件夹名称。 <base />头中的标签似乎没有效果。

如何指示 webpack 显示文件夹 ./target/classes/war 的内容在 http://localhost/ ? (不使用 output.publicPath )

为了完整起见,请在下面找到整个 webpack.config.js就目前而言:

var path = require('path');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var GoogleFontsPlugin = require("google-fonts-webpack-plugin");

module.exports = {

entry: {
polyfills: './src/main/webapp/polyfills.ts',
vendor: './src/main/webapp/vendor.ts',
app: './src/main/webapp/app/core/global/main/main.ts'
},

output: {
filename: 'target/classes/war/script/[name].[hash].bundle.js'
},

resolve: {
extensions: ['.ts', '.js'],

alias: {
css: path.resolve(__dirname, "src/main/webapp/WEB-INF/scss"),
src: path.resolve(__dirname, "src/main/webapp"),
dist: path.resolve(__dirname, "target/class/war")
}
},

module: {

rules: [
{
test: /\.ts$/,
loaders: [
{
loader: 'awesome-typescript-loader',
options: {
configFileName: path.resolve(__dirname, "src/main/webapp/WEB-INF/conf/tsconfig.json")
}
},
'angular2-template-loader'
]
},
{
test: /\.html$/,
loader: 'html-loader'
},
{
test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
loaders: [
{
loader: "file-loader",
options: {
hash: "sha512",
digest: "hex",
name: "/target/classes/war/assets/[name].[hash].[ext]"
}
}
]
},
{
test: /\.(css|scss)$/,
exclude: path.resolve(__dirname, "src/main/webapp/scss"),
include: path.resolve(__dirname, "src/main/webapp/app"),
loaders: [
{
loader: "raw-loader"
},
{
loader: "sass-loader"
}
]

},
{
test: /\.(css|scss)$/,
exclude: path.resolve(__dirname, "src/main/webapp/app"),
use: [
{
loader: "style-loader" // creates style nodes from JS strings
},
{
loader: "css-loader" // translates CSS into CommonJS
},
{
loader: "resolve-url-loader" // compiles Sass to CSS
},
{
loader: "sass-loader" // compiles Sass to CSS
}
]
}
]
},
plugins: [
// Workaround for angular/angular#11580
new webpack.ContextReplacementPlugin(
// The (\\|\/) piece accounts for path separators in *nix and Windows
/angular(\\|\/)core(\\|\/)@angular/,
path.resolve(__dirname, "src/main/webapp"), // location of your src
{} // a map of your routes
),

new webpack.optimize.CommonsChunkPlugin({
name: ['app', 'vendor', 'polyfills']
}),

new HtmlWebpackPlugin({
template: 'src/main/webapp/index.html',
filename: 'target/classes/war/index.html',
chunksSortMode: function (chunk1, chunk2) {
var order = ['polyfills', 'vendor', 'app'];
var order1 = order.indexOf(chunk1.names[0]);
var order2 = order.indexOf(chunk2.names[0]);
return order1 - order2;
}
}),

new ExtractTextPlugin({
filename: "target/classes/war/css/styles.[contenthash].css"
}),
new GoogleFontsPlugin({
fonts: [
{family: "Lato"}
],
path: "src/main/webapp/scss/fonts/",
filename: "src/main/webapp/scss/fonts/fonts.css"
})
]
};

最佳答案

这个 webpack 配置的结果是。

<script type="text/javascript" src="http://localhost/target/classes/war/script/app.5e643bae101bd4743814.bundle.js"></script>

配置
var path = require('path');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
entry: {
// it's .js now for testing
app: './src/main/webapp/app/core/global/main/main.js'
},
output: {
filename: 'target/classes/war/script/[name].[hash].bundle.js',
publicPath: 'http://localhost/'
},
module: {
rules: [
{
test: /\.(css|scss)$/,
loaders: [
{
loader: "raw-loader"
},
{
loader: "sass-loader"
}
]
},
]
},
plugins: [

new HtmlWebpackPlugin({
// it's .ejs now
template: 'src/main/webapp/index.template.ejs',
filename: 'target/classes/war/index.html',
}),

new ExtractTextPlugin({
filename: "target/classes/war/css/styles.[contenthash].css"
}),
]
};

关于Webpack 开发服务器 : define WWW root,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44617050/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com