From d0f6d3f1f6bdfef66de3d3fa0ad3a431a7e9f175 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8A=B1=E8=A3=A4=E8=A1=A9?= Date: Thu, 31 May 2018 16:53:08 +0800 Subject: [PATCH] chore: use babel-plugin-dynamic-import-node to lazy-loading (#727) detail see https://panjiachen.github.io/vue-element-admin-site/#/zh-cn/lazy-loading?id=%E8%B7%AF%E7%94%B1%E6%87%92%E5%8A%A0%E8%BD%BD --- .babelrc | 7 +++- package.json | 3 +- src/router/index.js | 97 ++++++++++++++++++++++----------------------- 3 files changed, 55 insertions(+), 52 deletions(-) diff --git a/.babelrc b/.babelrc index 3a280ba3..6c0b7f27 100644 --- a/.babelrc +++ b/.babelrc @@ -8,5 +8,10 @@ }], "stage-2" ], - "plugins": ["transform-vue-jsx", "transform-runtime"] + "plugins": ["transform-vue-jsx", "transform-runtime"], + "env": { + "development":{ + "plugins": ["dynamic-import-node"] + } + } } diff --git a/package.json b/package.json index e27147c9..c5b5e190 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "author": "Pan ", "license": "MIT", "scripts": { - "dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js", + "dev": "cross-env BABEL_ENV=development webpack-dev-server --inline --progress --config build/webpack.dev.conf.js", "build:prod": "cross-env NODE_ENV=production env_config=prod node build/build.js", "build:sit": "cross-env NODE_ENV=production env_config=sit node build/build.js", "lint": "eslint --ext .js,.vue src", @@ -61,6 +61,7 @@ "babel-eslint": "8.0.3", "babel-helper-vue-jsx-merge-props": "2.0.3", "babel-loader": "7.1.2", + "babel-plugin-dynamic-import-node": "^1.2.0", "babel-plugin-syntax-jsx": "6.18.0", "babel-plugin-transform-runtime": "6.23.0", "babel-plugin-transform-vue-jsx": "3.5.0", diff --git a/src/router/index.js b/src/router/index.js index 72eb22db..cf3020cb 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -1,13 +1,10 @@ import Vue from 'vue' import Router from 'vue-router' -const _import = require('./_import_' + process.env.NODE_ENV) -// in development-env not use lazy-loading, because lazy-loading too many pages will cause webpack hot update too slow. so only in production use lazy-loading; -// detail: https://panjiachen.github.io/vue-element-admin-site/#/lazy-loading Vue.use(Router) /* Layout */ -import Layout from '../views/layout/Layout' +import Layout from '@/views/layout/Layout' /** note: submenu only apppear when children.length>=1 * detail see https://panjiachen.github.io/vue-element-admin-site/#/router-and-nav?id=sidebar @@ -28,17 +25,17 @@ import Layout from '../views/layout/Layout' } **/ export const constantRouterMap = [ - { path: '/login', component: _import('login/index'), hidden: true }, - { path: '/authredirect', component: _import('login/authredirect'), hidden: true }, - { path: '/404', component: _import('errorPage/404'), hidden: true }, - { path: '/401', component: _import('errorPage/401'), hidden: true }, + { path: '/login', component: () => import('@/views/login/index'), hidden: true }, + { path: '/authredirect', component: () => import('@/views/login/authredirect'), hidden: true }, + { path: '/404', component: () => import('@/views/errorPage/404'), hidden: true }, + { path: '/401', component: () => import('@/views/errorPage/401'), hidden: true }, { path: '', component: Layout, redirect: 'dashboard', children: [{ path: 'dashboard', - component: _import('dashboard/index'), + component: () => import('@/views/dashboard/index'), name: 'dashboard', meta: { title: 'dashboard', icon: 'dashboard', noCache: true } }] @@ -49,7 +46,7 @@ export const constantRouterMap = [ redirect: '/documentation/index', children: [{ path: 'index', - component: _import('documentation/index'), + component: () => import('@/views/documentation/index'), name: 'documentation', meta: { title: 'documentation', icon: 'documentation', noCache: true } }] @@ -60,7 +57,7 @@ export const constantRouterMap = [ redirect: '/guide/index', children: [{ path: 'index', - component: _import('guide/index'), + component: () => import('@/views/guide/index'), name: 'guide', meta: { title: 'guide', icon: 'guide', noCache: true } }] @@ -86,7 +83,7 @@ export const asyncRouterMap = [ }, children: [{ path: 'page', - component: _import('permission/page'), + component: () => import('@/views/permission/page'), name: 'pagePermission', meta: { title: 'pagePermission', @@ -94,7 +91,7 @@ export const asyncRouterMap = [ } }, { path: 'directive', - component: _import('permission/directive'), + component: () => import('@/views/permission/directive'), name: 'directivePermission', meta: { title: 'directivePermission' @@ -108,7 +105,7 @@ export const asyncRouterMap = [ component: Layout, children: [{ path: 'index', - component: _import('svg-icons/index'), + component: () => import('@/views/svg-icons/index'), name: 'icons', meta: { title: 'icons', icon: 'icon', noCache: true } }] @@ -124,19 +121,19 @@ export const asyncRouterMap = [ icon: 'component' }, children: [ - { path: 'tinymce', component: _import('components-demo/tinymce'), name: 'tinymce-demo', meta: { title: 'tinymce' }}, - { path: 'markdown', component: _import('components-demo/markdown'), name: 'markdown-demo', meta: { title: 'markdown' }}, - { path: 'json-editor', component: _import('components-demo/jsonEditor'), name: 'jsonEditor-demo', meta: { title: 'jsonEditor' }}, - { path: 'splitpane', component: _import('components-demo/splitpane'), name: 'splitpane-demo', meta: { title: 'splitPane' }}, - { path: 'avatar-upload', component: _import('components-demo/avatarUpload'), name: 'avatarUpload-demo', meta: { title: 'avatarUpload' }}, - { path: 'dropzone', component: _import('components-demo/dropzone'), name: 'dropzone-demo', meta: { title: 'dropzone' }}, - { path: 'sticky', component: _import('components-demo/sticky'), name: 'sticky-demo', meta: { title: 'sticky' }}, - { path: 'count-to', component: _import('components-demo/countTo'), name: 'countTo-demo', meta: { title: 'countTo' }}, - { path: 'mixin', component: _import('components-demo/mixin'), name: 'componentMixin-demo', meta: { title: 'componentMixin' }}, - { path: 'back-to-top', component: _import('components-demo/backToTop'), name: 'backToTop-demo', meta: { title: 'backToTop' }}, - { path: 'drag-dialog', component: _import('components-demo/dragDialog'), name: 'dragDialog-demo', meta: { title: 'dragDialog' }}, - { path: 'dnd-list', component: _import('components-demo/dndList'), name: 'dndList-demo', meta: { title: 'dndList' }}, - { path: 'drag-kanban', component: _import('components-demo/dragKanban'), name: 'dragKanban-demo', meta: { title: 'dragKanban' }} + { path: 'tinymce', component: () => import('@/views/components-demo/tinymce'), name: 'tinymce-demo', meta: { title: 'tinymce' }}, + { path: 'markdown', component: () => import('@/views/components-demo/markdown'), name: 'markdown-demo', meta: { title: 'markdown' }}, + { path: 'json-editor', component: () => import('@/views/components-demo/jsonEditor'), name: 'jsonEditor-demo', meta: { title: 'jsonEditor' }}, + { path: 'splitpane', component: () => import('@/views/components-demo/splitpane'), name: 'splitpane-demo', meta: { title: 'splitPane' }}, + { path: 'avatar-upload', component: () => import('@/views/components-demo/avatarUpload'), name: 'avatarUpload-demo', meta: { title: 'avatarUpload' }}, + { path: 'dropzone', component: () => import('@/views/components-demo/dropzone'), name: 'dropzone-demo', meta: { title: 'dropzone' }}, + { path: 'sticky', component: () => import('@/views/components-demo/sticky'), name: 'sticky-demo', meta: { title: 'sticky' }}, + { path: 'count-to', component: () => import('@/views/components-demo/countTo'), name: 'countTo-demo', meta: { title: 'countTo' }}, + { path: 'mixin', component: () => import('@/views/components-demo/mixin'), name: 'componentMixin-demo', meta: { title: 'componentMixin' }}, + { path: 'back-to-top', component: () => import('@/views/components-demo/backToTop'), name: 'backToTop-demo', meta: { title: 'backToTop' }}, + { path: 'drag-dialog', component: () => import('@/views/components-demo/dragDialog'), name: 'dragDialog-demo', meta: { title: 'dragDialog' }}, + { path: 'dnd-list', component: () => import('@/views/components-demo/dndList'), name: 'dndList-demo', meta: { title: 'dndList' }}, + { path: 'drag-kanban', component: () => import('@/views/components-demo/dragKanban'), name: 'dragKanban-demo', meta: { title: 'dragKanban' }} ] }, @@ -150,9 +147,9 @@ export const asyncRouterMap = [ icon: 'chart' }, children: [ - { path: 'keyboard', component: _import('charts/keyboard'), name: 'keyboardChart', meta: { title: 'keyboardChart', noCache: true }}, - { path: 'line', component: _import('charts/line'), name: 'lineChart', meta: { title: 'lineChart', noCache: true }}, - { path: 'mixchart', component: _import('charts/mixChart'), name: 'mixChart', meta: { title: 'mixChart', noCache: true }} + { path: 'keyboard', component: () => import('@/views/charts/keyboard'), name: 'keyboardChart', meta: { title: 'keyboardChart', noCache: true }}, + { path: 'line', component: () => import('@/views/charts/line'), name: 'lineChart', meta: { title: 'lineChart', noCache: true }}, + { path: 'mixchart', component: () => import('@/views/charts/mixChart'), name: 'mixChart', meta: { title: 'mixChart', noCache: true }} ] }, @@ -161,7 +158,7 @@ export const asyncRouterMap = [ component: Layout, children: [{ path: 'index', - component: _import('tab/index'), + component: () => import('@/views/tab/index'), name: 'tab', meta: { title: 'tab', icon: 'tab' } }] @@ -177,12 +174,12 @@ export const asyncRouterMap = [ icon: 'table' }, children: [ - { path: 'dynamic-table', component: _import('table/dynamicTable/index'), name: 'dynamicTable', meta: { title: 'dynamicTable' }}, - { path: 'drag-table', component: _import('table/dragTable'), name: 'dragTable', meta: { title: 'dragTable' }}, - { path: 'inline-edit-table', component: _import('table/inlineEditTable'), name: 'inlineEditTable', meta: { title: 'inlineEditTable' }}, - { path: 'tree-table', component: _import('table/treeTable/treeTable'), name: 'treeTableDemo', meta: { title: 'treeTable' }}, - { path: 'custom-tree-table', component: _import('table/treeTable/customTreeTable'), name: 'customTreeTableDemo', meta: { title: 'customTreeTable' }}, - { path: 'complex-table', component: _import('table/complexTable'), name: 'complexTable', meta: { title: 'complexTable' }} + { path: 'dynamic-table', component: () => import('@/views/table/dynamicTable/index'), name: 'dynamicTable', meta: { title: 'dynamicTable' }}, + { path: 'drag-table', component: () => import('@/views/table/dragTable'), name: 'dragTable', meta: { title: 'dragTable' }}, + { path: 'inline-edit-table', component: () => import('@/views/table/inlineEditTable'), name: 'inlineEditTable', meta: { title: 'inlineEditTable' }}, + { path: 'tree-table', component: () => import('@/views/table/treeTable/treeTable'), name: 'treeTableDemo', meta: { title: 'treeTable' }}, + { path: 'custom-tree-table', component: () => import('@/views/table/treeTable/customTreeTable'), name: 'customTreeTableDemo', meta: { title: 'customTreeTable' }}, + { path: 'complex-table', component: () => import('@/views/table/complexTable'), name: 'complexTable', meta: { title: 'complexTable' }} ] }, @@ -196,9 +193,9 @@ export const asyncRouterMap = [ icon: 'example' }, children: [ - { path: 'create', component: _import('example/create'), name: 'createArticle', meta: { title: 'createArticle', icon: 'edit' }}, - { path: 'edit/:id(\\d+)', component: _import('example/edit'), name: 'editArticle', meta: { title: 'editArticle', noCache: true }, hidden: true }, - { path: 'list', component: _import('example/list'), name: 'articleList', meta: { title: 'articleList', icon: 'list' }} + { path: 'create', component: () => import('@/views/example/create'), name: 'createArticle', meta: { title: 'createArticle', icon: 'edit' }}, + { path: 'edit/:id(\\d+)', component: () => import('@/views/example/edit'), name: 'editArticle', meta: { title: 'editArticle', noCache: true }, hidden: true }, + { path: 'list', component: () => import('@/views/example/list'), name: 'articleList', meta: { title: 'articleList', icon: 'list' }} ] }, @@ -212,8 +209,8 @@ export const asyncRouterMap = [ icon: '404' }, children: [ - { path: '401', component: _import('errorPage/401'), name: 'page401', meta: { title: 'page401', noCache: true }}, - { path: '404', component: _import('errorPage/404'), name: 'page404', meta: { title: 'page404', noCache: true }} + { path: '401', component: () => import('@/views/errorPage/401'), name: 'page401', meta: { title: 'page401', noCache: true }}, + { path: '404', component: () => import('@/views/errorPage/404'), name: 'page404', meta: { title: 'page404', noCache: true }} ] }, @@ -221,7 +218,7 @@ export const asyncRouterMap = [ path: '/error-log', component: Layout, redirect: 'noredirect', - children: [{ path: 'log', component: _import('errorLog/index'), name: 'errorLog', meta: { title: 'errorLog', icon: 'bug' }}] + children: [{ path: 'log', component: () => import('@/views/errorLog/index'), name: 'errorLog', meta: { title: 'errorLog', icon: 'bug' }}] }, { @@ -234,9 +231,9 @@ export const asyncRouterMap = [ icon: 'excel' }, children: [ - { path: 'export-excel', component: _import('excel/exportExcel'), name: 'exportExcel', meta: { title: 'exportExcel' }}, - { path: 'export-selected-excel', component: _import('excel/selectExcel'), name: 'selectExcel', meta: { title: 'selectExcel' }}, - { path: 'upload-excel', component: _import('excel/uploadExcel'), name: 'uploadExcel', meta: { title: 'uploadExcel' }} + { path: 'export-excel', component: () => import('@/views/excel/exportExcel'), name: 'exportExcel', meta: { title: 'exportExcel' }}, + { path: 'export-selected-excel', component: () => import('@/views/excel/selectExcel'), name: 'selectExcel', meta: { title: 'selectExcel' }}, + { path: 'upload-excel', component: () => import('@/views/excel/uploadExcel'), name: 'uploadExcel', meta: { title: 'uploadExcel' }} ] }, @@ -246,27 +243,27 @@ export const asyncRouterMap = [ redirect: '/zip/download', alwaysShow: true, meta: { title: 'zip', icon: 'zip' }, - children: [{ path: 'download', component: _import('zip/index'), name: 'exportZip', meta: { title: 'exportZip' }}] + children: [{ path: 'download', component: () => import('@/views/zip/index'), name: 'exportZip', meta: { title: 'exportZip' }}] }, { path: '/theme', component: Layout, redirect: 'noredirect', - children: [{ path: 'index', component: _import('theme/index'), name: 'theme', meta: { title: 'theme', icon: 'theme' }}] + children: [{ path: 'index', component: () => import('@/views/theme/index'), name: 'theme', meta: { title: 'theme', icon: 'theme' }}] }, { path: '/clipboard', component: Layout, redirect: 'noredirect', - children: [{ path: 'index', component: _import('clipboard/index'), name: 'clipboardDemo', meta: { title: 'clipboardDemo', icon: 'clipboard' }}] + children: [{ path: 'index', component: () => import('@/views/clipboard/index'), name: 'clipboardDemo', meta: { title: 'clipboardDemo', icon: 'clipboard' }}] }, { path: '/i18n', component: Layout, - children: [{ path: 'index', component: _import('i18n-demo/index'), name: 'i18n', meta: { title: 'i18n', icon: 'international' }}] + children: [{ path: 'index', component: () => import('@/views/i18n-demo/index'), name: 'i18n', meta: { title: 'i18n', icon: 'international' }}] }, { path: '*', redirect: '/404', hidden: true }