webpack 配置案例for angular babel
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的更多相关文章
- webpack 3.X学习之Babel配置
Babel是什么 Babel是一个编译JavaScript的平台,它的强大之处表现在可以通过编译帮你达到: 使用下一代的javascript(ES6,ES7,--)代码,即使当前浏览器没有完成支持: ...
- storybook配置之基本配置和webpack配置
默认配置 Storybook有一个默认的适合(suits)大型项目开发的webpack配置,假如你使用react app,他类似于创建一个react app的配置,并经过调整(tweaked ),使其 ...
- (24/24) webpack小案例--自己动手用webpack构建一个React的开发环境
通过前面的学习,对webpack有了更深的认识,故此节我们就利用前面相关知识自己动手用webpack构建一个React的开发环境,就算是一个小案例吧. 注:此处使用的开发工具是Webstorm. 1. ...
- [webpack] 配置react+es6开发环境
写在前面 每次开新项目都要重新安装需要的包,简单记录一下. 以下仅包含最简单的功能: 编译react 编译es6 打包src中入口文件index.js至dist webpack配置react+es6开 ...
- Webpack配置示例和详细说明
/* * 请使用最新版本nodejs * 默认配置,是按生产环境的要求设置,也就是使用 webpack -p 命令就可以生成正式上线版本. * 也可以使用 webpack -d -w 命令,生成用于开 ...
- vue-cli#2.0 webpack 配置分析
目录结构: ├── README.md ├── build │ ├── build.js │ ├── check-versions.js │ ├── dev-client.js │ ├── dev-s ...
- webpack配置这一篇就够
最近看了一篇好文,根据这个文章重新梳理了一遍webpack打包过程,以前的一些问题也都清楚了,在这里分享一下,同时自己也做了一些小的调整 原文链接:http://www.jianshu.com/p/4 ...
- 前端工程化(二)---webpack配置
导航 前端工程化(一)---工程基础目录搭建 前端工程化(二)---webpack配置 前端工程化(三)---Vue的开发模式 前端工程化(四)---helloWord 继续上一遍的配置,本节主要记录 ...
- vue-cli中webpack配置详解
vue-cli是构建vue单页应用的脚手架,命令行输入vue init <template-name> <project-name>从而自动生成的项目模板,比较常用的模板有we ...
随机推荐
- C#获取当前路径的七种方法 【转载】
//1.获取模块的完整路径. string path1 = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName; // ...
- URL整理
Airtest project官网 http://airtest.netease.com/ poco辅助文档:http://poco.readthedocs.io/zh_CN/latest/index ...
- Flex Cairngorm框架知识整理
简介: Cairngorm是一个开源的Flex项目,为FLex提供了一个类似MVC的体系结构框架,它是Flex RIA开发的最好框架之一.使用Cairngorm框架可以大大提高开发和维护的效率. Ca ...
- 通过 DDNS 解决宽带拨号 ip 变化问题
前面你的文章我已经写了 写了 DMZ 内网映射的 方式. 这样内网主机已经暴露在外网中了. 但是 拨号上网我们的ip是 会变化的.大概规律就是 每次拨号都会变化.如果不拨号,每 24 小时 ip也会自 ...
- 使用openssl命令剖析RSA私钥文件格式
原文 https://blog.csdn.net/zhymax/article/details/7683925 Openssl提供了强大证书功能,生成密钥对.证书,颁发证书.生成crl.验证证书.销毁 ...
- HTML:meta标签使用总结 [转载] [360浏览器 指定极速模式]
meta标签作用 META标签是HTML标记HEAD区的一个关键标签,提供文档字符集.使用语言.作者等基本信息,以及对关键词和网页等级的设定等,最大的作用是能够做搜索引擎优化(SEO). PS:便于搜 ...
- MySQL 5.7 Invalid default value for 'CREATE_TIME'报错的解决方法
出处:http://blog.itpub.net/15498/viewspace-2136006/ 由于数据库的升级,今天在执行从MySQL 5.6导出来的SQL文件时报错: mysql> so ...
- mysql 备份与还原恢复
一.数据备份 1.使用mysqldump命令备份 mysqldump命令将数据库中的数据备份成一个文本文件.表的结构和表中的数据将存储在生成的文本文件中. mysqldump命令的工作原理很简单.它先 ...
- 利用JavaFx开发RIA桌面应用-事件监听
1 事件监听 最近利用javaFX开发桌面客户端,碰到需要给各种UI控件添加事件监听,在这里做一个简单的小结,供日后参考. 2 分类处理 在JavaGUI 和Android中,事件通常通过实现list ...
- 基于C#的PISDK研究(代码)
本篇文章主要利用PISDK从PI服务器取数,介绍多种取数方法. 首先需要一些基础的代码,比如获取PI服务的: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...