亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

webpack深入與實戰

難度中級
時長 3小時21分
學習人數
綜合評分9.60
259人評價 查看評價
9.8 內容實用
9.5 簡潔易懂
9.5 邏輯清晰
  • 查看全部
  • //?生成多個html
    const?path?=?require('path');
    const?HtmlWebpackPlugin?=?require('html-webpack-plugin');
    const?{?CleanWebpackPlugin?}?=?require('clean-webpack-plugin');
    
    module.exports?=?{
    ????entry:?{
    ????????app:?'./src/index.js',
    ????????print:?'./src/print.js',
    ????????a:?'./src/a.js',
    ????????b:?'./src/b.js',
    ????????c:?'./src/c.js',
    ????},
    ????output:?{
    ????????filename:?'js/[name].bundle.js',
    ????????path:?path.resolve(__dirname,?'dist'),
    ????????publicPath:?'http://cdn.com'
    ????},
    ????plugins:?[
    ????????new?CleanWebpackPlugin(),
    ????????new?HtmlWebpackPlugin({
    ????????????filename:?'a.html',
    ????????????//?html標題
    ????????????title:?'this?is?a.html',
    ????????????//?模板
    ????????????template:?'index.html',
    ????????????inject:?'head',
    ????????????//?引用的js
    ????????????chunks:?['a']
    ????????????//?minify:?{
    ????????????//?????removeComments:?false,
    ????????????//?????collapseWhitespace:?false
    ????????????//?}
    ????????}),
    ????????new?HtmlWebpackPlugin({
    ????????????filename:?'b.html',
    ????????????title:?'this?is?b.html',
    ????????????template:?'index.html',
    ????????????inject:?'head',
    ????????????chunks:?['b']
    ????????????//?minify:?{
    ????????????//?????removeComments:?false,
    ????????????//?????collapseWhitespace:?false
    ????????????//?}
    ????????}),
    ????????new?HtmlWebpackPlugin({
    ????????????filename:?'c.html',
    ????????????title:?'this?is?c.html',
    ????????????template:?'index.html',
    ????????????inject:?'head',
    ????????????chunks:?['c']
    ????????????//?壓縮
    ????????????//?minify:?{
    ????????????//?????removeComments:?false,
    ????????????//?????collapseWhitespace:?false
    ????????????//?}
    ????????})
    ????],
    };


    查看全部
  • const?path?=?require('path');
    const?HtmlWebpackPlugin?=?require('html-webpack-plugin');
    const?{?CleanWebpackPlugin?}?=?require('clean-webpack-plugin');
    
    module.exports?=?{
    ????entry:?{
    ????????app:?'./src/index.js',
    ????????print:?'./src/print.js'
    ????},
    ????plugins:?[
    ????????new?CleanWebpackPlugin(),
    ????????new?HtmlWebpackPlugin({
    ????????????title:?'Output?Management',
    ????????????inject:?'head',
    ????????????//?壓縮
    ????????????minify:?{
    ????????????????//?刪除注釋
    ????????????????removeComments:?true,
    ????????????????//?去除空格
    ????????????????collapseWhitespace:?true
    ????????????}
    ????????})
    ????],
    ????output:?{
    ????????filename:?'js/[name].bundle.js',
    ????????path:?path.resolve(__dirname,?'dist'),
    ????????publicPath:?'http://cdn.com'
    ????}
    };



    查看全部
  • const?path?=?require('path');
    const?HtmlWebpackPlugin?=?require('html-webpack-plugin');
    const?{?CleanWebpackPlugin?}?=?require('clean-webpack-plugin');
    
    module.exports?=?{
    ????entry:?{
    ????????app:?'./src/index.js',
    ????????print:?'./src/print.js'
    ????},
    ????plugins:?[
    ????????new?CleanWebpackPlugin(),
    ????????new?HtmlWebpackPlugin({
    ????????????title:?'Output?Management',
    ????????????inject:?'head'
    ????????})
    ????],
    ????output:?{
    ????????filename:?'js/[name].bundle.js',
    ????????path:?path.resolve(__dirname,?'dist'),
    ????????//?可以為生產環境地址,執行npm?run?build命令之后,dist目錄下?index.html中的js路徑會加上這個前綴
    ????????publicPath:?'http://cdn.com'
    ????}
    };


    查看全部
  • 自動化生成項目中的html頁面

    npm?install?--save-dev?html-webpack-plugin

    參考網址:

    (1)webpack插件列表:

    https://www.webpackjs.com/plugins/,

    (2)htmlWebpackPlugin插件介紹:

    https://www.webpackjs.com/plugins/html-webpack-plugin/

    (3)htmlWebpackPlugin插件配置:

    https://github.com/jantimon/html-webpack-plugin#configuration


    webpack.config.js

    const?path?=?require('path');
    const?HtmlWebpackPlugin?=?require('html-webpack-plugin');
    const?{?CleanWebpackPlugin?}?=?require('clean-webpack-plugin');
    
    module.exports?=?{
    ????entry:?{
    ????????app:?'./src/index.js',
    ????????print:?'./src/print.js'
    ????},
    ????plugins:?[
    ????????new?CleanWebpackPlugin(),
    ????????new?HtmlWebpackPlugin({
    ????????????title:?'Output?Management'
    ????????})
    ????],
    ????output:?{
    ????????//?加上js,可以使js和html分離開來
    ????????filename:?'js/[name].bundle.js',
    ????????path:?path.resolve(__dirname,?'dist')
    ????}
    };


    查看全部
  • 參考網址:https://www.webpackjs.com/guides/output-management/


    index.html

    <!doctype?html>
    <html>
    <head>
    ????<title>Output?Management</title>
    </head>
    <body>
    ????<script?src="./app.bundle.js"></script>
    </body>
    </html>


    index.js

    import?_?from?'lodash';
    import?printMe?from?'./print.js';
    
    function?component()?{
    ????var?element?=?document.createElement('div');
    ????var?btn?=?document.createElement('button');
    
    ????element.innerHTML?=?_.join(['Hello',?'webpack'],?'?');
    
    ????btn.innerHTML?=?'Click?me?and?check?the?console!';
    ????btn.onclick?=?printMe;
    ????element.appendChild(btn);
    
    ????return?element;
    }
    
    document.body.appendChild(component());


    webpack.config.js

    const?path?=?require('path');
    
    module.exports?=?{
    ????entry:?{
    ????????app:?'./src/index.js',
    ????????print:?'./src/print.js'
    ????},
    ????output:?{
    ????????filename:?'[name].bundle.js',
    ????????path:?path.resolve(__dirname,?'dist')
    ????}
    };


    print.js

    export?default?function?printMe()?{
    ????console.log('I?get?called?from?print.js!');
    }


    // 清理輸出目錄

    const?path?=?require('path');
    const?HtmlWebpackPlugin?=?require('html-webpack-plugin');
    const?{?CleanWebpackPlugin?}?=?require('clean-webpack-plugin');
    
    module.exports?=?{
    ????entry:?{
    ????????app:?'./src/index.js',
    ????????print:?'./src/print.js'
    ????},
    ????plugins:?[
    ????????new?CleanWebpackPlugin(),
    ????????new?HtmlWebpackPlugin({
    ????????????title:?'Output?Management'
    ????????})
    ????],
    ????output:?{
    ????????filename:?'[name].bundle.js',
    ????????path:?path.resolve(__dirname,?'dist')
    ????}
    };


    查看全部
  • package.json配置腳本

    {
    ??"name":?"webpack-demo",
    ??"version":?"1.0.0",
    ??"description":?"",
    ??"main":?"index.js",
    ??"scripts":?{
    ????"test":?"echo?\"Error:?no?test?specified\"?&&?exit?1",
    ????"build":?"webpack?--config?webpack.dev.config.js"
    ??},
    ??"keywords":?[],
    ??"author":?"",
    ??"license":?"ISC",
    ??"devDependencies":?{
    ????"css-loader":?"^3.4.2",
    ????"style-loader":?"^1.1.3",
    ????"webpack":?"^4.41.5",
    ????"webpack-cli":?"^3.3.10"
    ??},
    ??"dependencies":?{
    ????"lodash":?"^4.17.15"
    ??}
    }


    查看全部
  • // 指定config文件

    webpack?--config?web.dev.config.js

    npx?webpack?--config?webpack.dev.config.js


    查看全部
  • // webpack.config.js

    const?path?=?require('path');
    
    module.exports?=?{
    ????entry:?'./src/index.js',
    ????output:?{
    ????????filename:?'bundle.js',
    ????????path:?path.resolve(__dirname,?'dist')
    ????},
    ????module:?{
    ????????rules:?[
    ????????????//?加載css
    ????????????{
    ????????????????test:?/\.css$/,
    ????????????????use:?[
    ????????????????????'style-loader',
    ????????????????????'css-loader'
    ????????????????]
    ????????????},
    ????????????//?加載圖片
    ????????????{
    ????????????????test:?/\.(png|svg|jpg|gif)$/,
    ????????????????use:?[
    ????????????????????'file-loader'
    ????????????????]
    ????????????},
    ????????????//?加載字體
    ????????????{
    ????????????????test:?/\.(woff|woff2|eot|ttf|otf)$/,
    ????????????????use:?[
    ????????????????????'file-loader'
    ????????????????]
    ????????????}
    ????????]
    ????}
    };


    查看全部
  • //加載?CSS
    npm?install?--save-dev?style-loader?css-loader
    npm?run?build
    
    參考網址:https://www.webpackjs.com/guides/asset-management/
    查看全部
  • npm?init?-y
    npm?install?webpack?webpack-cli?--save-dev
    npx?webpack
    
    參考網址:https://www.webpackjs.com/guides/getting-started/
    查看全部
  • <%= %>? 有等號直接取值,沒有等號直接運行js代碼

    查看全部
  • css-loader使得webpack可以識別css文件

    style-loader可以把生成 一個style標簽并把css樣式插入進style標簽里面

    查看全部
  • webpack命令

    webpack hello.js hello.bundle.js

    查看全部
  • webpack.config.js配置:

    module.exports={

    ????entry: './src/script/main.js', // 打包的入口文件

    ????output: { // 打包后的文件

    ????????path: './dist/js', // 地址

    ????????filename: 'bundle.js' // 打包的文件名稱

    }? ?

    }

    查看全部

舉報

0/150
提交
取消
課程須知
1、對模塊化開發有一些了解 2、使用過 node 對 npm 有基本的了解 3、對打包有一個基本的概念
老師告訴你能學到什么?
1、模塊化的開發 2、webpack 的 CLI (命令行) 3、webpack 如何在項目中打包 4、webpack 如何處理項目中的資源文件

微信掃碼,參與3人拼團

微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

友情提示:

您好,此課程屬于遷移課程,您已購買該課程,無需重復購買,感謝您對慕課網的支持!