【问题标题】:Add third-party library in webpack在 webpack 中添加第三方库
【发布时间】:2020-05-13 13:25:40
【问题描述】:

我正在使用 webpack ...我想在我的 webpack.config.js 文件中使用 3rd 方库(knockoutjs 库),库保存在我的本地目录中

我尝试过使用

const ojL10n = require("D:/SVN/trunk/core/channel/framework/tmp/ojL10n.js")

但我收到错误...定义未在 D:/SVN/trunk/core/channel/framework/tmp/ojL10n.js 中定义

这是我的配置文件

"use strict";

require("amd-loader");

const webpack = require("webpack"),
path = require("path");

ojL10n = require("./tmp/ojL10n.js"), //library stored in local
text = require("./tmp/text.js"), //library stored in local

 webpackConfigurationObject = {
    entry: "./entryModule.js",
    target: "node",
    mode: "development",
    node: {
        __dirname: false
    },
    resolve: {
        alias: {
            text : text,
            ojL10n : ojL10n
        },
        extensions: [".ts",".js", ".json"]
    },
    output: {
        path: path.join(__dirname, "dest"),
        filename: "combinedFile.js",
        library: "combinedFile",
        libraryTarget: "commonjs2"
    },
    module: {
        rules: [{
            test: /\.ts$/,
            include: /src/,
            exclude: /(node_modules)|(dist)/,
            use: {
                loader: "babel-loader"
            },
            parser: { amd: false }
        }]
    },
    externals: {
        jquery: {
            commonjs2 : "jQuery"
        },
        knockout: {
            commonjs2 : "knockout"
        }
    }
};

const compiler = webpack(webpackConfigurationObject),

compiler.run(complete);

【问题讨论】:

  • 你能发布你的 webpack.config.js 吗?您是否尝试将 output.libraryTarget 设置为“umd”?
  • 你也可以尝试添加:{ test: /ojL10n/, loader: 'imports-loader?define=>false' },作为规则加载器
  • @RazRonen 感谢您的回复...我已在问题中添加了我的配置文件代码

标签: webpack knockout.js


【解决方案1】:

我得到了这个问题的解决方案。

resolveLoader: {
    // This adds ./loaders/ to the list of folders where Webpack is looking for loaders
    modules: ["node_modules", path.resolve(__dirname, "./loaders")],
    alias: {
      ojL10n: "ojL10n-loader",
      text: "text-loader"
    }

将 resolveLoader 添加到您的 webpack.config.js 文件中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-17
    • 2013-05-27
    • 1970-01-01
    • 2017-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多