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‘)
	}
}

  

时间: 2024-10-12 13:26:57

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

Webpack 踩坑系列之babel 配置

webpack 4.20.2 ,对于babel 配置 正确配置 错误配置 presets 要配置成跟@babel-core,对应的版本 原文地址:https://www.cnblogs.com/ghost-monkey/p/9716051.html

最简单的babel+webpack配置

首先先介绍一下2个重要的库:core-js 和 regenerator core-js core-js 是用于 JavaScript 的组合式标准化库,它包含 es5 (e.g: object.freeze), es6的 promise,symbols, collections, iterators, typed arrays, es7+提案等等的 polyfills 实现.也就是说,它几乎包含了所有 JavaScript 最新标准的垫片.不过为什么它不把 generator 也实现了... ?

webpack配置这一篇就够

最近看了一篇好文,根据这个文章重新梳理了一遍webpack打包过程,以前的一些问题也都清楚了,在这里分享一下,同时自己也做了一些小的调整 原文链接:http://www.jianshu.com/p/42e11515c10f git项目地址:https://github.com/gengchen528/webpackSample 更多前端内容可以到:http://www.bloggeng.com/ 写在前面的话 阅读本文之前,先看下面这个webpack的配置文件,如果每一项你都懂,那本文能带给你的

vue-cli#2.0 webpack 配置分析

目录结构: ├── README.md ├── build │ ├── build.js │ ├── check-versions.js │ ├── dev-client.js │ ├── dev-server.js │ ├── utils.js │ ├── webpack.base.conf.js │ ├── webpack.dev.conf.js │ └── webpack.prod.conf.js ├── config │ ├── dev.env.js │ ├── index.js │ └

webpack 配置学习笔记

最简单的 webpack 配置 const path = require('path') module.exports = { entry: './app/index.js', // 入口文件 output: { path: path.resolve(__dirname, 'build'), // 必须使用绝对地址,输出文件夹 filename: "bundle.js" // 打包后输出文件的文件名 } } webpack 命令配置 在 package.json 中添加代码 "

webpack开发案例(三)

基于webpack开发案例(二) 项目结构 案例一(实现根组件的头部和底部样式) App.vue <!--以后项目的根组件--> <template> <div> <!--1.0利用mint-ui中的header组件实现整个系统的头部--> <mt-header fixed title="hello"></mt-header> <!--2.0利用vue-router的<router-view>&l

webpack开发案例(四)

基于webpack开发案例(三) 案例一(新闻列表的实现) 目录结构 步骤一:创建newslist.vue文件 <template> <div id="tml"> <!--使用mui框架,实现新闻资讯列表样式--> <ul class="mui-table-view"> <li v-for="item in list" class="mui-table-view-cell mui-m

vue UI 告别webpack配置

目录 vue UI 告别webpack配置 开始体验 创建项目 项目细节 1. 插件 2. 依赖 3. 配置 4. 任务 总结 vue UI 告别webpack配置 vue-cli 3.0 的候选版本也已经发布多时了.vue-cli 3.0 版本为我们提供了集 创建.管理.分析 为一体的可视化界面vue ui,妈妈再也不用担心我不懂配置啦~让我们来一起尝尝鲜吧~ #安装最新版的vue-cli npm install -g @vue/cli #yarn/npm 安装(二选一) yarn globa

使用webpack配置react并添加到flask应用

学习react,配置是很痛苦的一关,虽然现在有了create-react-app这样方便的工具,但是必须要自己配置一遍,才能更好地进行项目开发. 首先要明确一个概念:react的文件必须经过编译才能被浏览器识别,因此我们需要webpack这个打包工具来把react的组件打包成一个js文件,然后将这个js文件放到flask目录下,然后引入到flask模版的html文件里面.当然,你也可以在前端用express等服务进行客户端渲染,只将flask服务视为一个传递数据的api. 下面开始配置吧 1.如