How to bundle a library with webpack?

前端 未结 2 1886
夕颜
夕颜 2021-02-03 22:42

I want to create a frontend library. Therefore I want to use webpack. I especially like the css and image loader. However I can only require non-JS files if I am using webpack.

2条回答
  •  清歌不尽
    2021-02-03 23:28

    You can find good guide for creating libraries in Webpack 2.0 documentation site. That's why I use ver 2 syntax in webpack.config.js for this example.

    Here is a Github repo with an example library.

    It builds all files from src/ (js, png and css) into one JS bundle which could be simply required as an umd module.

    for that we need to specify the follow settings in webpack.config.js:

    output: {
        path: './dist',
        filename: 'libpack.js',
        library: 'libpack',
        libraryTarget:'umd'
    },
    

    and package.json should have:

    "main": "dist/libpack.js",
    

    Note that you need to use appropriate loaders to pack everything in one file. e.g. base64-image-loader instead of file-loader

提交回复
热议问题