-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
An object of exported library is undefined if a project uses DllReferencePlugin plugin. #17669
Copy link
Copy link
Closed
Labels
Description
Bug report
What is the current behavior?
- I have a simple main project which contains
dllandappNode.js projects.
Here is the link of the project. - The
dllproject uses DllPlugin to createvender.jsandvendor-manifest.json. They are consumed byappproject. - The
appproject is exported as a library and consumesvendor.jswith 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
DllReferencePluginfrom webpack.config.js, app is exported correctly and there isinitfunction 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-1folder. - 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:23What is the expected behavior?
- The
appproject is exported as a library correctly. - There must be
window.appobject in a browser and it must containinitfunction (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]"
})
],
};Reactions are currently unavailable