【发布时间】:2016-12-22 04:04:27
【问题描述】:
我遇到了由 sass 样式引起的 webpack 包大小问题...
我有许多使用 materialize-css 库设计的 angular2 组件。我使用 @extend 而不是将 materialize-css 类放在我的 html 中来封装 html 本身的库。
我遇到的问题是 webpack 的包大小非常大,因为每个组件的 sass 都会导入 materialize-css 包,并且因为组件的样式是使用 raw 加载器加载的,所以它会嵌入到包 js 文件中。所以我得到的是每个组件都具有相同 materialize-css 样式的巨大捆绑包。
只是为了比较我编译的应用程序中所有样式都被注释掉了,捆绑包大小为 1.08MB,包含样式时为 5.26MB
谁能指出正确的解决方案?
谢谢!
编辑: 我的 webpack 定义:
Webpack.common:
import * as webpack from 'webpack';
import {Configuration} from 'webpack';
import * as path from 'path';
import {PathHelper} from '../../common/pathHelper';
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var HtmlWebpackPlugin = require('html-webpack-plugin');
process.env.ENV = process.env.NODE_ENV;
export var webpackCommonConfiguration: Configuration = {
entry: {
'polyfills': './src/app/polyfills.ts',
'app': './src/app/app.ts',
'signin': './src/app/signinApp.ts',
'vendor': './src/app/vendor.ts',
'design': './src/app/design.scss'
},
resolve: {
extensions: ['', '.ts', '.js', 'scss']
},
module: {
loaders: [
{
test: /\.ts$/,
loader: 'ts'
},
{
test: /\.html$/,
loader: 'html'
},
{
test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
loader: 'file?name=assets/[name].[hash].[ext]'
},
{
test: /\.scss$/,
exclude: PathHelper.getPathFromRoot('src', 'app', 'modules'),
loader: ExtractTextPlugin.extract('style', 'css!resolve-url!sass?sourceMap')
},
{
test: /\.scss$/,
include: PathHelper.getPathFromRoot('src', 'app', 'modules'),
loader: 'raw!sass'
},
{
test: /\.css$/,
exclude: PathHelper.getPathFromRoot('src', 'app', 'modules'),
loader: ExtractTextPlugin.extract('style', 'css!resolve-url!css?sourceMap')
},
{
test: /\.css$/,
include: PathHelper.getPathFromRoot('src', 'app', 'modules'),
loader: 'raw!resolve-url'
}
]
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({
name: 'signinCommon',
chunks: ['signin', 'vendor'],
minChunks: Infinity
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'homeCommon',
chunks: ['app', 'vendor'],
minChunks: Infinity
}),
new webpack.DefinePlugin({
'process.env': {
'ENV': JSON.stringify(process.env.NODE_ENV)
}
}),
new webpack.ProvidePlugin({
// Required for the velocity plugin to be loaded correctly (used in materialize css)
"window.$": "jquery",
"window.jQuery": "jquery",
"root.jQuery": "jquery"
}),
new HtmlWebpackPlugin({
template: PathHelper.getPathFromRoot('src', 'app', 'views', 'signin.html'),
filename: 'signin.html',
chunks: ['polyfills', 'signinCommon', 'design', 'vendor', 'signin']
}),
new HtmlWebpackPlugin({
template: PathHelper.getPathFromRoot('src', 'app', 'views', 'home.html'),
filename: 'home.html',
chunks: ['polyfills', 'homeCommon', 'design', 'vendor', 'app']
})
]
}
Webpack.prod:
var webpackMerge = require('webpack-merge');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
import * as webpack from 'webpack';
import {Configuration} from 'webpack';
import {webpackCommonConfiguration} from './webpack.common.config'
import {PathHelper} from '../../common/pathHelper';
interface IProductionConfiguration extends Configuration {
htmlLoader: any;
}
var config: IProductionConfiguration = {
devtool: 'source-map',
output: {
path: PathHelper.getPathFromRoot('dist'),
publicPath: '/dist/',
filename: '[name].[hash].js',
chunkFilename: '[id].[hash].chunk.js'
},
htmlLoader: {
minimize: false // workaround for ng2
},
plugins: [
new webpack.NoErrorsPlugin(),
new webpack.optimize.DedupePlugin(),
new webpack.optimize.UglifyJsPlugin(),
new ExtractTextPlugin('[name].[hash].css')
]
}
export var webpackProductionConfig: Configuration =
webpackMerge(webpackCommonConfiguration, config);
编辑2: 组件示例:
credits.component.ts:
import {Component} from '@angular/core';
@Component({
selector: 'credits',
template: require('./credits.component.html'),
styles: [require('./credits.component.scss')]
})
export class CreditsComponent {
}
credits.component.html:
<footer class="page-footer credits-footer">
<div class="footer-copyright">
<div class="container">
© 2016 Created by Slava Shpitalny
</div>
</div>
</footer>
credits.component.scss:
@import '~materialize-css/sass/materialize.scss';
:host{
.page-footer.credits-footer{
@extend .blue;
margin: 0;
padding: 0;
}
}
【问题讨论】:
-
更新了我的答案,希望对您有所帮助:)
标签: angular typescript sass webpack