部署Angular 2教程webpack build

时间:2016-10-24 12:21:40

标签: angularjs angular webpack

我目前正在尝试学习ng2。

我已按照此处的教程进行操作:https://angular.io/docs/ts/latest/guide/webpack.html

并且发现在我的构建中创建了一个dist,在尝试运行它时,无论是本地还是Github页面,我的bundle文件都收到404错误。

我已按照教程行进行操作,但似乎无法正常工作。

有人可以帮忙吗?

我的webpack构建如下:

webpack.common.js

var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var helpers = require('./helpers');

module.exports = {
    entry: {
        'polyfills': './src/polyfills.ts',
        'vendor': './src/vendor.ts',
        'app': './src/main.ts'
    },

    resolve: {
        extensions: ['', '.js', '.ts']
    },

    module: {
        loaders: [
            {
                test: /\.ts$/,
                loaders: ['awesome-typescript-loader', 'angular2-template-loader']
            },
            {
                test: /\.html$/,
                loader: 'html'
            },
            {
                test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
                loader: 'file?name=assets/[name].[hash].[ext]'
            },
            {
                test: /\.css$/,
                exclude: helpers.root('src', 'app'),
                loader: ExtractTextPlugin.extract('style', 'css?sourceMap')
            },
            {
                test: /\.css$/,
                include: helpers.root('src', 'app'),
                loader: 'raw'
            }
        ]
    },

    plugins: [
        new webpack.optimize.CommonsChunkPlugin({
            name: ['app', 'vendor', 'polyfills']
        }),

        new HtmlWebpackPlugin({
            template: 'src/index.html'
        })
    ]
};

webpack.prod.js

var webpack = require('webpack');
var webpackMerge = require('webpack-merge');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var commonConfig = require('./webpack.common.js');
var helpers = require('./helpers');

const ENV = process.env.NODE_ENV = process.env.ENV = 'production';

module.exports = webpackMerge(commonConfig, {
    devtool: 'source-map',

    output: {
        path: helpers.root('dist'),
        publicPath: '/',
        filename: '[name].[hash].js',
        chunkFilename: '[id].[hash].chunk.js'
    },

    htmlLoader: {
        minimize: false // workaround for ng2
    },

    plugins: [
        new webpack.NoErrorsPlugin(),
        new webpack.optimize.DedupePlugin(),
        new webpack.optimize.UglifyJsPlugin({ // https://github.com/angular/angular/issues/10618
            mangle: {
                keep_fnames: true
            }
        }),
        new ExtractTextPlugin('[name].[hash].css'),
        new webpack.DefinePlugin({
            'process.env': {
                'ENV': JSON.stringify(ENV)
            }
        })
    ]
});

的index.html

<!DOCTYPE html>
<html>

<head>
    <base href=/>
    <title>Angular With Webpack</title>
    <meta charset=UTF-8>
    <meta name=viewport content="width=device-width,initial-scale=1">
    <link href="/app.f053dbe7ce9dd32c3e43.css" rel="stylesheet">
</head>

<body>
    <my-app>Loading...</my-app>
    <script type="text/javascript" src="/polyfills.f053dbe7ce9dd32c3e43.js"></script>
    <script type="text/javascript" src="/vendor.f053dbe7ce9dd32c3e43.js"></script>
    <script type="text/javascript" src="/app.f053dbe7ce9dd32c3e43.js"></script>
</body>

</html>

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题!你在config dir中有karma.conf.js吗?您的项目结构应该是: enter image description here