Html 为什么webpack没有';是否将CSS从组件渲染到vendor.CSS文件?

Html 为什么webpack没有';是否将CSS从组件渲染到vendor.CSS文件?,html,css,angular,webpack,styles,Html,Css,Angular,Webpack,Styles,我用这个,效果很好。正如您在上所看到的,所有lybrarys(如bootstrap)都将其CSS呈现到vendor.CSS中,但我的组件直接将其呈现到html中,仅呈现到标记中html文件的头部分。你能告诉我如何正确配置它吗 // webpack.config.js var isDevBuild = process.argv.indexOf('--env.prod') < 0; var path = require('path'); var webpack = require('webp

我用这个,效果很好。正如您在上所看到的,所有lybrarys(如bootstrap)都将其CSS呈现到vendor.CSS中,但我的组件直接将其呈现到html中,仅呈现到标记中html文件的头部分。你能告诉我如何正确配置它吗

// 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(\?|$))([^.]+(\?|$))/;
//var ExtractTextPlugin = require('extract-text-webpack-plugin');

// 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: /\.html$/, loader: 'raw' },
            { test: /\.css$/, loader: 'to-string!css' },
            { test: /\.(png|jpg|jpeg|gif|svg)$/, loader: 'url', query: { limit: 25000 } },
            { test: /\.(jpg1)$/, loader: 'file?name=[name].[ext]' }
        ]
    }
};

