gpt4 book ai didi

javascript - 使用 Webpack 5 构建 ES 模块包,并在 Node.js ES 模块中使用该包

转载 作者:行者123 更新时间:2023-12-05 00:30:17 26 4
gpt4 key购买 nike

是否可以使用 Webpack 5 构建 ES 模块包(使用默认导出),然后在 Node.js ES 模块中使用(导入)该包?

  • 这是 live demo以下文件。

  • 我有以下文件:
    |- webpack.config.js
    |- package.json
    |- /src
    |- index.js
    index.js
    function util() {
    return "I'm a util!";
    }
    export default util;
    webpack.config.js
    const path = require('path');

    module.exports = {
    entry: './src/index.js',
    output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist')
    },
    experiments: {
    outputModule: true,
    },
    };
    package.json
    {
    "name": "exporter",
    "version": "1.0.0",
    "module": "dist/index.js",
    "scripts": {
    "build": "webpack"
    }
    }
    运行后 npm run build ,我们得到:
    |- webpack.config.js
    |- package.json
    |- /src
    |- index.js
    |- /dist
    |- bundle.js
    太好了,现在我只想创建一些“演示”文件 importer.js这将导入 util并使用它。为方便起见,我将在同一个文件夹中创建它:
    |- importer.js
    |- webpack.config.js
    |- package.json
    |- /src
    |- index.js
    |- /dist
    |- bundle.js
    importer.js
    import util from './dist/bundle.js';

    console.log(util());
    现在我运行 node importer.js ,我得到这个(预期的)错误:
    Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
    [...]
    SyntaxError: Cannot use import statement outside a module
    好吧,让我们添加 "type": "module"package.json :
    package.json
    {
    "name": "exporter",
    "version": "1.0.0",
    "module": "dist/index.js",
    "scripts": {
    "build": "webpack"
    },
    "type": "module"
    }
    现在,再试一次 node importer.js给我们另一个错误:
    SyntaxError: The requested module './dist/bundle.js' does not provide an export named 'default'
    更重要的是,当试图重新运行 npm run build ,我们得到另一个错误:
    [webpack-cli] Failed to load 'path\to\webpack.config.js' config
    [webpack-cli] ReferenceError: require is not defined
  • 注:Webpack 5 and ESM可能被认为有些相关,但实际上并非如此,因为它想使用 webpack.config.js本身作为一个 ES 模块。

  • 我怎样才能让它工作?

    最佳答案

    由于您使用 type: "module" , Node.js 会将此模块视为 ESM。来自文档 esm_no_require_exports_or_module_exports , require , module.exports , __dirname在 ESM 中不可用。我将使用 ESM 语法重写 webpack.config.js .
    __dirname 创建一个 polyfill多变的。
    来自 webpack 文档:

    If you're using webpack to compile a library to be consumed by others, make sure to set output.libraryTarget to 'module' when output.module is true.


    例如。 webpack.config.js :
    import path from "path";
    import { fileURLToPath } from "url";

    const __dirname = path.dirname(fileURLToPath(import.meta.url));

    export default {
    mode: "development",
    entry: "./src/index.js",
    output: {
    filename: "bundle.js",
    path: path.resolve(__dirname, "dist"),
    library: {
    type: "module",
    },
    },
    experiments: {
    outputModule: true,
    },
    };
    package.json :
    {
    "name": "exporter",
    "version": "1.0.0",
    "main": "dist/bundle.js",
    "scripts": {
    "build": "webpack"
    },
    "devDependencies": {
    "webpack": "^5.51.1",
    "webpack-cli": "^4.8.0"
    },
    "type": "module"
    }
    ./src/index.js :
    function util() {
    return "I'm a util!";
    }
    export default util;
    importer.js :
    import util from "./dist/bundle.js";

    console.log(util());
    build :
    ⚡  npm run build   

    > exporter@1.0.0 build /Users/dulin/workspace/github.com/mrdulin/webpack-samples/packages/webpack-v5/stackoverflow/68913996
    > webpack

    asset bundle.js 3.01 KiB [compared for emit] [javascript module] (name: main)
    runtime modules 670 bytes 3 modules
    ./src/index.js 65 bytes [built] [code generated]
    webpack 5.51.1 compiled successfully in 87 ms
    执行 importer.js :
    ⚡  node importer.js 
    I'm a util!

    关于javascript - 使用 Webpack 5 构建 ES 模块包,并在 Node.js ES 模块中使用该包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68913996/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com