1、dev.js:

const webpack = require('webpack');
const webpackUglifyJsPlugin = require('webpack-uglify-js-plugin');
const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
function root(__path) {
return path.join(__dirname, __path);
}
const config = {
entry: [
"webpack-hot-middleware/client?reload=true",
// 这里是你的入口文件
"./src/index.js",
],
output: { //输出目录
publicPath: "",
path: __dirname,
filename: 'bundle.js',
},
module: {
rules: [{
test: /\.jsx?$/,
use: [{
loader: 'babel-loader', //只需要babel就可以写ng2的代码了
options: {
presets: ['es2015', 'es2016', 'es2017', 'stage-0'], //使用的presets
plugins: ['transform-decorators-legacy'] //使用的babel插件
}
}],
exclude: /node_modules/
}, {
test: /\.scss$/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
loader: "css-loader!autoprefixer-loader?{browsers:['last 6 Chrome versions', 'last 3 Safari versions', 'iOS >= 5', 'Android >= 4.0']}!sass-loader",
}),
}, {
test: /\.png$/,
use: {
loader: 'file-loader',
options: {
name: '../img/[name].[ext]'
}
}
}]
},
plugins: [
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.ContextReplacementPlugin( //这个是ng需要用的插件,以免报错
// The (\\|\/) piece accounts for path separators in *nix and Windows
/angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/,
root('./src'), // location of your src
{} // a map of your routes
),
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin(),
new ExtractTextPlugin('css/style.css')
/*new HtmlWebpackPlugin({
title: 'index',
hash:true,
template: 'index.ejs', // Load a custom template (ejs by default see the FAQ for details)
})*/
]
};
module.exports = config;

2、server.js:

var webpack = require('webpack'),
webpackDevMiddleware = require('webpack-dev-middleware'),
webpackHotMiddleware = require('webpack-hot-middleware'),
config = require("./config/dev.js"),
express = require('express'),
app = express(),
compiler = webpack(config);
app.use(webpackDevMiddleware(compiler, {
publicPath: config.output.publicPath,
noInfo: true,
stats: {
colors: true,
progress: true
}
}));
app.use(webpackHotMiddleware(compiler, {
noInfo: true,
publicPath: config.output.publicPath
})); app.get('*', function(req, res) {
var fileName = req.url;
console.log(fileName);
if (fileName == '/') {
res.sendFile(__dirname + '/index.html');
}else{
res.sendFile(__dirname + fileName);
}
});
app.listen(8087,'0.0.0.0');

3、package.json:

{
"name": "wtest",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node server.js",
"build": "NODE_ENV=production&&npm run output",
"output": "webpack --config webpack.build.js",
"test": "node ./dist/test.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@angular/common": "^2.1.0",
"@angular/compiler": "^2.1.0",
"@angular/core": "^2.1.0",
"@angular/http": "^2.1.0",
"@angular/platform-browser": "^2.1.0",
"@angular/platform-browser-dynamic": "^2.1.0",
"@angular/router": "^3.1.0",
"es6-promise": "3.0.2",
"es6-shim": "^0.33.3",
"reflect-metadata": "0.1.8",
"rxjs": "^5.2.0",
"zone.js": "^0.6.26",
"autoprefixer-loader": "^3.2.0",
"babel-core": "^6.23.1",
"babel-loader": "^6.3.2",
"babel-plugin-angular2-annotations": "^5.1.0",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-preset-angular2": "^0.0.2",
"babel-preset-es2015": "^6.22.0",
"babel-preset-es2016": "^6.22.0",
"babel-preset-es2017": "^6.22.0",
"babel-preset-react": "^6.23.0",
"babel-preset-stage-0": "^6.22.0",
"copy-webpack-plugin": "^4.0.1",
"css-loader": "^0.26.2",
"extract-text-webpack-plugin": "^2.0.0",
"file-loader": "^0.10.1",
"html-webpack-plugin": "^2.28.0",
"node-sass": "^4.5.0",
"reload": "^1.1.1",
"sass-loader": "^6.0.2",
"style-loader": "^0.13.2",
"uglifyjs-webpack-plugin": "^0.3.0",
"webpack": "^2.2.1",
"webpack-del-plugin": "^0.0.1",
"webpack-dev-middleware": "^1.10.1",
"webpack-dev-server": "^2.4.1",
"webpack-hot-middleware": "^2.17.1",
"webpack-spritesmith": "^0.3.1",
"webpack-uglify-js-plugin": "^1.1.9"
}
}

4、index.js:

import 'reflect-metadata';
import 'zone.js'; //这两个是为了兼容angular2正常使用而导入的插件
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; // import { enableProdMode } from '@angular/core'; import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser'; import App from './test.js';
import Test from "./test2.js";
@NgModule({
imports: [ BrowserModule ],
declarations: [
App,
Test //声明所有的组件,这样才能使用组建
],
bootstrap: [ App ] //启动组建,这样index.html 中才能使用该标签
})
class AppModule { } platformBrowserDynamic().bootstrapModule(AppModule);

5、test.js:

import { Component } from '@angular/core';
import Test from "./test2.js"; @Component({
selector: 'my-app',
template: `
<h1>sdfjsdfkj</h1>
<h2>Have {{ what }}</h2>
<test-app [toChildData]="toChildData"></test-app>
`
})
export default class App {
constructor() {
this.what = "a good time!";
this.toChildData='sdfksdfdjs';
}
};

6、test2.js:

import {
Component,
Input
} from '@angular/core'; @Component({
selector: 'test-app',
template: `
<div (click)="testfn()">{{testData}}</div> //事件促发案例
`
}) export default class Test {
@Input() toChildData;
constructor() {
this.testData = '46456654';
}
testfn() {
console.log(this.toChildData);
alert('dflksjdfj')
}
}

  

webpack 配置案例for angular babel的更多相关文章

  1. webpack 3.X学习之Babel配置

    Babel是什么 Babel是一个编译JavaScript的平台,它的强大之处表现在可以通过编译帮你达到: 使用下一代的javascript(ES6,ES7,--)代码,即使当前浏览器没有完成支持: ...

  2. storybook配置之基本配置和webpack配置

    默认配置 Storybook有一个默认的适合(suits)大型项目开发的webpack配置,假如你使用react app,他类似于创建一个react app的配置,并经过调整(tweaked ),使其 ...

  3. (24/24) webpack小案例--自己动手用webpack构建一个React的开发环境

    通过前面的学习,对webpack有了更深的认识,故此节我们就利用前面相关知识自己动手用webpack构建一个React的开发环境,就算是一个小案例吧. 注:此处使用的开发工具是Webstorm. 1. ...

  4. [webpack] 配置react+es6开发环境

    写在前面 每次开新项目都要重新安装需要的包,简单记录一下. 以下仅包含最简单的功能: 编译react 编译es6 打包src中入口文件index.js至dist webpack配置react+es6开 ...

  5. Webpack配置示例和详细说明

    /* * 请使用最新版本nodejs * 默认配置,是按生产环境的要求设置,也就是使用 webpack -p 命令就可以生成正式上线版本. * 也可以使用 webpack -d -w 命令,生成用于开 ...

  6. vue-cli#2.0 webpack 配置分析

    目录结构: ├── README.md ├── build │ ├── build.js │ ├── check-versions.js │ ├── dev-client.js │ ├── dev-s ...

  7. webpack配置这一篇就够

    最近看了一篇好文,根据这个文章重新梳理了一遍webpack打包过程,以前的一些问题也都清楚了,在这里分享一下,同时自己也做了一些小的调整 原文链接:http://www.jianshu.com/p/4 ...

  8. 前端工程化(二)---webpack配置

    导航 前端工程化(一)---工程基础目录搭建 前端工程化(二)---webpack配置 前端工程化(三)---Vue的开发模式 前端工程化(四)---helloWord 继续上一遍的配置,本节主要记录 ...

  9. vue-cli中webpack配置详解

    vue-cli是构建vue单页应用的脚手架,命令行输入vue init <template-name> <project-name>从而自动生成的项目模板,比较常用的模板有we ...

随机推荐

  1. oracle-sql内置函数

    函数  oracle 自定义函数入门 主题:ORACLE函数大全 ############################### set operators UNION, UNION ALL, INT ...

  2. hadoop大数据基础框架技术详解

    一.什么是大数据 进入本世纪以来,尤其是2010年之后,随着互联网特别是移动互联网的发展,数据的增长呈爆炸趋势,已经很难估计全世界的电子设备中存储的数据到底有多少,描述数据系统的数据量的计量单位从MB ...

  3. [转] Centos7 yum lock,无法上网问题,以及安装python3.5

    centos 7 无法上网问题 转自 http://www.cnblogs.com/katios/p/5660336.html 博主本着学无止境的精神在虚拟机上安装了一个centos7 来敲敲命令行. ...

  4. SQL Server 数值四舍五入,小数点后保留2位

    1.round() 函数是四舍五入用,第一个参数是我们要被操作的数据,第二个参数是设置我们四舍五入之后小数点后显示几位. 2.numeric 函数的2个参数,第一个表示数据长度,第二个参数表示小数点后 ...

  5. 升级ambari、HDP版本(ambari 2.1升级到2.4、HDP2.3升级到2.5)

    转载自:http://blog.csdn.net/levy_cui/article/details/52461377 官方升级版本说明 http://docs.hortonworks.com/HDPD ...

  6. RedHat6.5安装zookeeper集群

    版本号: Redhat6.5  zookeeper-3.4.6  JDK1.8 zookeeper下载 官网下载地址:https://mirrors.tuna.tsinghua.edu.cn/apac ...

  7. Feign 自定义编码器、解码器和客户端

    Feign 的编码器.解码器和客户端都是支持自定义扩展,可以对请求以及结果和发起请求的过程进行自定义实现,Feign 默认支持 JSON 格式的编码器和解码器,如果希望支持其他的或者自定义格式就需要编 ...

  8. [CMAKE] 详解CMakeLists.txt文件

    [快速查询]https://cmake.org/cmake/help/v2.8.8/cmake.html#section_Commands 1 CMake简介 CMake是跨平台编译工具,比make更 ...

  9. Lucene - CustomScoreQuery 自定义排序

    在某些场景需要做自定义排序(非单值字段排序.非文本相关度排序),除了自己重写collect.weight,可以借助CustomScoreQuery. 场景:根据tag字段中标签的数量进行排序(tag字 ...

  10. Solr[Q] -No live SolrServers available to handle this request, no servers hosting shard

    No live SolrServers available to handle this request , no servers hosting shard 当某一片索引对应的结点全部宕了以后,会报 ...