使用服务器端渲染设置webpack来加载asp.net core项目中的Sass文件
Setup webpack with server side rendering to load Sass files in asp.net core project
我正在使用一个名为"aspnetcore-spa"的Yeoman项目模板,这是一个ASP.net core 1模板,与主要的SPA框架(Angular2和React)结合使用。
我用Angular2创建了一个项目。生物模板的代码工作得很好,没有问题。一旦我将Sass加载器添加到webpack.config.js中,并从任何angular文件中引用Sass文件。
在webpack.config.js中:var isDevBuild = process.argv.indexOf('--env.prod') < 0;
var path = require('path');
var webpack = require('webpack');
var nodeExternals = require('webpack-node-externals');
var merge = require('webpack-merge');
var allFilenamesExceptJavaScript = /'.(?!js('?|$))([^.]+('?|$))/;
// Configuration in common to both client-side and server-side bundles
var sharedConfig = {
resolve: { extensions: [ '', '.js', '.ts' ] },
output: {
filename: '[name].js',
publicPath: '/dist/' // Webpack dev middleware, if enabled, handles requests for this URL prefix
},
module: {
loaders: [
{ test: /'.ts$/, include: /ClientApp/, loader: 'ts', query: { silent: true } },
{ test: /'.scss$/,include:/ClientApp/, loaders: ["style", "css", "sass"] },
{ test: /'.html$/,include: /ClientApp/, loader: 'raw' },
{ test: /'.css$/, loader: 'to-string!css' },
{ test: /'.(png|jpg|jpeg|gif|svg)$/, loader: 'url', query: { limit: 25000 } }
]
}
};
// Configuration for client-side bundle suitable for running in browsers
var clientBundleOutputDir = './wwwroot/dist';
var clientBundleConfig = merge(sharedConfig, {
entry: { 'main-client': './ClientApp/boot-client.ts' },
output: { path: path.join(__dirname, clientBundleOutputDir) },
plugins: [
new webpack.DllReferencePlugin({
context: __dirname,
manifest: require('./wwwroot/dist/vendor-manifest.json')
})
].concat(isDevBuild ? [
// Plugins that apply in development builds only
new webpack.SourceMapDevToolPlugin({
filename: '[file].map', // Remove this line if you prefer inline source maps
moduleFilenameTemplate: path.relative(clientBundleOutputDir, '[resourcePath]') // Point sourcemap entries to the original file locations on disk
})
] : [
// Plugins that apply in production builds only
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.UglifyJsPlugin()
])
});
// Configuration for server-side (prerendering) bundle suitable for running in Node
var serverBundleConfig = merge(sharedConfig, {
entry: { 'main-server': './ClientApp/boot-server.ts' },
output: {
libraryTarget: 'commonjs',
path: path.join(__dirname, './ClientApp/dist')
},
target: 'node',
devtool: 'inline-source-map',
externals: [nodeExternals({ whitelist: [allFilenamesExceptJavaScript] })] // Don't bundle .js files from node_modules
});
module.exports = [clientBundleConfig, serverBundleConfig];
在我的组件中:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-wine',
template: require('./wine.component.html'),
styles: require('./wine.component.scss')
})
export class WineComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
我已经安装了与sass loader相关的npm包:
npm install node-sass sass-loader --save-dev
我检查了wwwroot/dist文件夹中的main-server.js文件,这是webpack捆绑的结果,我看到。scss文件被加载,它们的样式被正确处理。一旦我运行应用程序,显示这个异常,这是来自服务器端渲染端:
处理请求时发生未处理的异常。
异常:调用Node模块失败,错误:ReferenceError:窗口未定义在E:'Dev'MyApp'MyAppCore'src'MyApp. web 'ClientApp'dist'main-server.js:573:31 at E:'Dev'MyApp'MyAppCore'src'MyApp. web 'ClientApp'dist'main-server.js:568:48 at module。(E:'Dev'MyApp'MyAppCore'src'MyApp. web 'ClientApp'dist'main-server.js:590:69)(E: '开发' MyApp ' MyAppCore ' src ' MyApp.Web ' ClientApp ' dist ' main-server.js: 526:38)在 webpack_require (E: '开发' MyApp ' MyAppCore ' src ' MyApp.Web ' ClientApp ' dist ' main-server.js: 20:30)在E: '开发' MyApp ' MyAppCore ' src ' MyApp.Web ' ClientApp ' dist ' main-server.js: 501:22在Object.module.exports (E: '开发' MyApp ' MyAppCore ' src ' MyApp.Web ' ClientApp ' dist ' main-server.js: 506:3)在 webpack_require (E: '开发' MyApp ' MyAppCore ' src ' MyApp.Web ' ClientApp ' dist ' main-server.js: 20:30)对象。(E:'Dev'MyApp'MyAppCore'src'MyApp. web 'ClientApp'dist'main-server.js:129:25) at webpack_require (E:'Dev'MyApp'MyAppCore'src'MyApp. web 'ClientApp'dist'main-server.js:20:30)
这显然是因为webpack的服务器端渲染,因为它在node. js端运行代码(通过ASP.net Core的Javascript服务),并且有一个代码与DOM window
对象相结合,在节点上无效。
什么线索吗?
我设法解决了这个问题,这里是web.config.js位:
(注意.scss文件的加载器)
module: {
loaders: [
{ test: /'.ts$/, include: /ClientApp/, loader: 'ts', query: { silent: true } },
{ test: /'.scss$/,include:/ClientApp/, loaders: ["to-string", "css", "sass"] },
{ test: /'.html$/,include: /ClientApp/, loader: 'raw' },
{ test: /'.css$/, loader: 'to-string!css' },
{ test: /'.(png|jpg|jpeg|gif|svg)$/, loader: 'url', query: { limit: 25000 } }
]
}
在Angular组件中,我把样式改成了:
(传递所需的css文件数组而不是单个css文件)
@Component({
selector: 'app-wine',
template: require('./wine.component.html'),
styles: [require('./wine.component.scss')]
})
- 正在将数据主题添加到所有项目
- 动态地改变“”的URL;添加新项目”;链接使用javascript/jquery
- 如何检查管道中未定义的项目
- 有没有一种方法可以添加相同的项目val=“0”;4〃;到JavaScript中数组的每个对象
- 学生搜索项目jquery/javascript
- 如何获取不属于我项目的上一页的URL
- Dojo:访问dijit.form.Select中单击的项目
- 角度的项目列表 ng 重复,ng 单击显示全宽描述
- 我需要为我的朋友在这个项目上提供帮助
- 如何从 HTML 查询中删除项目
- 在Meteor项目中安装并包含npm模块后出错
- 当在Ember中点击一个项目时,我如何将一个活动类添加到项目列表中
- 将单击事件添加到附加的项目中
- 通过另一个php应用程序将我的项目推送到Github存储库中
- 在javascript中搜索项目列表的性能
- 如何将本地依赖项添加到npm项目中
- angularjs移除焦点上的活动类并添加到下一个项目
- 为什么react/redux前端项目在package.json中包含生产依赖项
- 将webpack和babel与父项目目录中的文件一起使用
- 使用服务器端渲染设置webpack来加载asp.net core项目中的Sass文件