用户登录
用户注册

分享至

详解用webpack2搭建angular2的项目

  • 作者: wangkx1990
  • 来源: 51数据库
  • 2021-08-24

webpack2和angular2搭建的项目

github地址:项目链接

  1. npm install,安装依赖包
  2. npm run dev,启动本地工程,在localhost:1699进行预览
package.json
{
 "name": "angular-webpack",
 "version": "1.0.0",
 "description": "webpack2 & angular2",
 "scripts": {
  "dev": "babel-node ./src/config/dev.js"
 },
 "author": "travis lee",
 "license": "isc",
 "dependencies": {
  "@angular/common": "~4.0.0",
  "@angular/compiler": "~4.0.0",
  "@angular/core": "~4.0.0",
  "@angular/forms": "~4.0.0",
  "@angular/http": "~4.0.0",
  "@angular/platform-browser": "~4.0.0",
  "@angular/platform-browser-dynamic": "~4.0.0",
  "@angular/router": "~4.0.0",
  "core-js": "^2.4.1",
  "es6-shim": "^0.35.3",
  "reflect-metadata": "^0.1.8",
  "rxjs": "5.0.1",
  "zone.js": "^0.8.4"
 },
 "devdependencies": {
  "babel-core": "^6.25.0",
  "babel-loader": "^7.1.0",
  "babel-preset-es2015": "^6.24.1",
  "clean-webpack-plugin": "^0.1.16",
  "css-loader": "^0.28.4",
  "extract-text-webpack-plugin": "^2.1.2",
  "path": "^0.12.7",
  "style-loader": "^0.18.2",
  "ts-loader": "^2.1.0",
  "typescript": "^2.3.4",
  "typings": "^2.1.1",
  "uglifyjs-webpack-plugin": "^0.4.6",
  "webpack": "^2.6.1",
  "webpack-dev-server": "^2.5.0",
  "webpack-merge": "^4.1.0"
 }
}

webpack配置文件开发版:

import path from 'path'
import config from "./webpack.config"
import merge from "webpack-merge"
import webpack from "webpack"
import webpackdevserver from "webpack-dev-server"
import { format } from 'util'

let port = 1699;
let public_path = "http://localhost:" + port + "/";
let webpackconifg = merge(config, {
  devtool: "source-map",
  //debug: true, webpack2 已切换到plugins中,据说在3中将取消
  entry: {
    main: [
      format("webpack-dev-server/client?%s", public_path),
      "webpack/hot/dev-server",
      "./src/main.ts"
    ]
  },
  output: {
    path: path.resolve(__dirname, '../../dist'),
    publicpath: public_path,
    filename: '[name].js'
  },
  plugins: [
    new webpack.hotmodulereplacementplugin()
  ]
})
const compiler = webpack(webpackconifg);
new webpackdevserver(compiler, {
  inline: true,
  hot: true,
  port: port,
  stats: {
    colors: true
  }
}).listen(port, 'localhost', (err) => {
  console.log(123)
})

搭建中遇到的问题:

the url 'localhost:1699/sockjs-node' is invalid, 

该问题是由于webpack配置文件中的publicpath前边没有加http://,导致url解析失败

uncaught reflect-metadata shim is required when using class decorators,

 这个问题是由于main.ts文件中没有引入 reflect-metadata和zone.js


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

软件
前端设计
程序设计
Java相关