GVKun编程网logo

如何在angular 2应用程序上使用webpack进行代码分割?(angularjs webpack)

15

对于想了解如何在angular2应用程序上使用webpack进行代码分割?的读者,本文将提供新的信息,我们将详细介绍angularjswebpack,并且为您提供关于#使用webpack进行版本管理工

对于想了解如何在angular 2应用程序上使用webpack进行代码分割?的读者,本文将提供新的信息,我们将详细介绍angularjs webpack,并且为您提供关于# 使用webpack进行版本管理工具(webpack-plugin-auto-version)、angular – 使用Webpack时出错、angular – 在angluar2 webpack中使用Font-awesome、angular – 如何解决module.id应该是使用webpack2的字符串错误的有价值信息。

本文目录一览:

如何在angular 2应用程序上使用webpack进行代码分割?(angularjs webpack)

如何在angular 2应用程序上使用webpack进行代码分割?(angularjs webpack)

我有这样的应用程序结构:
├── /dashboard
│   ├── dashboard.css
│   ├── dashboard.html
│   ├── dashboard.component.ts
│   ├── dashboard.service.ts
│   ├── index.ts
├── /users
│   ├── users.css
│   ├── users.html
│   ├── users.component.ts
│   ├── users.service.ts
│   ├── index.ts
├── /login
│   ├── login.css
│   ├── login.html
│   ├── login.component.ts
│   ├── login.service.ts
│   ├── index.ts
├── app.component.ts
├── app.module.ts
├── app.routes.ts
├── app.styles.css

我想将我的应用程序代码拆分成这样的代码:

├── dashboard.js
├── users.js
├── login.js
├── app.js

我似乎无法找到一个如何用webpack做这个的例子.所以2个问题.可以这样做吗?而且,如何做到这一点?

任何线索或帮助将不胜感激.我整个上午一直在研究这个问题.

Angular文档建议它为here,但没有我能找到的示例或教程.所以这是可能的,但没有人知道该怎么做?

你可以找到webpack配置here

您必须将每一个作为切入点
entry: {
  'dashboard': './src/dashboard/index.ts','users': './src/users/index.ts','login': './src/login/index.ts','app': './src/app.module.ts'
}

然后确保没有代码在不同的入口点重复设置它们在commons chunk插件中.该订单是应用程序中遇到的重要代码,随后在仪表板中也很重要,或者用户只会显示在其中存在/需要的最后一个代码中.

plugins: [
    new webpack.optimize.CommonsChunkPlugin({
       name: ['app','dashboard','login','users'] 
    })
]

你也可以从这里获得一些灵感:
https://angular.io/docs/ts/latest/guide/webpack.html#!#common-configuration

# 使用webpack进行版本管理工具(webpack-plugin-auto-version)

# 使用webpack进行版本管理工具(webpack-plugin-auto-version)

使用webpack进行版本管理工具(webpack-plugin-auto-version)

出处: https://luoyangfu.com/detail/...

  • GitHub [webpack-plugin-auto-version] https://github.com/zsirfs/web...
  • npm [webpack-plugin-auto-version] https://www.npmjs.com/package...
  • issue https://github.com/zsirfs/web...





距离1.2.3版本以及好几个月了,当时是因为在前端开发中经常会因为缓冲问题,导致最新版本和线上版本或者测试版本出现不同,bug得不到及时有效的修复,手动修改版本可以,但是费时费力,容易出错,设计到动态加载的位置,动态创建脚本的时候就会出错,所以有效的版本管理工具就是能够有效的利用version,自动的根据package.json中version更换major,minor,patch版本。看效果图:
https://cdn-cos.luoyangfu.com/2018-08-01/image/15326711404526.jpg

插件提供配置项

{
    // 文件名替换标记 [version] -> v1.2.2
      filenameMark: options.filenameMark,
      // 版权名称
      copyright: options.copyright || ''[webpack-plugin-auto-version]'',
      // 保存的时候格式化package.json的indent
      space: options.space || 2,
      // 是否自动清理老版本
      cleanup: options.cleanup || false,
      // 是否检测资源内的标签
      inspectContent: options.inspectContent || !!options.template,
      // 自定义资源内版本替换模板 [VERSION]version[/VERSION]
      template: options.template || `[${this.copyright}]version[/${this.copyright}]`,
      // 自定义忽略后缀,默认是[''.html'']忽略html文件打入版本文件夹
      ignoreSuffix: [], // 忽略的后缀或者文件关键词
      isAsyncJs: false,
      htmlTempSuffix: [''.html'', ''.vm'', ''.ejs'', ''.handlbars'']
}
  • filename: 文件名称标记
  • copyright: 版权声明会出现在js,css,html文件头部

https://cdn-cos.luoyangfu.com/2018-08-01/image/15326713793121.jpg

  • space: 版本回存到package.json中的时候格式化间距默认为2字符(之后版本会默认读取.editconfig)
  • cleanup: 自动清除

https://cdn-cos.luoyangfu.com/2018-08-01/image/15326715774000.jpg
存在多个版本的时候可以设置为true,自动清除比当前版本低的文件夹

  • inspectContent: 检测代码内是否有可替换为版本的模板,看设置option.template
  • template: 替换的模板,默认是[webpack-plugin-auto-version]version[/webpack-plugin-auto-version]
  • ignoreSuffix: 配置忽略后缀的文件存放到编译根目录下
  • isAsyncJs: 配置是否是动态创建script的,动态创建script如果是更具publicPath则可用,会在publicPath加入版本号
  • htmlTempSuffix: 按照html语法的后缀,默认是: html, ejs, handlebars,vm 文件等

插件使用方法

安装

npm/yarn:

npm i -D webpack-plugin-auto-version # yarn add -D webpack-plugin-auto-version

配置webpack.config.prod.js

const WebpackPluginAutoVersion = require(''webpack-plugin-auto-version'')

module.exports = {
    // ...忽略其他配置
    plugins: [
        // ...其他插件
        new WebpackPluginAutoVersion()
    ]
    
}

webpack 插件开发

使用webpack插件开发的话需要一个函数对象,函数对象对外暴露apply方法,apply参数有webpack的编译对象complier,其中包含webpack的配置项,编译支援,编译分块,还有webpack的钩子以及其他插件暴露的钩子函数。

看下面代码:

if (complier.hooks) {
      // 兼容webpack ^4.0.0
      complier.hooks.compilation.tap(''WebpackAutoVersionPlugin'', (compliation) => {
        compliation.hooks.htmlWebpackPluginBeforeHtmlGeneration &&
          compliation.hooks.htmlWebpackPluginBeforeHtmlGeneration.tapAsync(
            ''WebpackAutoVersionPlugin'',
            htmlWebpackPluginBeforeHtmlGeneration(this)
          )
        compliation.hooks.htmlWebpackPluginAlterAssetTags &&
          compliation.hooks.htmlWebpackPluginAlterAssetTags.tapAsync(
            ''WebpackAutoVersionPlugin'',
            htmlWebpackPluginAlterAssetTags(this)
          )
        compliation.hooks.htmlWebpackPluginAfterHtmlProcessing &&
          compliation.hooks.htmlWebpackPluginAfterHtmlProcessing.tapAsync(
            ''WebpackAutoVersionPlugin'',
            htmlWebpackPluginAfterHtmlProcessing(this)
          )
      })
    } else {
      // 如果存在html-webpack-plugin 则监听
      complier.plugin(''compilation'', (compilation) => {
        compilation.plugin(
          ''html-webpack-plugin-before-html-generation'',
          htmlWebpackPluginBeforeHtmlGeneration(this)
        )
        compilation.plugin(
          ''html-webpack-plugin-alter-asset-tags'',
          htmlWebpackPluginAlterAssetTags(this)
        )
        compilation.plugin(
          ''html-webpack-plugin-after-html-processing'',
          htmlWebpackPluginAfterHtmlProcessing(this)
        )
      })
    }

这里对html-webpack-plugin的钩子函数监听,配合html-webpack-plugin继续网页资源的构造,同时兼容webpack4.x || webpack3.x两种版本。

详细代码查看: L161

GitHub仓库: https://github.com/zsirfs/webpack-plugin-auto-version 欢迎star,fork

问题反馈: issues

angular – 使用Webpack时出错

angular – 使用Webpack时出错

我正在尝试按照 this tutorial学习webpack.

当我跑下午开始时,我得到70个错误,如下所示.

$npm start

> angular2-webpack@1.0.0 start /Users/angular-webpak

[at-loader] Using typescript@2.0.10 from typescript and "tsconfig.json" from /Users/angular-webpak/src/tsconfig.json.

[at-loader] Checking finished with 70 errors
...

ERROR in [at-loader] node_modules/@types/jasmine/index.d.ts:39:52 
    TS1005: '=' expected.
ERROR in [at-loader] node_modules/@angular/common/src/directives/ng_class.d.ts:48:34 
    TS2304: Cannot find name 'Set'.
ERROR in [at-loader] node_modules/@angular/common/src/pipes/async_pipe.d.ts:44:38 
    TS2304: Cannot find name 'Promise'.
ERROR in [at-loader] node_modules/@angular/compiler/src/aot/compiler.d.ts:32:38 
    TS2304: Cannot find name 'Promise'.
ERROR in [at-loader] node_modules/@angular/compiler/src/aot/compiler.d.ts:44:32 
    TS2304: Cannot find name 'Map'.
ERROR in [at-loader] node_modules/@angular/compiler/src/aot/compiler_host.d.ts:20:33 
    TS2304: Cannot find name 'Promise'.
ERROR in [at-loader] node_modules/@angular/compiler/src/compile_Metadata.d.ts:342:20 
    TS2304: Cannot find name 'Set'.
ERROR in [at-loader] node_modules/@angular/compiler/src/compile_Metadata.d.ts:344:28 
    TS2304: Cannot find name 'Set'.
ERROR in [at-loader] node_modules/@angular/compiler/src/compile_Metadata.d.ts:346:15 
    TS2304: Cannot find name 'Set'.
ERROR in [at-loader] node_modules/@angular/compiler/src/compile_Metadata.d.ts:348:23 
    TS2304: Cannot find name 'Set'.
ERROR in [at-loader] node_modules/@angular/compiler/src/compile_Metadata.d.ts:350:17 
    TS2304: Cannot find name 'Set'.
ERROR in [at-loader] node_modules/@angular/compiler/src/compile_Metadata.d.ts:352:25 
    TS2304: Cannot find name 'Set'.
ERROR in [at-loader] node_modules/@angular/compiler/src/directive_normalizer.d.ts:38:72 
    TS2304: Cannot find name 'Promise'.
ERROR in [at-loader] node_modules/@angular/compiler/src/directive_normalizer.d.ts:40:74 
    TS2304: Cannot find name 'Promise'.
ERROR in [at-loader] node_modules/@angular/compiler/src/i18n/extractor.d.ts:14:33 
    TS2304: Cannot find name 'Promise'.
ERROR in [at-loader] node_modules/@angular/compiler/src/i18n/extractor.d.ts:22:35 
    TS2304: Cannot find name 'Promise'.
ERROR in [at-loader] node_modules/@angular/compiler/src/jit/compiler.d.ts:44:49 
    TS2304: Cannot find name 'Promise'.
ERROR in [at-loader] node_modules/@angular/compiler/src/jit/compiler.d.ts:46:65 
    TS2304: Cannot find name 'Promise'.
ERROR in [at-loader] node_modules/@angular/compiler/src/Metadata_resolver.d.ts:58:104 
    TS2304: Cannot find name 'Promise'.
ERROR in [at-loader] node_modules/@angular/compiler/src/output/output_ast.d.ts:433:63 
    TS2304: Cannot find name 'Set'.
ERROR in [at-loader] node_modules/@angular/compiler/src/resource_loader.d.ts:13:23 
    TS2304: Cannot find name 'Promise'.
ERROR in [at-loader] node_modules/@angular/compiler/src/util.d.ts:33:18 
    TS2304: Cannot find name 'Promise'.
ERROR in [at-loader] node_modules/@angular/compiler/src/util.d.ts:34:46 
    TS2304: Cannot find name 'Promise'.
<snipped several similar errors>
ERROR in [at-loader] node_modules/@angular/core/src/change_detection/differs/default_keyvalue_differ.d.ts:24:15 
    TS2304: Cannot find name 'Map'.
ERROR in [at-loader] node_modules/@angular/core/src/change_detection/differs/default_keyvalue_differ.d.ts:28:16 
    TS2304: Cannot find name 'Map'.
<snipped several similar errors>
ERROR in [at-loader] node_modules/@angular/platform-browser/src/dom/shared_styles_host.d.ts:11:30 
    TS2304: Cannot find name 'Set'.
ERROR in [at-loader] node_modules/@angular/platform-browser/src/dom/shared_styles_host.d.ts:22:30 
    TS2304: Cannot find name 'Set'.
ERROR in [at-loader] node_modules/@angular/router/src/config.d.ts:307:85 
    TS2304: Cannot find name 'Promise'.
ERROR in [at-loader] node_modules/@angular/router/src/interfaces.d.ts:78:99 
    TS2304: Cannot find name 'Promise'.
ERROR in [at-loader] node_modules/@angular/router/src/interfaces.d.ts:157:109 
    TS2304: Cannot find name 'Promise'.
ERROR in [at-loader] node_modules/@angular/router/src/interfaces.d.ts:227:115 
    TS2304: Cannot find name 'Promise'.
ERROR in [at-loader] node_modules/@angular/router/src/interfaces.d.ts:297:89 
    TS2304: Cannot find name 'Promise'.
ERROR in [at-loader] node_modules/@angular/router/src/interfaces.d.ts:367:50 
    TS2304: Cannot find name 'Promise'.
ERROR in [at-loader] node_modules/@angular/router/src/router.d.ts:392:70 
    TS2304: Cannot find name 'Promise'.
ERROR in [at-loader] node_modules/@angular/router/src/router.d.ts:414:59 
    TS2304: Cannot find name 'Promise'.
ERROR in [at-loader] node_modules/@angular/router/src/utils/collection.d.ts:36:79 
    TS2304: Cannot find name 'Promise'.
ERROR in [at-loader] node_modules/@types/jasmine/index.d.ts:39:38 
    TS2371: A parameter initializer is only allowed in a function or constructor implementation.
ERROR in [at-loader] node_modules/@types/jasmine/index.d.ts:39:46 
    TS2304: Cannot find name 'keyof'.
ERROR in [at-loader] node_modules/rxjs/Observable.d.ts:68:60 
    TS2304: Cannot find name 'Promise'.
ERROR in [at-loader] node_modules/rxjs/Observable.d.ts:68:70 
    TS2304: Cannot find name 'Promise'.
ERROR in [at-loader] node_modules/rxjs/observable/PromiSEObservable.d.ts:40:31 
    TS2304: Cannot find name 'Promise'.
ERROR in [at-loader] node_modules/rxjs/observable/PromiSEObservable.d.ts:41:26 
    TS2304: Cannot find name 'Promise'.
ERROR in [at-loader] node_modules/rxjs/operator/toPromise.d.ts:2:60 
    TS2304: Cannot find name 'Promise'.
ERROR in [at-loader] node_modules/rxjs/operator/toPromise.d.ts:3:79 
    TS2304: Cannot find name 'Promise'.
ERROR in [at-loader] node_modules/rxjs/operator/toPromise.d.ts:3:89 
    TS2304: Cannot find name 'Promise'.
ERROR in [at-loader] src/app/app.component.ts:8:14 
    TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option to remove this warning.
ERROR in [at-loader] src/app/app.module.ts:13:14 
    TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option to remove this warning.
Child html-webpack-plugin for "index.html":
    chunk    {0} index.html 299 bytes [entry] [rendered]
Child extract-text-webpack-plugin:
    chunk    {0} extract-text-webpack-plugin-output-filename 1.95 kB [entry] [rendered]
webpack: Failed to compile.
修复程序在这里 https://github.com/angular/angular.io/pull/3233/commits/8e886fc2710acadd749139c2172519de254b849c

你必须修改webpack.common.js

angular – 在angluar2 webpack中使用Font-awesome

angular – 在angluar2 webpack中使用Font-awesome

我一直在努力在使用webpack的angular2应用程序中安装font-awesome.

更新了我的webpack.config.js以包含加载字体文件:

module: {
loaders: [
  { test: /\.ts$/,loader: 'awesome-typescript-loader' },{ test: /\.json$/,loader: 'json-loader' },{ test: /\.html/,loader: 'raw-loader' },{ test: /\.css$/,loader: 'to-string-loader!css-loader' },// For font-awesome
  {test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)(\?v=[0-9]\.[0-9]\.[0-9])?$/,loader: 'file?name=fonts/[name].[hash].[ext]?'}
]

},

使用以下命令安装font-awesome:

npm install font-awesome

添加了对ts文件的要求
要求(” ../ node_modules /字体真棒/ CSS /字体awesome.css’);

我一直在收到这个错误:

ERROR in ./~/font-awesome/css/font-awesome.css
Module build Failed: Error: Cannot find module ‘../fonts/fontawesome-webfont.eot?v=4.6.3’

任何想法如何解决这一问题 ?.

解决方法

我想你应该使用style-loader而不是to-string-loader:

{ test: /\.css$/,loader: 'style!css' },

angular – 如何解决module.id应该是使用webpack2的字符串错误

angular – 如何解决module.id应该是使用webpack2的字符串错误

所以我在一个Angular2项目上使用webpack2,该项目有几个外部依赖项.其中一些依赖项使用commonjs并声明如下组件:
@Component({
    moduleId: module.id,templateUrl: 'mycomponent.html'
    ...
})

这会导致以下错误:

Error: moduleId should be a string in "MyComponent"

经过一些研究,我发现这是因为Webpack期望组件将id作为数字,而Angular将其声明为字符串.我无法更改依赖关系代码.我能做些什么才能忍受这种依赖?

谢谢!

所以这就是我现在想要依赖这种依赖的方式.使用字符串替换加载程序为我删除该行:
{ test: /.*node_modules\/my-dependency-folder\/.*\.js/,loader: 'string-replace-loader',query: { search: 'moduleId: module.id,',replace: '' } }

希望这可以帮助有同样问题的人.

我们今天的关于如何在angular 2应用程序上使用webpack进行代码分割?angularjs webpack的分享就到这里,谢谢您的阅读,如果想了解更多关于# 使用webpack进行版本管理工具(webpack-plugin-auto-version)、angular – 使用Webpack时出错、angular – 在angluar2 webpack中使用Font-awesome、angular – 如何解决module.id应该是使用webpack2的字符串错误的相关信息,可以在本站进行搜索。

本文标签: