Skip to content

An object of exported library is undefined if a project uses DllReferencePlugin plugin. #17669

@aaronamm

Description

@aaronamm

Bug report

What is the current behavior?

  • I have a simple main project which contains dll and app Node.js projects.
    Here is the link of the project.
  • The dll project uses DllPlugin to create vender.js and vendor-manifest.json. They are consumed by app project.
  • The app project is exported as a library and consumes vendor.js with DllReferencePlugin.
  • When using app.js in a browser and call app.int(), a browser show an error because app variable is undefined.
  • However, when I remove DllReferencePlugin from webpack.config.js, app is exported correctly and there is init function in the app object.

If the current behavior is a bug, please provide the steps to reproduce.

  • Clone the demo project from git@github.com:codesanook/webpack-issues.git
  • CD to webpack-issue-1 folder.
  • Build Node.js projects with the following commands: (I use Yarn Workspaces).
    yarn
    yarn workspaces run dev
  • Launch Web Dev server to serve index.html in a dist folder with:
    yarn workspace app run serve
  • You will find an error message in a developer console as a following message:
Uncaught TypeError: Cannot read properties of undefined (reading 'init')
    at index.html:11:23

What is the expected behavior?

  • The app project is exported as a library correctly.
  • There must be window.app object in a browser and it must contain init function (defined in index.js).
  • We can call app.int() in a script block of index.html without any issue.

Other relevant information:
webpack version: 5.88.2
Node.js version: 16.20.1
Operating System: Linux 5.15 Ubuntu 18.04.5 LTS (Bionic Beaver)
Additional tools:

  • Yarn: 1.22.10
  • npm: 8.19.4
  • webpack-cli: 5.1.4
  • @babel/core: 7.22.17
  • babel-loader: 9.1.3
  • webpack-dev-server: 4.15.1
// webpack-issue-1/src/app/webpack.config.js

const webpack = require("webpack");
const path = require("path");
const resolve = path.resolve;
const outputPath = resolve('../../dist');

module.exports = {
  entry: {
    'app': './src/index',
  },
  mode: 'development',
  output: {
    path: outputPath,
    filename: '[name].js',
    library: {
      name: '[name]',
      type: 'var',
      export: 'default',
    },
  },
  resolve: {
    extensions: ['.js']
  },
  module: {
    rules: [
      {
        test: /\.(ts|js)x?$/,
        use: [
          'babel-loader',
        ],
        exclude: /node_modules/
      },
    ]
  },
  plugins: [
    new webpack.DllReferencePlugin({
      manifest: path.join(outputPath, "./vendor-manifest.json")
    }),
  ],
  devServer: {
    static: {
      directory: outputPath,
    },
    compress: true,
    port: 9000,
    open: true,
    client: {
      overlay: false,
    },
  },
};
// webpack-issue-1/src/dll/webpack.config.js

const webpack = require("webpack");
const path = require("path");
const resolve = path.resolve;
const outputPath = resolve('../../dist');

module.exports = {
  entry: {
    'vendor': './src/index',
  },
  mode: 'development',
  output: {
    path: outputPath,
    filename: '[name].js',
    library: '[name]',
  },
  resolve: {
    extensions: ['.js']
  },
  module: {
    rules: [
      {
        test: /\.(ts|js)x?$/,
        use: [
          'babel-loader',
        ],
        exclude: /node_modules/
      },
    ]
  },
  plugins: [
    new webpack.DllPlugin({
      path: path.join(outputPath, "[name]-manifest.json"),
      name: "[name]"
    })
  ],
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions