捆绑问题(angular2 + webpack 捆绑)

Posted

技术标签:

【中文标题】捆绑问题(angular2 + webpack 捆绑)【英文标题】:Issues on bundling (angular2 + webpack bundling) 【发布时间】:2017-08-04 18:06:29 【问题描述】:

这是我的 package.json。我添加了 webpack 的依赖项。我对我的依赖项和脚本是否正确感到困惑?因为,当我执行 npm start 时,它开始捆绑并在浏览器上启动 angular2 项目,我遇到了 无法获取 app.bundle.js 的问题,我提供了 index.html、webpack。 config.js 下面,..


  "name": "angular-quickstart",
  "version": "1.0.0",
  "description": "QuickStart package.json from the documentation, supplemented with testing support",
  "scripts": 
    "build": "rimraf dist && webpack --config webpack.config.js --progress",
    "build:watch": "tsc -p src/ -w",
    "build:e2e": "tsc -p e2e/",
    "serve": "lite-server -c=bs-config.json",
    "serve:e2e": "lite-server -c=bs-config.e2e.json",
    "prestart": "npm run build",
    "start": "concurrently \"npm run build:watch\" \"npm run serve\"",
    "pree2e": "npm run build:e2e",
    "e2e": "concurrently \"npm run serve:e2e\" \"npm run protractor\" --kill-others --success first",
    "preprotractor": "webdriver-manager update",
    "protractor": "protractor protractor.config.js",
    "pretest": "npm run build",
    "test": "concurrently \"npm run build:watch\" \"karma start karma.conf.js\"",
    "pretest:once": "npm run build",
    "test:once": "karma start karma.conf.js --single-run",
    "lint": "tslint ./src/**/*.ts -t verbose"
  ,
  "peerDependencies": 
    "uglify-js": "^2.8.0",
    "webpack": "^1.9 || ^2 || ^2.1.0-beta || ^2.2.0-rc"
  ,
  "keywords": [],
  "author": "",
  "license": "MIT",
  "dependencies": 
    "@angular/common": "~2.4.0",
    "@angular/compiler": "~2.4.0",
    "@angular/core": "~2.4.0",
    "@angular/forms": "~2.4.0",
    "@angular/http": "~2.4.0",
    "@angular/platform-browser": "~2.4.0",
    "@angular/platform-browser-dynamic": "~2.4.0",
    "@angular/router": "~3.4.0",
    "angular-in-memory-web-api": "~0.2.4",
    "core-js": "^2.4.1",
    "glob": "^7.1.1",
    "ng2-spin-kit": "^1.3.0",
    "ng2-toastr": "^1.5.1",
    "reflect-metadata": "^0.1.10",
    "rxjs": "5.0.1",
    "systemjs": "0.19.40",
    "webpack": "^2.2.1",
    "zone.js": "^0.7.4"
  ,
  "devDependencies": 
    "@types/jasmine": "2.5.36",
    "@types/node": "^6.0.46",
    "babel-core": "^6.24.0",
    "babel-loader": "^6.4.0",
    "babel-preset-env": "^1.2.1",
    "canonical-path": "0.0.2",
    "concurrently": "^3.2.0",
    "jasmine-core": "~2.4.1",
    "karma": "^1.3.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-cli": "^1.0.1",
    "karma-jasmine": "^1.0.2",
    "karma-jasmine-html-reporter": "^0.2.2",
    "lite-server": "^2.2.2",
    "lodash": "^4.16.4",
    "protractor": "~4.0.14",
    "rimraf": "^2.5.4",
    "tslint": "^3.15.1",
    "typescript": "~2.0.10",
    "webpack": "^2.2.1",
    "webpack-dev-server": "2.4.1",
    "webpack-merge": "^3.0.0"
  ,
  "repository": 

"build": "rimraf dist && webpack --config webpack.config.js --进度”

而webpack.config.js文件是

var webpack = require('webpack'),
  glob = require('glob');

module.exports = 

  entry: 
    'myPages': glob.sync('./src/**/*.js'), // here including js files from src folder
  ,
  output: 
    path: './dist',
    filename: 'app.bundle.js',
    library: "node_modules",
  ,
  target: 'web',
  module: 
    loaders: [
        test: /\.node$/,
        loader: 'node-loader'
      ,
      // javascript: js, jsx
      
        test: /\.jsx?$/,
        use: 'babel-loader'
      ,
      // CSS: scss, css

      
        test: /\.css$/,
        loader: "style!css"
      ,
      // HTML: htm, html
      
        test: /\.html?$/,
        loader: "file?name=[name].[ext]"
      
    ]
  ,
  plugins: [
    new webpack.optimize.UglifyJsPlugin(
      compress: 
        warnings: false
      ,
    )
  ]
;

webpack.config.js 提供了名为“dist/app.bundle.js”的捆绑输出文件(在 dist 文件夹内),但我在将 app.bundle.js 文件插入 html 页面时遇到了问题,我的 index.html 是

<!DOCTYPE html>
<html>

<head>
    <title>Angular_E&O</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

    <base href="/E&O">
    <link rel="stylesheet" href="styles.css">
    <link href="node_modules/ng2-toastr/bundles/ng2-toastr.min.css" rel="stylesheet" />
    <script src="node_modules/core-js/client/shim.min.js"></script>
    <script src="node_modules/zone.js/dist/zone.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>
    <script src="node_modules/ng2-toastr/bundles/ng2-toastr.min.js"></script>
</head>

<body>
         <script src="dist/app.bundle.js"></script>
    <mainapp>content loading..</mainapp>
</body>

</html>

index.html 被调用

&lt;script src="dist/app.bundle.js"&gt;&lt;/script&gt;

在这个脚本行中我得到了错误,它无法得到 app.bundle.js,即使我检查了路径也是正确的

由于我是 webpack 捆绑的初学者,请帮助我,..

【问题讨论】:

【参考方案1】:

请在根标签后添加捆绑的js文件 像这样

<mainapp>content loading..</mainapp>
<script src="dist/app.bundle.js"></script>

【讨论】:

以上是关于捆绑问题(angular2 + webpack 捆绑)的主要内容,如果未能解决你的问题,请参考以下文章

没有资产文件夹将 Angular 2 应用程序与最新的 webpack 捆绑在一起

“你可能需要一个适合这种文件类型的加载器”,webpack 无法解析 angular2 文件

TypeScript with Webpack - 显示 JavaScript 但不显示 TypeScript 源映射

Angular 2 的 JSPM 与 WebPack

JSPM 与 Angular 2 的 WebPack

使用 webpack 优化 Angular 2 应用程序构建持续时间