javascript - Pass export through a main file in webpack - Stack Overflow

I am trying to build a library using webpack. When consuming my library, I want to be able to call myLi

I am trying to build a library using webpack. When consuming my library, I want to be able to call myLibrary.showAlert() from a script tag or other js document. My issue is that I have multiple files all being imported into a main.js file which is the entry point of my library. If I import and export each file in main.js, it works. My question is, is there a way to export all files without listing each one individually?

Example:

//alerts.js
export function showAlert() {
  console.log("show alert called");
}

//translations.js
export function translateText() {
  console.log("translate text called");
}

//main.js
import "./components/alerts.js";
import "./components/translations.js";

     //Note: I want to avoid this part
export * from "/components/alerts.js";
export * from "/components/translations.js";

//webpack.library.config.js
const path = require("path");
const libraryName = "rmsui";
var configLib = Object.assign({}, config, {
    mode: "development",
    name: "myLibrary",
    entry: {
        // JS
        myLibrary: "./js/main.js",
        errorPages: "./js/errorPages.js" //some other entrypoint not shown above
    },
    output: {
        path: path.resolve(__dirname, "./wwwroot/dist"),
        filename: "js/[name].js",
        globalObject: "this",
        library: {
            name: libraryName,
            type: "umd",
        },
        libraryTarget: "var",
        //umdNamedDefine: true,
        assetModuleFilename: "assets/[hash][ext][query]"
    },
    plugins: [
        //new CleanWebpackPlugin(),
        new MiniCssExtractPlugin({
            filename: "css/[name].css"
        })
    ]
});
module.exports = configLib;

I am trying to build a library using webpack. When consuming my library, I want to be able to call myLibrary.showAlert() from a script tag or other js document. My issue is that I have multiple files all being imported into a main.js file which is the entry point of my library. If I import and export each file in main.js, it works. My question is, is there a way to export all files without listing each one individually?

Example:

//alerts.js
export function showAlert() {
  console.log("show alert called");
}

//translations.js
export function translateText() {
  console.log("translate text called");
}

//main.js
import "./components/alerts.js";
import "./components/translations.js";

     //Note: I want to avoid this part
export * from "/components/alerts.js";
export * from "/components/translations.js";

//webpack.library.config.js
const path = require("path");
const libraryName = "rmsui";
var configLib = Object.assign({}, config, {
    mode: "development",
    name: "myLibrary",
    entry: {
        // JS
        myLibrary: "./js/main.js",
        errorPages: "./js/errorPages.js" //some other entrypoint not shown above
    },
    output: {
        path: path.resolve(__dirname, "./wwwroot/dist"),
        filename: "js/[name].js",
        globalObject: "this",
        library: {
            name: libraryName,
            type: "umd",
        },
        libraryTarget: "var",
        //umdNamedDefine: true,
        assetModuleFilename: "assets/[hash][ext][query]"
    },
    plugins: [
        //new CleanWebpackPlugin(),
        new MiniCssExtractPlugin({
            filename: "css/[name].css"
        })
    ]
});
module.exports = configLib;

Share Improve this question edited Mar 20 at 23:33 Hilory 2,1417 gold badges14 silver badges30 bronze badges asked Mar 20 at 17:35 Lee HarringtonLee Harrington 111 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

try this solution: Modify your main.js file like this:

    const modules = require.context("./components", false, /\.js$/);
    
    modules.keys().forEach((key) => {
        Object.assign(exports, modules(key));
    });

require.context("./components", false, /.js$/) loads all .js files from the components directory.

.keys() gets all file paths.

.forEach() iterates over them and imports the modules.

Object.assign(exports, modules(key)) merges all exports into a single object.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744392636a4571983.html

相关推荐

  • javascript - Pass export through a main file in webpack - Stack Overflow

    I am trying to build a library using webpack. When consuming my library, I want to be able to call myLi

    7天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信