| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 859d905 commit bf9c8f1
10 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,6 +12,7 @@ export interface BuildOptions { | |||
| 12 | 12 | baseHref?: string; | |
| 13 | 13 | aot?: boolean; | |
| 14 | 14 | sourcemap?: boolean; | |
| 15 | + vendorChunk?: boolean; | ||
| 15 | 16 | } | |
| 16 | 17 | ||
| 17 | 18 | const BuildCommand = Command.extend({ | |
@@ -33,7 +34,8 @@ const BuildCommand = Command.extend({ | |||
| 33 | 34 | { name: 'suppress-sizes', type: Boolean, default: false }, | |
| 34 | 35 | { name: 'base-href', type: String, default: null, aliases: ['bh'] }, | |
| 35 | 36 | { name: 'aot', type: Boolean, default: false }, | |
| 36 | - { name: 'sourcemap', type: Boolean, default: true, aliases: ['sm'] } | ||
| 37 | + { name: 'sourcemap', type: Boolean, default: true, aliases: ['sm'] }, | ||
| 38 | + { name: 'vendor-chunk', type: Boolean, default: true } | ||
| 37 | 39 | ], | |
| 38 | 40 | ||
| 39 | 41 | run: function (commandOptions: BuildOptions) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -28,6 +28,7 @@ export interface ServeTaskOptions { | |||
| 28 | 28 | aot?: boolean; | |
| 29 | 29 | sourcemap?: boolean; | |
| 30 | 30 | open?: boolean; | |
| 31 | + vendorChunk?: boolean; | ||
| 31 | 32 | } | |
| 32 | 33 | ||
| 33 | 34 | const ServeCommand = Command.extend({ | |
@@ -83,6 +84,7 @@ const ServeCommand = Command.extend({ | |||
| 83 | 84 | { name: 'ssl-cert', type: String, default: 'ssl/server.crt' }, | |
| 84 | 85 | { name: 'aot', type: Boolean, default: false }, | |
| 85 | 86 | { name: 'sourcemap', type: Boolean, default: true, aliases: ['sm'] }, | |
| 87 | + { name: 'vendor-chunk', type: Boolean, default: true }, | ||
| 86 | 88 | { | |
| 87 | 89 | name: 'open', | |
| 88 | 90 | type: Boolean, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,7 @@ | |||
| 1 | 1 | import * as webpack from 'webpack'; | |
| 2 | 2 | import * as path from 'path'; | |
| 3 | 3 | import {GlobCopyWebpackPlugin} from '../plugins/glob-copy-webpack-plugin'; | |
| 4 | + import {packageChunkSort} from '../utilities/package-chunk-sort'; | ||
| 4 | 5 | import {BaseHrefWebpackPlugin} from '@angular-cli/base-href-webpack'; | |
| 5 | 6 | ||
| 6 | 7 | const HtmlWebpackPlugin = require('html-webpack-plugin'); | |
@@ -12,17 +13,20 @@ export function getWebpackCommonConfig( | |||
| 12 | 13 | environment: string, | |
| 13 | 14 | appConfig: any, | |
| 14 | 15 | baseHref: string, | |
| 15 | - sourcemap: boolean | ||
| 16 | + sourcemap: boolean, | ||
| 17 | + vendorChunk: boolean | ||
| 16 | 18 | ) { | |
| 17 | 19 | ||
| 18 | 20 | const appRoot = path.resolve(projectRoot, appConfig.root); | |
| 19 | 21 | const appMain = path.resolve(appRoot, appConfig.main); | |
| 22 | + const nodeModules = path.resolve(projectRoot, 'node_modules'); | ||
| 20 | 23 | const styles = appConfig.styles | |
| 21 | 24 | ? appConfig.styles.map((style: string) => path.resolve(appRoot, style)) | |
| 22 | 25 | : []; | |
| 23 | 26 | const scripts = appConfig.scripts | |
| 24 | 27 | ? appConfig.scripts.map((script: string) => path.resolve(appRoot, script)) | |
| 25 | 28 | : []; | |
| 29 | + const extraPlugins: any[] = []; | ||
| 26 | 30 | ||
| 27 | 31 | let entry: { [key: string]: string[] } = { | |
| 28 | 32 | main: [appMain] | |
@@ -32,11 +36,19 @@ export function getWebpackCommonConfig( | |||
| 32 | 36 | if (appConfig.styles.length > 0) { entry['styles'] = styles; } | |
| 33 | 37 | if (appConfig.scripts.length > 0) { entry['scripts'] = scripts; } | |
| 34 | 38 | ||
| 39 | + if (vendorChunk) { | ||
| 40 | + extraPlugins.push(new webpack.optimize.CommonsChunkPlugin({ | ||
| 41 | + name: 'vendor', | ||
| 42 | + chunks: ['main'], | ||
| 43 | + minChunks: (module: any) => module.userRequest && module.userRequest.startsWith(nodeModules) | ||
| 44 | + })); | ||
| 45 | + } | ||
| 46 | + | ||
| 35 | 47 | return { | |
| 36 | 48 | devtool: sourcemap ? 'source-map' : false, | |
| 37 | 49 | resolve: { | |
| 38 | 50 | extensions: ['.ts', '.js'], | |
| 39 | - modules: [path.resolve(projectRoot, 'node_modules')] | ||
| 51 | + modules: [nodeModules] | ||
| 40 | 52 | }, | |
| 41 | 53 | context: path.resolve(__dirname, './'), | |
| 42 | 54 | entry: entry, | |
@@ -52,9 +64,7 @@ export function getWebpackCommonConfig( | |||
| 52 | 64 | enforce: 'pre', | |
| 53 | 65 | test: /\.js$/, | |
| 54 | 66 | loader: 'source-map-loader', | |
| 55 | - exclude: [ | ||
| 56 | - /node_modules/ | ||
| 57 | - ] | ||
| 67 | + exclude: [ nodeModules ] | ||
| 58 | 68 | }, | |
| 59 | 69 | // in main, load css as raw text | |
| 60 | 70 | { | |
@@ -91,7 +101,7 @@ export function getWebpackCommonConfig( | |||
| 91 | 101 | new HtmlWebpackPlugin({ | |
| 92 | 102 | template: path.resolve(appRoot, appConfig.index), | |
| 93 | 103 | filename: path.resolve(appConfig.outDir, appConfig.index), | |
| 94 | - chunksSortMode: 'dependency' | ||
| 104 | + chunksSortMode: packageChunkSort(['inline', 'styles', 'scripts', 'vendor', 'main']) | ||
| 95 | 105 | }), | |
| 96 | 106 | new BaseHrefWebpackPlugin({ | |
| 97 | 107 | baseHref: baseHref | |
@@ -104,10 +114,6 @@ export function getWebpackCommonConfig( | |||
| 104 | 114 | .replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&')), | |
| 105 | 115 | path.resolve(appRoot, appConfig.environments[environment]) | |
| 106 | 116 | ), | |
| 107 | - new webpack.optimize.CommonsChunkPlugin({ | ||
| 108 | - // Optimizing ensures loading order in index.html | ||
| 109 | - name: ['styles', 'scripts', 'main'].reverse() | ||
| 110 | - }), | ||
| 111 | 117 | new webpack.optimize.CommonsChunkPlugin({ | |
| 112 | 118 | minChunks: Infinity, | |
| 113 | 119 | name: 'inline' | |
@@ -121,8 +127,8 @@ export function getWebpackCommonConfig( | |||
| 121 | 127 | options: { | |
| 122 | 128 | postcss: [ autoprefixer() ] | |
| 123 | 129 | }, | |
| 124 | - }), | ||
| 125 | - ], | ||
| 130 | + }) | ||
| 131 | + ].concat(extraPlugins), | ||
| 126 | 132 | node: { | |
| 127 | 133 | fs: 'empty', | |
| 128 | 134 | global: true, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -25,6 +25,7 @@ export class NgCliWebpackConfig { | |||
| 25 | 25 | baseHref?: string, | |
| 26 | 26 | isAoT = false, | |
| 27 | 27 | sourcemap = true, | |
| 28 | + vendorChunk = false, | ||
| 28 | 29 | ) { | |
| 29 | 30 | const config: CliConfig = CliConfig.fromProject(); | |
| 30 | 31 | const appConfig = config.config.apps[0]; | |
@@ -36,7 +37,8 @@ export class NgCliWebpackConfig { | |||
| 36 | 37 | environment, | |
| 37 | 38 | appConfig, | |
| 38 | 39 | baseHref, | |
| 39 | - sourcemap | ||
| 40 | + sourcemap, | ||
| 41 | + vendorChunk | ||
| 40 | 42 | ); | |
| 41 | 43 | let targetConfigPartial = this.getTargetConfig(this.ngCliProject.root, appConfig); | |
| 42 | 44 | const typescriptConfigPartial = isAoT | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -25,7 +25,8 @@ export default Task.extend({ | |||
| 25 | 25 | outputDir, | |
| 26 | 26 | runTaskOptions.baseHref, | |
| 27 | 27 | runTaskOptions.aot, | |
| 28 | - runTaskOptions.sourcemap | ||
| 28 | + runTaskOptions.sourcemap, | ||
| 29 | + runTaskOptions.vendorChunk | ||
| 29 | 30 | ).config; | |
| 30 | 31 | const webpackCompiler: any = webpack(config); | |
| 31 | 32 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -24,7 +24,8 @@ export default <any>Task.extend({ | |||
| 24 | 24 | outputDir, | |
| 25 | 25 | runTaskOptions.baseHref, | |
| 26 | 26 | runTaskOptions.aot, | |
| 27 | - runTaskOptions.sourcemap | ||
| 27 | + runTaskOptions.sourcemap, | ||
| 28 | + runTaskOptions.vendorChunk | ||
| 28 | 29 | ).config; | |
| 29 | 30 | ||
| 30 | 31 | const webpackCompiler: any = webpack(config); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -27,7 +27,8 @@ export default Task.extend({ | |||
| 27 | 27 | undefined, | |
| 28 | 28 | undefined, | |
| 29 | 29 | commandOptions.aot, | |
| 30 | - commandOptions.sourcemap | ||
| 30 | + commandOptions.sourcemap, | ||
| 31 | + commandOptions.vendorChunk | ||
| 31 | 32 | ).config; | |
| 32 | 33 | ||
| 33 | 34 | // This allows for live reload of page when changes are made to repo. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,17 @@ | |||
| 1 | + export function packageChunkSort(packages: string[]) { | ||
| 2 | + return function sort(left: any, right: any) { | ||
| 3 | + let leftIndex = packages.indexOf(left.names[0]); | ||
| 4 | + let rightindex = packages.indexOf(right.names[0]); | ||
| 5 | + | ||
| 6 | + if ( leftIndex < 0 || rightindex < 0) { | ||
| 7 | + // Unknown packages are loaded last | ||
| 8 | + return 1; | ||
| 9 | + } | ||
| 10 | + | ||
| 11 | + if (leftIndex > rightindex) { | ||
| 12 | + return 1; | ||
| 13 | + } | ||
| 14 | + | ||
| 15 | + return -1; | ||
| 16 | + }; | ||
| 17 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,11 @@ | |||
| 1 | + import {ng} from '../../utils/process'; | ||
| 2 | + import {expectFileToExist} from '../../utils/fs'; | ||
| 3 | + import {expectToFail} from '../../utils/utils'; | ||
| 4 | + | ||
| 5 | + | ||
| 6 | + export default function() { | ||
| 7 | + return ng('build') | ||
| 8 | + .then(() => expectFileToExist('dist/vendor.bundle.js')) | ||
| 9 | + .then(() => ng('build', '--no-vendor-chunk')) | ||
| 10 | + .then(() => expectToFail(() => expectFileToExist('dist/vendor.bundle.js'))); | ||
| 11 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -25,6 +25,7 @@ export default function() { | |||
| 25 | 25 | <script type="text/javascript" src="inline.bundle.js"></script> | |
| 26 | 26 | <script type="text/javascript" src="styles.bundle.js"></script> | |
| 27 | 27 | <script type="text/javascript" src="scripts.bundle.js"></script> | |
| 28 | + <script type="text/javascript" src="vendor.bundle.js"></script> | ||
| 28 | 29 | <script type="text/javascript" src="main.bundle.js"></script> | |
| 29 | 30 | `)); | |
| 30 | 31 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments