【问题标题】:Split Angular2 app onto chunks with Webpack使用 Webpack 将 Angular2 应用程序拆分为块
【发布时间】:2016-08-28 16:18:25
【问题描述】:

如果我想构建用 Typescript 编写的 angular2 项目怎么办,但我不想将所有内容捆绑到一个包中。我想将供应商捆绑到 vendor.bundle.js 和其他东西保持与项目结构相同的形状。

基础结构:

app/
    main-module/
        main-module.ts
        main-module.html
        main-module.css
    page-module/
        page-module.ts
        page-module.html
        page-module.css
   main.ts
   vendor.ts

我们想要的结构:

app/
    main-module/
        main-module.js
        main-module.html
        main-module.css
    page-module/
        page-module.js
        page-module.html
        page-module.css
    main.js
    vendor.bundle.js

在 index.html 中我们这样做

<script src="vendor.bundle.js"></script>
<script src="main-module.js"></script>
<script src="page-module.js"></script>

我的 webpack.config.js

var path = require('path');
var webpack = require('webpack');

module.exports = {
context: __dirname + '/app',

entry: {
    'vendor': './vendor',
    'main': './main'
},

stats: {
    colors: true,
    reasons: true
},

output: {
    path: path.join(__dirname, '/js'),
    publicPath: 'js/',
    filename: '[name].bundle.js',
    sourceMapFilename: '[name].[chunkhash].bundle.map',
    chunkFilename: '[id].chunk.js'
},

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

module: {
    loaders: [
        {
            test: /\.ts$/, loader: 'ts-loader',
            exclude: /node_modules/
        },

        {
            test: /\.css$/,
            loaders: ['to-string-loader', 'css-loader']
        },

        {
            test: /\.html$/,
            loader: 'raw-loader'
            // exclude: [helpers.root('src/index.html')]
        }
    ]
},

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

【问题讨论】:

    标签: angular webpack systemjs


    【解决方案1】:

    Webpack 需要 en 入口点,没有入口点会抛出错误。如果你只想编译你的 ts 文件,那么你不需要 webpack pr systemjs 你可以简单地使用 typescript 编译器,你将保持相同的文件结构。 Webpack 更多。

    什么是 webpack

    webpack 是一个模块打包器。

    webpack 获取具有依赖关系的模块并生成代表这些模块的静态资产。

    注意:- 如果你想要一个具有 sam 文件结构但没有 ts 文件的 dist 文件夹,那么你可以使用 gulp 之类的工具来实现这种方法。

    我希望它有所帮助。快乐编码

    【讨论】:

    • 我有一些使用 CommonJS 的依赖项。例如 node_modules 中的很多东西,我将如何在 SystemJS 上管理它?
    • 请多解释一下什么样的依赖关系?以及您想如何管理它们?
    • 很抱歉造成混淆。我不太确定 SystemJS 是如何工作的。但我必须处理 Angular、RxJS 模块。我不想将整个 node_modules 文件夹投入生产。相反,我想拥有带有 Angular、RxJS 和其他东西的 vendor.bunle.js 以及基础应用程序 js 文件。我已经更新了应用结构,请看上方。
    • 指定多个入口点并使用 1 个公共块似乎是最常用的代码拆分方式。但是如何将 Angular 应用代码拆分成块呢?
    猜你喜欢
    • 2016-11-26
    • 2017-11-30
    • 1970-01-01
    • 2017-07-09
    • 1970-01-01
    • 2016-09-30
    • 1970-01-01
    • 1970-01-01
    • 2019-10-24
    相关资源
    最近更新 更多