Webpack to build without including peer dependencies
up vote
1
down vote
favorite
I am building a React library and using Webpack to bundle it. I'm well aware of the concept behind dependencies and peer dependencies.
In the project, React is specified as a peer dependency.
"peerDependencies": {
"react": ">= 16.3.0",
"react-dom": ">= 16.3.0"
}
This is my build script
"build": "webpack --config webpack.config.babel.js"
and this is my webpack.config.babel.js
import path from 'path';
import CleanWebpackPlugin from 'clean-webpack-plugin';
const packageJson = require('./package.json');
export default () => ({
mode: 'production',
entry: {
index: path.join(__dirname, 'src/index.js')
},
output: {
path: path.join(__dirname, 'dist'),
filename: '[name].js',
library: packageJson.name,
libraryTarget: 'umd'
},
module: {
rules: [
{
test: /.jsx?$/,
exclude: /node_modules/,
include: path.join(__dirname, 'src'),
use: [
{
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env', '@babel/preset-react']
}
}
]
},
{
test: /.(scss)$/,
loader: 'style-loader!css-loader!sass-loader'
}
]
},
resolve: {
extensions: ['.js', '.jsx', '.scss']
},
plugins: [new CleanWebpackPlugin(['dist/*.*'])],
optimization: {
splitChunks: {
name: 'vendor',
minChunks: 2
}
}
});
Every time I build, React code is included in the bundled file.
I'm unsure about how to bundle the library without including React in the bundled file.
javascript reactjs webpack
add a comment |
up vote
1
down vote
favorite
I am building a React library and using Webpack to bundle it. I'm well aware of the concept behind dependencies and peer dependencies.
In the project, React is specified as a peer dependency.
"peerDependencies": {
"react": ">= 16.3.0",
"react-dom": ">= 16.3.0"
}
This is my build script
"build": "webpack --config webpack.config.babel.js"
and this is my webpack.config.babel.js
import path from 'path';
import CleanWebpackPlugin from 'clean-webpack-plugin';
const packageJson = require('./package.json');
export default () => ({
mode: 'production',
entry: {
index: path.join(__dirname, 'src/index.js')
},
output: {
path: path.join(__dirname, 'dist'),
filename: '[name].js',
library: packageJson.name,
libraryTarget: 'umd'
},
module: {
rules: [
{
test: /.jsx?$/,
exclude: /node_modules/,
include: path.join(__dirname, 'src'),
use: [
{
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env', '@babel/preset-react']
}
}
]
},
{
test: /.(scss)$/,
loader: 'style-loader!css-loader!sass-loader'
}
]
},
resolve: {
extensions: ['.js', '.jsx', '.scss']
},
plugins: [new CleanWebpackPlugin(['dist/*.*'])],
optimization: {
splitChunks: {
name: 'vendor',
minChunks: 2
}
}
});
Every time I build, React code is included in the bundled file.
I'm unsure about how to bundle the library without including React in the bundled file.
javascript reactjs webpack
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am building a React library and using Webpack to bundle it. I'm well aware of the concept behind dependencies and peer dependencies.
In the project, React is specified as a peer dependency.
"peerDependencies": {
"react": ">= 16.3.0",
"react-dom": ">= 16.3.0"
}
This is my build script
"build": "webpack --config webpack.config.babel.js"
and this is my webpack.config.babel.js
import path from 'path';
import CleanWebpackPlugin from 'clean-webpack-plugin';
const packageJson = require('./package.json');
export default () => ({
mode: 'production',
entry: {
index: path.join(__dirname, 'src/index.js')
},
output: {
path: path.join(__dirname, 'dist'),
filename: '[name].js',
library: packageJson.name,
libraryTarget: 'umd'
},
module: {
rules: [
{
test: /.jsx?$/,
exclude: /node_modules/,
include: path.join(__dirname, 'src'),
use: [
{
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env', '@babel/preset-react']
}
}
]
},
{
test: /.(scss)$/,
loader: 'style-loader!css-loader!sass-loader'
}
]
},
resolve: {
extensions: ['.js', '.jsx', '.scss']
},
plugins: [new CleanWebpackPlugin(['dist/*.*'])],
optimization: {
splitChunks: {
name: 'vendor',
minChunks: 2
}
}
});
Every time I build, React code is included in the bundled file.
I'm unsure about how to bundle the library without including React in the bundled file.
javascript reactjs webpack
I am building a React library and using Webpack to bundle it. I'm well aware of the concept behind dependencies and peer dependencies.
In the project, React is specified as a peer dependency.
"peerDependencies": {
"react": ">= 16.3.0",
"react-dom": ">= 16.3.0"
}
This is my build script
"build": "webpack --config webpack.config.babel.js"
and this is my webpack.config.babel.js
import path from 'path';
import CleanWebpackPlugin from 'clean-webpack-plugin';
const packageJson = require('./package.json');
export default () => ({
mode: 'production',
entry: {
index: path.join(__dirname, 'src/index.js')
},
output: {
path: path.join(__dirname, 'dist'),
filename: '[name].js',
library: packageJson.name,
libraryTarget: 'umd'
},
module: {
rules: [
{
test: /.jsx?$/,
exclude: /node_modules/,
include: path.join(__dirname, 'src'),
use: [
{
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env', '@babel/preset-react']
}
}
]
},
{
test: /.(scss)$/,
loader: 'style-loader!css-loader!sass-loader'
}
]
},
resolve: {
extensions: ['.js', '.jsx', '.scss']
},
plugins: [new CleanWebpackPlugin(['dist/*.*'])],
optimization: {
splitChunks: {
name: 'vendor',
minChunks: 2
}
}
});
Every time I build, React code is included in the bundled file.
I'm unsure about how to bundle the library without including React in the bundled file.
javascript reactjs webpack
javascript reactjs webpack
asked Nov 20 at 5:28
Dinesh Pandiyan
1,884823
1,884823
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
I figured it out.
Webpack has a configuration called externals. Anything specified as an external will not be bundled together.
All I had to do was to add this config to webpack.config.babel.js
externals: {
react: 'react',
reactDOM: 'react-dom'
}
More info here - webpack externals
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
I figured it out.
Webpack has a configuration called externals. Anything specified as an external will not be bundled together.
All I had to do was to add this config to webpack.config.babel.js
externals: {
react: 'react',
reactDOM: 'react-dom'
}
More info here - webpack externals
add a comment |
up vote
2
down vote
accepted
I figured it out.
Webpack has a configuration called externals. Anything specified as an external will not be bundled together.
All I had to do was to add this config to webpack.config.babel.js
externals: {
react: 'react',
reactDOM: 'react-dom'
}
More info here - webpack externals
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
I figured it out.
Webpack has a configuration called externals. Anything specified as an external will not be bundled together.
All I had to do was to add this config to webpack.config.babel.js
externals: {
react: 'react',
reactDOM: 'react-dom'
}
More info here - webpack externals
I figured it out.
Webpack has a configuration called externals. Anything specified as an external will not be bundled together.
All I had to do was to add this config to webpack.config.babel.js
externals: {
react: 'react',
reactDOM: 'react-dom'
}
More info here - webpack externals
answered Nov 20 at 6:16
Dinesh Pandiyan
1,884823
1,884823
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386748%2fwebpack-to-build-without-including-peer-dependencies%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown