我做这个ASP.NET 4项目的前端(我几乎不知道.NET),当我按F5或Ctrl-F5时,应用程序服务是http://localhot:52724上的Index.cshtml但是当我运行npm start
时,我的webpack配置在http://localhost:8000上提供资产。这是一个React前端。
在index.cshtml文件中,如果我使用以下内容引用我的JS,一切都很好。
<script src="http://localhost:8000/assets/app.js"></script>
但我更愿意将其作为
服务<script src="assets/app.js"></script>
如果可能的话,在同一个端口上。或者可能有更好的方法吗?
这是base.js(在webpack.config.js中导入)
'use strict';
var path = require('path');
var port = 8000;
var srcPath = path.join(__dirname, '/../src');
var publicPath = '/assets/';
var additionalPaths = [];
module.exports = {
additionalPaths: additionalPaths,
port: port,
debug: true,
output: {
path: path.join(__dirname, '/../dist/assets'),
filename: 'app.js',
publicPath: publicPath
},
devServer: {
contentBase: './src/',
historyApiFallback: true,
hot: true,
inline: true,
port: port,
publicPath: publicPath,
noInfo: false
},
resolve: {
extensions: ['', '.js', '.jsx'],
alias: {
actions: srcPath + '/actions/',
components: srcPath + '/components/',
sources: srcPath + '/sources/',
stores: srcPath + '/stores/',
styles: srcPath + '/styles/',
config: srcPath + '/config/' + process.env.REACT_WEBPACK_ENV
}
},
module: {
preLoaders: [{
test: /\.(js|jsx)$/,
include: srcPath,
loader: 'eslint-loader'
}],
loaders: [{
test: /\.css$/,
loader: 'style-loader!css-loader'
}, {
test: /\.sass/,
loader: 'style-loader!css-loader!sass-loader?outputStyle=expanded&indentedSyntax'
}, {
test: /\.scss/,
loader: 'style-loader!css-loader!sass-loader?outputStyle=expanded'
}, {
test: /\.less/,
loader: 'style-loader!css-loader!less-loader'
}, {
test: /\.styl/,
loader: 'style-loader!css-loader!stylus-loader'
}, {
test: /\.(png|jpg|gif)$/,
loader: 'url-loader?limit=8192'
}, {
test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: 'url-loader?limit=10000&mimetype=application/font-woff'
}, {
test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: 'file-loader'
}]
}
};
这是dev.js(在webpack.config.js中导入)
'use strict';
var path = require('path');
var webpack = require('webpack');
var _ = require('lodash');
var baseConfig = require('./base');
// Add needed plugins here
var BowerWebpackPlugin = require('bower-webpack-plugin');
var config = _.merge({
entry: [
'webpack-dev-server/client?http://localhost:8000',
'webpack/hot/only-dev-server',
'./UI/src/index'
],
cache: true,
devtool: 'eval',
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin(),
new BowerWebpackPlugin({
searchResolveModulesDirectories: false
}),
new webpack.ProvidePlugin({
jQuery: 'jquery',
'fetch': 'imports?this=>global!exports?global.fetch!whatwg-fetch'
})
]
}, baseConfig);
// Add needed loaders
config.module.loaders.push({
test: /\.(js|jsx)$/,
loader: 'react-hot!babel-loader',
include: [].concat(
config.additionalPaths,
[ path.join(__dirname, '/../src') ]
)
});
module.exports = config;
和server.js文件
/*eslint no-console:0 */
require('core-js/fn/object/assign');
var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');
var config = require('./webpack.config');
var open = require('open');
new WebpackDevServer(webpack(config), config.devServer)
.listen(config.port, 'localhost', function(err) {
if (err) {
console.log(err);
}
console.log('Listening at localhost:' + config.port);
//console.log('Opening your system browser...');
//open('http://localhost:' + config.port);
});
答案 0 :(得分:2)
你可以看一下我工作的这个来源
// webpack.config.js
var webpack = require('webpack');
var extractTextPlugin = require('extract-text-webpack-plugin');
var clean = require('clean-webpack-plugin');
var webpackStrip = require('strip-loader');
var path = require('path');
var IS_DEBUG = process.env.NODE_ENV !== 'production';
var webpackConfig = {
entry: {
home: [
'./src/entries/home',
'./src/styles/entries/home'
],
vendor: [
// Styles
'bootstrap/dist/css/bootstrap',
'./src/styles/core',
// Scripts
'bootstrap',
'jquery'
],
},
output: {
path: __dirname + '/bundles',
filename: '[name].js',
publicPath: '/Content/bundles/'
},
resolve: {
extensions: ['', '.js', '.css', '.scss']
},
module: {
loaders: [
{
test: /\.js$/,
loader: 'babel',
exclude: /(node_modules|bower_components)/,
cacheable: true,
query: {
presets: ['es2015'],
retainLines: true,
cacheDirectory: true
}
},
{ test: /\.js$/, loader: 'eslint-loader', exclude: /(node_modules|bower_components)/ },
{ test: /\.css$/, loaders: ['style', 'css'] },
{
test: /\.scss$/,
loaders: ['style', 'css', 'sass'],
},
{ test: /\.woff2?(\?v=\d+\.\d+\.\d+)?$/, loader: "url?limit=10000&mimetype=application/font-woff" },
{ test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: "url?limit=10000&mimetype=application/octet-stream" },
{ test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: "file" },
{ test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: "url?limit=10000&mimetype=image/svg+xml" }
]
},
sassLoader: {
includePaths: ['./src/styles'],
},
devtool: IS_DEBUG ? 'cheap-source-map' : 'source-map',
plugins: [
new webpack.ProvidePlugin({
jQuery: 'jquery',
$: 'jquery',
'window.jQuery': 'jquery'
}),
new webpack.optimize.CommonsChunkPlugin(
'vendor', '[name].js'
)
]
};
if (IS_DEBUG) {
webpackConfig.plugins.push(
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin()
);
Object.keys(webpackConfig.entry).forEach(function(item) {
if (item === 'vendor') {
webpackConfig.entry[item].unshift('webpack/hot/dev-server');
} else {
webpackConfig.entry[item].unshift('webpack-hot-middleware/client');
}
});
}
// Production
if (!IS_DEBUG) {
webpackConfig.plugins.push(
new clean(['bundles']),
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
}
}),
new extractTextPlugin("[name].css")
);
webpackConfig.module.loaders.forEach(function(item) {
if (item.test.test('.scss')) {
delete item.loaders;
item.loader = extractTextPlugin.extract('css!sass');
}
if (item.test.test('.css')) {
delete item.loaders;
item.loader = extractTextPlugin.extract('css')
}
});
webpackConfig.module.loaders.unshift(
{ test: /\.js$/, loader: webpackStrip.loader('debug', 'console.log') }
);
}
module.exports = webpackConfig;
我正在创建一个带有hapijs的服务器来为资产提供服务并使其适用于HRM插件。还有一个代理ASP.NET来获取HTML和控制器响应。
// webpack-dev-server.js
import { Server } from 'hapi';
import H2o2 from 'h2o2';
import yargs from 'yargs';
import Webpack from 'webpack';
import WebpackPlugin from 'hapi-webpack-plugin';
import webpackConfig from './webpack.config';
const argv = yargs.argv;
const isNumeric = n => !isNaN(parseFloat(n)) && isFinite(n);
if (!isNumeric(argv.port)) {
console.log(`Port must be numeric`);
process.exit(-1);
}
const compiler = new Webpack(webpackConfig);
const server = new Server();
server.connection({ host: 'localhost', port: 6789, labels: 'proxy-server' });
const assets = {
publicPath: webpackConfig.output.publicPath,
hot: true,
noInfo: true,
quiet: false,
host: 'localhost',
port: 6790,
stats: {
colors: true,
},
};
const hot = {
log: console.log,
path: '/__webpack_hmr',
heartbeat: 10 * 1000,
};
server.register([
{
register: H2o2,
},
{
register: WebpackPlugin,
options: { compiler, assets, hot },
},
], error => {
if (error) {
return console.error(error);
}
server.route({
method: ['GET', 'POST'],
path: '/{path*}',
handler: (request, reply) => {
if (/^Content\/bundles\/[A-Za-z0-9\-]+\.css/.test(request.params.path)) {
const response = reply('// This is a fake CSS content... :)');
response.type('text/css');
return response;
}
return reply.proxy({
host: 'localhost',
port: argv.port,
passThrough: true,
});
},
});
server.start(() => console.log(`Server running on ${server.info.uri}`));
});
这是项目的repo,它尚未完成,但代理功能正常运行。
要以开发模式启动服务器,请运行npm start -- --port=[asp-net-project-port]
并使用npm run build
构建生产
所有输出文件都将放在Content/bundles
文件夹中,因此文件home.js
,vendor.js
和相关资源将存在。然后在剃刀中,您可以将javascript或样式链接到该文件。