首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Webpack分包共享文件

Webpack分包共享文件
EN

Stack Overflow用户
提问于 2021-03-18 12:29:11
回答 1查看 109关注 0票数 0

webpack 5.26

我有公共文件js,它是跨包使用的通用函数。

CommonFunctions

  • Fun1
  • Fun2

Budle1

  • CommonFunction - Fun1

Bundle2

  • CommonFunction - Fun2

如果对CommonFunction有任何更改。Bundle1和Budle2由webpack更新

只需要适当的包就应该更新。这里webpack配置文件

代码语言:javascript
复制
  module.exports = {
    entry: {
        bundle1: './bundle1.ts',
        bundle2: './bundle2.ts'
    },
    resolve: {
        extensions: ['.ts'] // add your other extensions here
    },
    output: {
        filename: '[name].bundle.js',
        path: path.resolve(__dirname, "../JsFiles"),
        library: "testCommon",

    },
    module: {
        rules: [{ test: /\.ts$/, use: 'awesome-typescript-loader' }],
    },

    mode: 'development'
}


--CommonFunctions.ts
export class CommonFunctions {
    Fun1() { };
    Fun2() { };
}

--bundle1.ts

import CommonFunctions from '../Js'
export class bundle1 {
    b1() { CommonFunctions.Fun1 }
}

--bundle2.ts
import CommonFunctions from '../Js'
export class bundle2 {
    b2() { CommonFunctions.Fun2 }
}

如何用webpack制作共用共享好友的文件/模块?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-19 10:50:58

代码语言:javascript
复制
optimization: {
    splitChunks: {
        cacheGroups: {
            Util: {
                test: path.resolve(__dirname, 'CommonFunctions'),
                name: 'CommonFunctions',
                enforce: true,
                chunks: "all"
            },
            
        },
    },
},

通过创建单独的块,webpack被创建为分离的JS文件,现在我可以单独加载公共文件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66691113

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档