简体   繁体   English

使用Webpack为NPM包构建Angular 2

[英]Use Webpack to build Angular 2 for NPM Package

I have a collection of components & services built in Angular 2. Currently they are being bundled and packaged via gulp to a dist folder that is then published on a private npm server. 我有一个在Angular 2中构建的组件和服务的集合。目前它们被捆绑并通过gulp打包到dist文件夹,然后在私人npm服务器上发布。 I want to, if possible, stop using Gulp. 如果可能的话,我想停止使用Gulp。

My question is, can I use just Webpack to bundle, minify, etc my files to the dist folder. 我的问题是,我可以使用Webpack将我的文件捆绑,缩小等文件到dist文件夹。 As well as have the ability to run my jasmine tests in them? 除了能够在其中运行我的茉莉花测试吗?

I am having trouble getting started. 我无法入门。 I have a index.ts file that series of exports of all files from each components folder, as well as the main module. 我有一个index.ts文件,该文件系列从每个组件文件夹以及主模块导出所有文件。 Does that become the entry of my webpack config? 这会成为我的webpack配置的入口吗?

Yes - the file that has all other files as dependencies - is a good candidate for entry, you can also have more that one file as entry to split verdor/your own code to separate js, here the example config (assuming you would use webpack2): 是 - 包含所有其他文件作为依赖项的文件 - 是一个很好的入门候选者,你也可以有更多的那个文件作为分割verdor /你自己的代码来分隔js的条目,这里是示例配置(假设你将使用webpack2 ):

https://github.com/wkwiatek/angular2-webpack2/blob/master/webpack.config.js https://github.com/wkwiatek/angular2-webpack2/blob/master/webpack.config.js

This is working version of webpack 2 config 这是webpack 2配置的工作版本

module.exports = {
    module: {
        rules: [
            {
                // Allows me to 
                // @Component({
                //    styles: [require('./styles.scss')]
                // })
                test: /\.scss$/,
                use: [
                    { loader: "raw-loader" },
                    { loader: "sass-loader" }
                ]
            },
            {
                // bundle external dependencies to separate file
                test: /\.css$/,
                use: ExtractTextPlugin.extract({
                fallback: "style-loader",
                use: "css-loader"
                })
            },
            {
                // ts files compilation
                test: /\.tsx?$/,
                loader: 'ts-loader',
                exclude: /node_modules/,
            },
            {
                // Allows me to
                // @Component({
                //     template: require('./template.html')
                // })
                test: /\.html?$/,
                loader: 'html-loader'
            },
            {
                enforce: 'pre',
                test: /\.tsx?$/,
                use: "source-map-loader"
            },
            // stuffs for correct font-awesome loading
            { test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "url-loader?limit=10000&mimetype=application/font-woff" },
            { test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "file-loader" }

        ]
    },
    plugins: [
        new ExtractTextPlugin("vendor.css"),
        new webpack.optimize.OccurrenceOrderPlugin(),
        // inject vendor.css and index.js links into index.html
        new HtmlWebpackPlugin({
            template: conf.path.src('index.html')
        }),
        // workaround for disable warning about critical dependency
        new webpack.ContextReplacementPlugin(
            /angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/,
            conf.paths.src
        )
    ],
    devtool: 'source-map',
    output: {
        path: path.join(process.cwd(), conf.paths.tmp),
        filename: 'index.js'
    },
    resolve: {
        extensions: [
            '.js',
            '.ts'
        ]
    },
    // entry point
    entry: `./src/index`,

};

Webpack is a module bundler. Webpack是一个模块捆绑器。 It has nothing to do with a tasks runner, although in many cases can substitute the need of gulp or grunt. 它与任务运行器无关,尽管在许多情况下可以替代gulp或grunt的需要。

http://www.acuriousanimal.com/2015/10/15/webpack-not-another-task-runner-tool.html http://www.acuriousanimal.com/2015/10/15/webpack-not-another-task-runner-tool.html

So to answer your questions: 那么回答你的问题:

1.) Yes, it can be used to bundle/minify/etc & output to a dist folder. 1.)是的,它可以用于捆绑/缩小/ etc和输出到dist文件夹。

2.) Running jasmine tests? 2.)运行茉莉花测试? Webpack doesn't really play a part here. Webpack并没有真正发挥作用。 You will likely use an npm script to execute tests directly using the jasmine CLI. 您可能会使用npm脚本直接使用jasmine CLI执行测试。 Unit tests should likely be run on small separate components, not the bundled application as a whole. 单元测试应该可以在小的独立组件上运行,而不是整个捆绑的应用程序。 A decent tutorial here: https://semaphoreci.com/community/tutorials/testing-components-in-angular-2-with-jasmine 这是一个不错的教程: https//semaphoreci.com/community/tutorials/testing-components-in-angular-2-with-jasmine

Yes you definitely can do these things. 是的,你绝对可以做这些事情。 In webpack, you would have to specify an entry point. 在webpack中,您必须指定一个入口点。

First, you need to create a file called webpack.config.js in the root of your project. 首先,您需要在项目的根目录中创建一个名为webpack.config.js的文件。 This is going to be your config file which webpack will try to look into by default. 这将是您的config文件,webpack将默认尝试查看。

It looks like this: 它看起来像这样:

Note: This is a webpack 2 implementation 注意:这是webpack 2实现

module.exports = {
entry: {
  bundle: 'path/to/entry/point' // Note: 'bundle' key will become the name of the bundled file
}

output: {
    path: 'build', // the folder you want your bundle.js to be contained
    filename: '[name].js' // with [name], webpack will automatically look into your entry point key which is 'bundle' in this case
},

// this will tell webpack what files you want to compile and what loaders you would want to use for each
module: {
    rules: [ // from 'loaders' to 'rules'
        {
            test: /\.js$/,
            loader: 'babel-loader',
            exclude: /node_modules/,
        },
        {
            test: /\.sass$/,
            exclude: /node_modules/,
            loader: ExtractTextPlugin.extract({
              fallbackLoader: 'style-loader',
              loader: ['style-loader','sass-loader']
            })
        }
    ]
},

plugins: [
    new ExtractTextPlugin('bundle.css') // what the output file (css) is going to be
]}

Hope this helps :) 希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM