2016-01-06 162 views
2

我有這個非常有趣的webpack問題,我無法弄清楚我的生活。Webpack字體包括問題

我有像這樣的標準字體面對面聲明:

// MarkOT-ExtraLight 
@font-face { 
    font-family: 'MarkOT-ExtraLight'; 
    src: require('MarkOT-ExtraLight.eot?#iefix') format('embedded-opentype'), 
     require('MarkOT-ExtraLight.otf') format('opentype'), 
     require('MarkOT-ExtraLight.woff') format('woff'), 
     require('MarkOT-ExtraLight.ttf') format('truetype'), 
     require('MarkOT-ExtraLight.svg#MarkOT-ExtraLight') format('svg'); 
    font-weight: normal; 
    font-style: normal; 
} 

```

現在我注意到,除了沒有我的字體加載了我的手機上使用require工作的罰款。所以我從require切換到url,你看,它一切正常。

我將我的應用程序部署到了heroku,直到我訪問我的網站時,我發現我的css文件已從2.8 MB增長到57.3 MB。是的,你聽到了,57.3 MB。我通過在我的字體聲明中使用require切換到使用url來確認這是實際發生的情況,對它進行了三次測試。

有沒有人遇到類似的東西?我已經在下面包含了我的webpack配置。

var webpack = require('webpack'); 
var path = require('path'); 
var HtmlWebpackPlugin = require('html-webpack-plugin'); 
var BrowserSyncPlugin = require('browser-sync-webpack-plugin'); 
var ExtractTextPlugin = require('extract-text-webpack-plugin'); 

//Environment constants 

const DEVELOPMENT = "development"; 
const PRODUCTION = "production"; 

// Functions for resolving all the aliases 
var path_base = path.resolve(__dirname, '../'); 
const resolve = path.resolve; 
const base = function() { 
    var args = [path_base]; 
    args.push.apply(args, arguments); 
    return resolve.apply(resolve,args); 
}; 
const resolve_alias = base.bind(null, 'src/client'); 
const aliases = [ 
    'actions', 
    'components', 
    'constants', 
    'containers', 
    'middleware', 
    'reducers', 
    'routes', 
    'store', 
    'styles', 
    'utils' 
]; 
const resolved_aliases = aliases.reduce(function(accumulator, directory){ 
    accumulator[directory] = resolve_alias(directory); 
    return accumulator; 
}, {}); 

const productionVendors = [ 
    'react', 
    'react-dom', 
    'react-router', 
    'redux', 
    'react-redux', 
    'redux-simple-router', 
    'classnames', 
    'underscore', 
    'history', 
    'immutable', 
    'object-assign', 
    'rx', 
    'slick-carousel', 
    'redux-actions' 
]; 

const developmentVendors = [ 
    'react', 
    'react-dom', 
    'react-router', 
    'redux', 
    'react-redux', 
    'redux-simple-router', 
    'classnames', 
    'underscore', 
    'history', 
    'immutable', 
    'redbox-react', 
    'object-assign', 
    'rx', 
    'slick-carousel', 
    'redux-actions' 
]; 

const devPlugins = [ 
    new webpack.HotModuleReplacementPlugin(), 
    new webpack.NoErrorsPlugin(), 
    new HtmlWebpackPlugin({ 
    templateContent: function(templateParams, webpackCompiler) { 
     return "<!DOCTYPE html><html><head lang='en'><meta charset='UTF-8'><meta name='viewport' content='width=device-width, initial-scale=1'><title>Busy - Better marketing for all.</title></head><body><div id='app'></div><script src='vendors.js'></script><script src='main.js'></script></body></html>" 
    } 
    }), 
    new BrowserSyncPlugin(
     { 
     host: 'localhost', 
     port: 7000, 
     proxy: 'http://localhost:8080/webpack-dev-server/' 
     }, 
     { 
     reload: false 
     } 
    ) 
] 

const productionPlugins = [ 
    new ExtractTextPlugin('[name].css'), 
    new webpack.IgnorePlugin(/\/config$/), 
    new webpack.DefinePlugin({ 
    'process.env': { 
     NODE_ENV: JSON.stringify(PRODUCTION) 
    } 
    }), 
    new webpack.optimize.DedupePlugin(), 
    new webpack.optimize.OccurenceOrderPlugin(), 
    new webpack.optimize.UglifyJsPlugin({ 
    compress: { 
     warnings: false 
    } 
    }) 
] 

const environment = process.env.NODE_ENV || DEVELOPMENT; 

var config = { 
    context: path.resolve(__dirname, '..'), 
    entry: { 
     main: 
     [ 
      'font-awesome-webpack!./src/client/theme/font-awesome.config.js', 
      path.resolve(__dirname, '..', 'src/client/index.js') 
     ] 
    }, 
    output: { 
    path: path.resolve(__dirname, '..', 'dist'), 
    pathInfo: true, 
    filename: '[name].js' 
    }, 
    module: { 
    preLoaders: [ 
     { 
     test: /\.js$/, 
     loader: "eslint-loader", 
     exclude: /node_modules/ 
     } 
    ], 
    loaders: [ 
     { 
     test: /src\/.+.js$/, 
     exclude: /node_modules/, 
     loader: 'babel', 
     query: { 
      presets: ['react', 'es2015', 'stage-0'] 
     } 
     }, 
     { test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "url-loader?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: /\.otf(\?v=\d+\.\d+\.\d+)?$/, loader: "url?limit=10000&mimetype=application/font-sfnt" }, 
     { test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: "url?mimetype=image/svg+xml" }, 
     { 
     test: /\.(jpe?g|gif|png)$/, 
     loader: 'url?25000' 
     }, 
     { 
     test: /\.mp4$/, 
     loader: 'file', 
     exclude: /node_modules/ 
     } 
    ] 
    }, 
    resolve: { 
    alias: resolved_aliases, 
    modulesDirectories: [ 
     'node_modules', 
     'src' 
    ] 
    }, 
    plugins: [ 
     new webpack.optimize.CommonsChunkPlugin('vendors', 'vendors.js') 
    ] 
}; 

// Vendor splitting 
config.entry.vendors = environment === DEVELOPMENT ? developmentVendors : productionVendors 

if (environment === DEVELOPMENT) { 
    config.plugins = config.plugins.concat(devPlugins); 
} else if (environment === PRODUCTION) { 
    config.plugins = config.plugins.concat(productionPlugins); 
} 

// Add development server entry points 
if (environment === DEVELOPMENT) { 
    config.entry.main.unshift('webpack-dev-server/client?http://localhost:8080', 'webpack/hot/dev-server'); 
} 

// Add dev tool configurations 
if (environment === DEVELOPMENT) { 
    config.devServer = { 
    hot: true, 
    host: 'localhost', 
    port: 8080, 
    proxy: {"/*": "http://localhost:3000"} 
    } 
} 

// Configure CSS and Sass loaders 
if (environment === DEVELOPMENT) { 
    config.module.loaders.push({ test: /.(scss|css)$/, loader: 'style!css?modules&importLoaders=3&localIdentName=[local]___[hash:base64:5]&sourceMap!autoprefixer?browsers=last 2 version!resolve-url!sass?sourceMapContents&outputStyle=expanded&sourceMap&includePaths[]=' + encodeURIComponent(require('node-bourbon').includePaths) + 
'&includePaths[]=' + encodeURIComponent(require('node-neat').includePaths[1]) + '&includePaths[]=' + path.resolve(__dirname, '..', 'src/client/') }); 
} else if (environment === PRODUCTION) { 
    config.module.loaders.push({ test: /.(scss|css)$/, loader: ExtractTextPlugin.extract('style','css?modules&importLoaders=3&localIdentName=[local]___[hash:base64:5]&sourceMap!autoprefixer?browsers=last 2 version!resolve-url!sass?sourceMap&includePaths[]=' + encodeURIComponent(require('node-bourbon').includePaths) + 
'&includePaths[]=' + encodeURIComponent(require('node-neat').includePaths[1]) + '&includePaths[]=' + path.resolve(__dirname, '..', 'src/client/')) }); 

}

// Configure devtool 
if (environment === DEVELOPMENT) { 
    config.devtool = 'inline-source-map'; 
} else if (environment === PRODUCTION) { 
    config.devtool = 'source-map'; 
} 

module.exports = config; 
+0

可能的重複:http://stackoverflow.com/questions/27588136/using-a-function-in-sass-is-returning-the-string-containing-the-name-of-the-func – cimmanon

+1

如何?這個問題是'在Sass中使用函數返回包含函數名稱而不是結果'的字符串,與webpack無關 – barndog

+0

考慮到你沒有顯示編譯後的輸出,我有充分的理由懷疑你'我沒有輸入必要的功能來完成這項工作。 – cimmanon

回答

5

儘量減少限100例如,這將防止的WebPack包含文件中的字體。

{ 
    test: /\.(woff2|woff|ttf|eot|svg|otf)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, 
    loaders: ["url-loader?limit=100&name=fonts/[name]_[hash].[ext]"] 
} 

上面的加載器會將您的字體文件複製到您的目標文件夾中的字體文件夾。

+0

我被困擾了4天的css大小,這是21Mb由於嵌入字體,現在減少到230Kb,即使沒有縮小。:) –