angular.js - webpack打包大型SPA配置问题
怪我咯
怪我咯 2017-05-15 17:13:06
0
3
808

目前项目是多页面应用。使用的是angular1.x 前端工具是Webpack。要转换成SPA。打算使用angular-ui-router做路由管理。
问题是把所有的文件打包成一个太大了。想法是只打包成一个第三个类库的vendor.js和某业务逻辑相关的app.js
比如登录页面我只想加载vendor.js和login.js登录后进到dashboard页面。我也只想加载vendor.js和dashborard.js。类似这样
请问该如何配置angular-ui-router和webpack呢,谢谢。

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(3)
phpcn_u1582

Assume your directory structure is like this

src
  - common
    - utils.js
    
  - login
    - index.js
    
  - dashboard 
    - index.js
    

Code Blocks

common/utils.js

// 通用模块,逻辑。
console.log('utils....')

login/index.js

require('./common/utils') // 引入公共模块

// 自己的业务模块
console.log('login....')

dashboard/index.js

require('./common/utils') // 引入公共模块

// 自己的业务模块
console.log('dashboard....')

To get the results you expect, webpack.config.js configure as follows:

var webapck = require('webpack')
module.exports = {
    entry:{
        login:'./src/login/index.js',
        dashboard:'./src/dashboard/index.js'
    },
    output:{
        publicPath:'/',
        path: __dirname + '/dist',
        filename:'js/[name].js',
        chunkFilename:'js/[id].js'
    },
    plugins:[
        new webpack.optimize.CommonsChunkPlugin({ 
           // 通过这个模块,就可以提取公共的模块 common/utils 
            name:'vendor',
            filename: '[name].js'
        })
    ]
}

The final result after packaging

 dist
    - js
        login.js
        dashboard.js
        
    vendor.js

As above. . .

刘奇

Reference code splitting

左手右手慢动作

I can now use webpack to generate the required js. How to introduce various js in combination with angular-ui-router? Thank you

I found an article about angular on-demand loading

http://www.cnblogs.com/ys-ys/..., based on ui-router, ocLazyLoad
seems to meet my needs. Thank you

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!