nodejs+webpack+vue之webpack

來源:互聯網
上載者:User

標籤:plugin   hash   完整   基於   常用   rman   use   檔案   連接埠   

webpack是一款模組載入器兼打包工具,把JS、樣式,圖片都作為模組來使用和處理。項目下有個設定檔webpack.config.js,用來告訴webpack需要做什麼,本項目的webpack.config.js檔案內容如下:

引入模組:

var path = require(‘path‘)var webpack = require(‘webpack‘)

入口檔案及輸出檔案配置:

通過entry節點可以配置入口檔案,且支援多個入口檔案,每個模板只會有一個入口檔案。

通過oupput節點可以配置輸出檔案,輸出檔案的檔案名稱是根據入口檔案來配置的,[name]的相應內容是entry節點下對應該的key,如下,則會輸出/dist/builg.index.js和dist/build.user.js兩個檔案。

module.exports = {
//入口檔案 entry: { index:
‘./src/index.js‘, user:‘./src/user.js‘ },
//輸出檔案 output: { path: path.resolve(__dirname,
‘./dist‘), publicPath: ‘/dist/‘, filename: ‘build.[name].js‘ }}

模組載入器:

它告知webpack每一種檔案都需要使用什麼載入器來處理,常用的載入器有style-loader,css-loader,sass-loader,url-loader;

module.exports = {    module: {    rules: [      {
//把vue檔案輸出成組件 test:
/\.vue$/, loader: ‘vue-loader‘, options: { loaders: { ‘scss‘: ExtractTextPlugin.extract(‘vue-style-loader!css-loader!sass-loader‘), ‘sass‘: ExtractTextPlugin.extract(‘vue-style-loader!css-loader!sass-loader?indentedSyntax‘) } } }, {
     //將所有目錄下的es6代碼轉譯為es5代碼,但不包含node_modules目錄下的檔案 test:
/\.js$/, loader: ‘babel-loader‘, exclude: /node_modules/ }, {
     //用於打包圖片 test:
/\.(png|jpg|gif|svg)$/, loader: ‘file-loader‘, options: { name: ‘[name].[ext]?[hash]‘ } }, {
     //把scss檔案轉譯成css檔案 test:
/\.scss$/, loader:ExtractTextPlugin.extract({ fallbackLoader: ‘style-loader‘, loader: ‘css-loader!sass-loader‘ }) } ] }}

別名設定:

設定別名後,就不需要寫真實的路徑,只需用別名代替,如:require(‘httpHelper‘)。

resolve: {    alias: {      ‘vue$‘: ‘vue/dist/vue.common.js‘,
    ‘httpHelper‘: ‘src/util/httpHelper.js‘
}}

外掛程式配置:

extract-text-webpack-plugin外掛程式,不把樣式打包到指令碼中,而是獨立打包樣式檔案,產生新的css檔案。

//樣式打包var ExtractTextPlugin = require(‘extract-text-webpack-plugin‘);

plugins: [
new ExtractTextPlugin("./[name].css")
]

構建本機伺服器:

該伺服器基於node.js構建,可讓瀏覽器監測代碼的修改,並自動重新整理,預設連接埠為‘8080’。

安裝:npm install --save-dev webpack-dev-server

devServer: {    historyApiFallback: true,    noInfo: true}

判斷是生產環境還是開發環境:

以下代碼中NODE_ENV的配置是與package.json檔案下的scripts節點下的內容相關,module.exports.plugins裡的外掛程式配置會合并到plugins:[new ExtractTextPlugin("./[name].css")]中。

if (process.env.NODE_ENV === ‘production‘) {  module.exports.devtool = ‘#source-map‘   module.exports.plugins = (module.exports.plugins || []).concat([    new webpack.DefinePlugin({      ‘process.env‘: {        NODE_ENV: ‘"production"‘      }    }),    new webpack.optimize.UglifyJsPlugin({      sourceMap: true,      compress: {        warnings: false      }    }),    new webpack.LoaderOptionsPlugin({      minimize: true    })  ])}

完整代碼如下:

var path = require(‘path‘)var webpack = require(‘webpack‘)
//樣式打包
var ExtractTextPlugin = require(‘extract-text-webpack-plugin‘)module.exports = {
//入口檔案配置 entry: { index:
‘./src/index.js‘, user:‘./src/user.js‘ },
//輸出檔案配置 output: { path: path.resolve(__dirname,
‘./dist‘), publicPath: ‘/dist/‘, filename: ‘build.[name].js‘ }, module: { rules: [ {
     //把vue檔案輸出成組件 test:
/\.vue$/, loader: ‘vue-loader‘, options: { loaders: { ‘scss‘: ExtractTextPlugin.extract(‘vue-style-loader!css-loader!sass-loader‘), ‘sass‘: ExtractTextPlugin.extract(‘vue-style-loader!css-loader!sass-loader?indentedSyntax‘) } } }, {
     //將所有目錄下的es6代碼轉譯為es5代碼,但不包含node_modules目錄下的檔案 test:
/\.js$/, loader: ‘babel-loader‘, exclude: /node_modules/ }, {
     //用於打包圖片 test:
/\.(png|jpg|gif|svg)$/, loader: ‘file-loader‘, options: { name: ‘[name].[ext]?[hash]‘ } }, {
    //把scss檔案轉譯成css檔案 test:
/\.scss$/, loader:ExtractTextPlugin.extract({ fallbackLoader: ‘style-loader‘, loader: ‘css-loader!sass-loader‘ }) } ] }, resolve: {
  //設定別名 alias: {
‘vue$‘: ‘vue/dist/vue.common.js‘ } }, plugins: [
//樣式輸出路徑
new ExtractTextPlugin("./[name].css") ], devServer: { historyApiFallback: true, noInfo: true }, performance: { hints: false }, devtool: ‘#eval-source-map‘}if (process.env.NODE_ENV === ‘production‘) { module.exports.devtool = ‘#source-map‘ module.exports.plugins = (module.exports.plugins || []).concat([ new webpack.DefinePlugin({ ‘process.env‘: { NODE_ENV: ‘"production"‘ } }), new webpack.optimize.UglifyJsPlugin({ sourceMap: true, compress: { warnings: false } }), new webpack.LoaderOptionsPlugin({ minimize: true }) ])}

 

nodejs+webpack+vue之webpack

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.