springboot + webpack dev server,重建后不改变localhost包文件
springboot + webpack dev server, does not change localhost bundle file after rebuilding
click this pic and please read below
1.this 第一张图在 运行 webpack-dev-sercer --hot --inline
之后
第二张图是我的html : 我调用js文件的方式
我将 jsx 文件更改为测试服务器
npm 表示完全重建
但是,localhost:9090 中的捆绑 js 文件在重建后不会像上图
那样改变
下面的代码是我的webpack.config.js文件
var path = require('path');
var webpack = require('webpack');
var merge = require('webpack-merge');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var jeet = require('jeet');
var nib = require('nib');
var RES = path.join(__dirname, 'src/main/resources');
var STATIC = path.join(__dirname, 'src/main/resources/static');
const TARGET = process.env.npm_lifecycle_event;
const common = {
entry: {
chunkA: path.join(STATIC, 'chunkA/chunkA_world.jsx'),
chunkB: path.join(STATIC, 'chunkB/chunkB_world.jsx')
},
output: {
path: path.join(STATIC, 'jsbuilt'),
filename: '[name].bundle.js',
chunkFileName: '[id].bundle.js',
publicPath: ''
},
plugins: [
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.CommonsChunkPlugin({
name: 'vendors',
minChunks(module, count) {
return (
module.resource &&
module.resource.indexOf(path.resolve('node_modules')) === 0
)
}
}),
new HtmlWebpackPlugin({
}),
new webpack.DefinePlugin({
"process.env": {
NODE_ENV: JSON.stringify('development')
}
}),
new ExtractTextPlugin('styles.css')
],
resolve: {
extensions: ['', '.js'],
root: RES
},
module: {
preLoaders: [
{
test: /\.css$/,
loader: 'stripcomment'
}
],
loaders: [{
test: /\.jsx?$/,
exclude: /(node_modules)/,
loader: ['babel'],
include: STATIC,
query:
{
presets:['es2015','stage-0','react'],
compact: false
}
}, {
test: /\.styl$/,
loader: ExtractTextPlugin.extract('css-loader!stylus-loader')
}, {
test: /\.json/,
loader: ['json-loader']
}]
},
stylus: {
use: [jeet(), nib()]
}
};
if (TARGET === 'start' || !TARGET) {
module.exports = merge(common, {
devServer: {
port: 9090,
proxy: {
'/*': {
target: 'http://localhost:8080',
secure: false,
prependPath: false
}
},
publicPath: 'http://localhost:9090/',
historyApiFallback: true
},
devtool: 'source-map'
});
}
if (TARGET === 'build') {
module.exports = merge(common, {});
}
我的项目是 spring boot
所以,我的问题是,如何更改localhost9090 js文件?
您需要更新您的 Webpack 输出位置以指向目标目录,以便您实现高效的实时重新加载工作流。
例子:
module.exports = {
entry: "./src/app.js",
output: {
path: '../../../target/classes/static/js',
filename: "bundle.js"
},
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: ['es2015', 'react']
}
}
]
}
};
嵌入式服务器从 Target 目录中拉取,因此对于 Webpack 等外部构建过程直接推送到该目录来说效果更好。
我更改了我的 webpack.config.js 文件,如下所示
var path = require('path');
var webpack = require('webpack');
var merge = require('webpack-merge');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var jeet = require('jeet');
var nib = require('nib');
var RES = path.join(__dirname, 'src/main/resources');
var STATIC = path.join(__dirname, 'src/main/resources/static');
module.exports = {
devtool: 'source-map',
entry: {
chunkA: path.join(STATIC, 'chunkA/chunkA_world.jsx'),
chunkB: path.join(STATIC, 'chunkB/chunkB_world.jsx')
},
output: {
path: path.join(STATIC, 'jsbuilt'),
filename: '[name].bundle.js',
chunkFileName: '[id].bundle.js',
publicPath: 'http://localhost:9090/jsbuilt/'
},
plugins: [
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.CommonsChunkPlugin({
name: 'vendors',
minChunks(module, count) {
return (
module.resource &&
module.resource.indexOf(path.resolve('node_modules')) === 0
)
}
}),
new webpack.DefinePlugin({
"process.env": {
NODE_ENV: JSON.stringify('development')
}
}),
new ExtractTextPlugin('styles.css')
],
resolve: {
extensions: ['', '.js'],
root: RES
},
module: {
preLoaders: [
{
test: /\.css$/,
loader: 'stripcomment'
}
],
loaders: [{
test: /\.jsx?$/,
exclude: /(node_modules)/,
loader: ['babel'],
include: STATIC,
query:
{
presets:['es2015','stage-0','react'],
compact: false
}
}, {
test: /\.styl$/,
loader: ExtractTextPlugin.extract('css-loader!stylus-loader')
}, {
test: /\.json/,
loader: ['json-loader']
}]
},
stylus: {
use: [jeet(), nib()]
},
devServer: {
port: 9090,
proxy: {
'/*': {
target: 'http://localhost:8080',
secure: false,
prependPath: false
}
},
publicPath: 'http://localhost:9090/jsbuilt',
historyApiFallback: true
}
};
它奏效了。谢谢!
click this pic and please read below
1.this 第一张图在 运行 webpack-dev-sercer --hot --inline
之后第二张图是我的html : 我调用js文件的方式
我将 jsx 文件更改为测试服务器 npm 表示完全重建 但是,localhost:9090 中的捆绑 js 文件在重建后不会像上图
那样改变
下面的代码是我的webpack.config.js文件
var path = require('path');
var webpack = require('webpack');
var merge = require('webpack-merge');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var jeet = require('jeet');
var nib = require('nib');
var RES = path.join(__dirname, 'src/main/resources');
var STATIC = path.join(__dirname, 'src/main/resources/static');
const TARGET = process.env.npm_lifecycle_event;
const common = {
entry: {
chunkA: path.join(STATIC, 'chunkA/chunkA_world.jsx'),
chunkB: path.join(STATIC, 'chunkB/chunkB_world.jsx')
},
output: {
path: path.join(STATIC, 'jsbuilt'),
filename: '[name].bundle.js',
chunkFileName: '[id].bundle.js',
publicPath: ''
},
plugins: [
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.CommonsChunkPlugin({
name: 'vendors',
minChunks(module, count) {
return (
module.resource &&
module.resource.indexOf(path.resolve('node_modules')) === 0
)
}
}),
new HtmlWebpackPlugin({
}),
new webpack.DefinePlugin({
"process.env": {
NODE_ENV: JSON.stringify('development')
}
}),
new ExtractTextPlugin('styles.css')
],
resolve: {
extensions: ['', '.js'],
root: RES
},
module: {
preLoaders: [
{
test: /\.css$/,
loader: 'stripcomment'
}
],
loaders: [{
test: /\.jsx?$/,
exclude: /(node_modules)/,
loader: ['babel'],
include: STATIC,
query:
{
presets:['es2015','stage-0','react'],
compact: false
}
}, {
test: /\.styl$/,
loader: ExtractTextPlugin.extract('css-loader!stylus-loader')
}, {
test: /\.json/,
loader: ['json-loader']
}]
},
stylus: {
use: [jeet(), nib()]
}
};
if (TARGET === 'start' || !TARGET) {
module.exports = merge(common, {
devServer: {
port: 9090,
proxy: {
'/*': {
target: 'http://localhost:8080',
secure: false,
prependPath: false
}
},
publicPath: 'http://localhost:9090/',
historyApiFallback: true
},
devtool: 'source-map'
});
}
if (TARGET === 'build') {
module.exports = merge(common, {});
}
我的项目是 spring boot 所以,我的问题是,如何更改localhost9090 js文件?
您需要更新您的 Webpack 输出位置以指向目标目录,以便您实现高效的实时重新加载工作流。
例子:
module.exports = {
entry: "./src/app.js",
output: {
path: '../../../target/classes/static/js',
filename: "bundle.js"
},
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: ['es2015', 'react']
}
}
]
}
};
嵌入式服务器从 Target 目录中拉取,因此对于 Webpack 等外部构建过程直接推送到该目录来说效果更好。
我更改了我的 webpack.config.js 文件,如下所示
var path = require('path');
var webpack = require('webpack');
var merge = require('webpack-merge');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var jeet = require('jeet');
var nib = require('nib');
var RES = path.join(__dirname, 'src/main/resources');
var STATIC = path.join(__dirname, 'src/main/resources/static');
module.exports = {
devtool: 'source-map',
entry: {
chunkA: path.join(STATIC, 'chunkA/chunkA_world.jsx'),
chunkB: path.join(STATIC, 'chunkB/chunkB_world.jsx')
},
output: {
path: path.join(STATIC, 'jsbuilt'),
filename: '[name].bundle.js',
chunkFileName: '[id].bundle.js',
publicPath: 'http://localhost:9090/jsbuilt/'
},
plugins: [
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.CommonsChunkPlugin({
name: 'vendors',
minChunks(module, count) {
return (
module.resource &&
module.resource.indexOf(path.resolve('node_modules')) === 0
)
}
}),
new webpack.DefinePlugin({
"process.env": {
NODE_ENV: JSON.stringify('development')
}
}),
new ExtractTextPlugin('styles.css')
],
resolve: {
extensions: ['', '.js'],
root: RES
},
module: {
preLoaders: [
{
test: /\.css$/,
loader: 'stripcomment'
}
],
loaders: [{
test: /\.jsx?$/,
exclude: /(node_modules)/,
loader: ['babel'],
include: STATIC,
query:
{
presets:['es2015','stage-0','react'],
compact: false
}
}, {
test: /\.styl$/,
loader: ExtractTextPlugin.extract('css-loader!stylus-loader')
}, {
test: /\.json/,
loader: ['json-loader']
}]
},
stylus: {
use: [jeet(), nib()]
},
devServer: {
port: 9090,
proxy: {
'/*': {
target: 'http://localhost:8080',
secure: false,
prependPath: false
}
},
publicPath: 'http://localhost:9090/jsbuilt',
historyApiFallback: true
}
};
它奏效了。谢谢!