// Configuration for client-side bundle suitable for running in browsers
var clientBundleConfig = merge(sharedConfig, {
    entry: { 'main-client': './ClientApp/boot-client.ts' },
    output: { path: path.join(__dirname, './wwwroot/dist') },
    devtool: isDevBuild ? 'inline-source-map' : null,
    plugins: [
        new webpack.DllReferencePlugin({
            context: __dirname,
            manifest: require('./wwwroot/dist/vendor-manifest.json')
        })
    ].concat(isDevBuild ? [] : [
        // 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];
//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(\?|$)([^.]+(\?|$)/;
//var ExtractTextPlugin=require('extract-text-webpack-plugin');
//客户端和服务器端捆绑包的共同配置
var sharedConfig={
解析:{extensions:['','.js','.ts']},
输出:{
文件名:'[name].js',
publicPath:'/dist/'//Webpack dev中间件,如果启用,将处理对此URL前缀的请求
},
模块:{
装载机:[
{test:/\.ts$/,include:/ClientApp/,loader:'ts',查询:{silent:true},
{test:/\.html$/,加载程序:'raw'},
{test:/\.css$/,加载程序:'to string!css'},
{test://\(png | jpg | jpeg | gif | svg)$/,加载程序:'url',查询:{limit:25000},
{test://\(jpg1)$/,加载程序:'file?name=[name].[ext]'}
]
}
};
//适用于在浏览器中运行的客户端捆绑包的配置
var clientBundleConfig=merge(sharedConfig{
条目:{'main client':'./ClientApp/boot client.ts'},
输出:{path:path.join(uu dirname,'./wwwroot/dist'),
devtool:isDevBuild?“内联源映射”:null,
插件:[
新建webpack.DllReferencePlugin({
上下文:_dirname,
清单:要求('./wwwroot/dist/vendor manifest.json')
})
].concat(isDevBuild?[]:[
//仅适用于生产版本的插件
新建webpack.optimize.OccurenceOrderPlugin(),
新建webpack.optimize.UglifyJsPlugin()
])
});
//适合在节点中运行的服务器端(预渲染)捆绑包的配置
var serverBundleConfig=merge(sharedConfig{
条目:{'main server':'./ClientApp/boot server.ts'},
输出:{
libraryTarget:'commonjs',
path:path.join(uu dirname,'./ClientApp/dist')
},
目标:'节点',
devtool:“内联源映射”,
externals:[nodeExternals({whitelist:[allFilenamesExceptJavaScript]})]//不捆绑来自节点\模块的.js文件
});
module.exports=[clientBundleConfig,serverBundleConfig];

//webpack.config.vendor.js
var isDevBuild=process.argv.indexOf('--env.prod')<0;
var path=require('path');
var webpack=require('webpack');
var ExtractTextPlugin=require('extract-text-webpack-plugin');
var extractCSS=新的ExtractTextPlugin('vendor.css');
module.exports={
决心:{
扩展名:['',.js']
},
模块:{
装载机:[
{测试:/\(png | woff | woff2 | eot | ttf | svg)(\?$)/,加载器:'url加载器?限制=100000',
{test:/\.css(\?\$)/,加载程序:extractCSS.extract(['css'])}
]
},
条目:{
供应商:[
“@angular/common”,
“@angular/compiler”,
“@angular/core”,
“@angular/http”,
“@角度/平台浏览器”,
“@角度/平台浏览器动态”,
“@angular/router”,
“@angular/platform服务器”,
“angular2通用”,
“angular2通用多边形填充”,
“自举”,
'bootstrap/dist/css/bootstrap.css',
“es6垫片”,
“es6承诺”,
“jquery”,
'zone.js',
“角度2模态”
]
},
输出:{
path:path.join(uu dirname,'wwwroot','dist'),
文件名:'[name].js',
库:'[name]\[hash]',
},
插件:[
提取CSS,
new webpack.ProvidePlugin({$:'jquery',jquery:'jquery'}),//将这些标识符映射到jquery包(因为Bootstrap希望它是一个全局变量)
新建webpack.optimize.OccurenceOrderPlugin(),
新建webpack.DllPlugin({
path:path.join(uu dirname,'wwwroot','dist','[name]-manifest.json'),
名称:'[name]\[hash]'
})
].concat(isDevBuild?[]:[
新建webpack.optimize.UglifyJsPlugin({compress:{warnings:false}})
])
};

您似乎正在使用加载组件css。您可以将其更改为与webpack.config中的vendor.css相同的加载程序(最有可能是文件加载程序)。似乎您正在使用它加载组件css。您可以将其更改为与webpack.config中的vendor.css相同的加载程序(很可能是文件加载程序)。如果比较配置文件中的两个css加载程序,您可以看到
webpack.config.vendor.js
有以下行:

{test:/\.css(\?\$)/,加载程序:extractCSS.extract(['css'])}

装载机下方。这一行接受CSS并将其放入自己的文件中。因此,如果更改
webpack.config.js的CSS加载程序,并确保添加:

var-extractCSS=new-ExtractTextPlugin('styles.css');//也可以随意命名


在文件的顶部。它应该可以工作。

如果比较配置文件中的两个css加载程序,您可以看到
webpack.config.vendor.js
有以下行:

{test:/\.css(\?\$)/,加载程序:extractCSS.extract(['css'])}

装载机下方。这一行接受CSS并将其放入自己的文件中。因此,如果更改
webpack.config.js的CSS加载程序,并确保添加:

var-extractCSS=new-ExtractTextPlugin('styles.css');//也可以随意命名


在文件的顶部。它应该可以工作。

你能添加你的
webpack.config.js
吗?网页包
// webpack.config.vendor.js

var isDevBuild = process.argv.indexOf('--env.prod') < 0;
var path = require('path');
var webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var extractCSS = new ExtractTextPlugin('vendor.css');

module.exports = {
    resolve: {
        extensions: ['', '.js']
    },
    module: {
        loaders: [
            { test: /\.(png|woff|woff2|eot|ttf|svg)(\?|$)/, loader: 'url-loader?limit=100000' },
            { test: /\.css(\?|$)/, loader: extractCSS.extract(['css']) }
        ]
    },
    entry: {
        vendor: [
            '@angular/common',
            '@angular/compiler',
            '@angular/core',
            '@angular/http',
            '@angular/platform-browser',
            '@angular/platform-browser-dynamic',
            '@angular/router',
            '@angular/platform-server',
            'angular2-universal',
            'angular2-universal-polyfills',
            'bootstrap',
            'bootstrap/dist/css/bootstrap.css',
            'es6-shim',
            'es6-promise',
            'jquery',
            'zone.js',
            'angular2-modal'
        ]
    },
    output: {
        path: path.join(__dirname, 'wwwroot', 'dist'),
        filename: '[name].js',
        library: '[name]_[hash]',
    },
    plugins: [
        extractCSS,
        new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jquery' }), // Maps these identifiers to the jQuery package (because Bootstrap expects it to be a global variable)
        new webpack.optimize.OccurenceOrderPlugin(),
        new webpack.DllPlugin({
            path: path.join(__dirname, 'wwwroot', 'dist', '[name]-manifest.json'),
            name: '[name]_[hash]'
        })
    ].concat(isDevBuild ? [] : [
        new webpack.optimize.UglifyJsPlugin({ compress: { warnings: false } })
    ])
};