admin管理员组文章数量:1313140
First real foray into using webpack, and I'm trying to create a reusable library. I can't figure out how to properly export my library classes. Here's a general rundown of what I'm currently doing, and how I'm trying to use what's been built.
I have an entry point like so:
import ClassA from './classA';
import ClassB from './classB';
export {ClassA, ClassB};
That I would like to use the built library to import my classes when needed in my application:
import {ClassA} from './libs/library.js'; // currently using chrome flags for testing
But I can not figure out the right 'output' configuration for my webpack build. I'm using babel-env with a .babelrc like:
{
"presets": [[
"env", {
"targets": {
"browsers": "last 2 versions"
}
}
]]
}
and a webpack config like:
const path = require('path');
export default () => ({
entry: __dirname + '/src/index.js',
output: {
path: path.resolve(__dirname, './libs'),
filename: 'library.js',
libraryTarget: 'umd',
library: ''
},
externals: {},
module: {
rules: [{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "eslint-loader"
}
}]
},
resolve: {
modules: [path.resolve('./node_modules'), path.resolve('./src')],
extensions: ['.json', '.js']
}
});
But, when trying to use my classes, via the import shown earlier, I get an error:
Uncaught SyntaxError: The requested module does not provide an export named 'ClassA'
Anyone have an idea what I'm doing wrong? I'm sure I'm just missing the right configuration of webpack options, but hours of Google have not helped.
First real foray into using webpack, and I'm trying to create a reusable library. I can't figure out how to properly export my library classes. Here's a general rundown of what I'm currently doing, and how I'm trying to use what's been built.
I have an entry point like so:
import ClassA from './classA';
import ClassB from './classB';
export {ClassA, ClassB};
That I would like to use the built library to import my classes when needed in my application:
import {ClassA} from './libs/library.js'; // currently using chrome flags for testing
But I can not figure out the right 'output' configuration for my webpack build. I'm using babel-env with a .babelrc like:
{
"presets": [[
"env", {
"targets": {
"browsers": "last 2 versions"
}
}
]]
}
and a webpack config like:
const path = require('path');
export default () => ({
entry: __dirname + '/src/index.js',
output: {
path: path.resolve(__dirname, './libs'),
filename: 'library.js',
libraryTarget: 'umd',
library: ''
},
externals: {},
module: {
rules: [{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "eslint-loader"
}
}]
},
resolve: {
modules: [path.resolve('./node_modules'), path.resolve('./src')],
extensions: ['.json', '.js']
}
});
But, when trying to use my classes, via the import shown earlier, I get an error:
Uncaught SyntaxError: The requested module does not provide an export named 'ClassA'
Anyone have an idea what I'm doing wrong? I'm sure I'm just missing the right configuration of webpack options, but hours of Google have not helped.
Share Improve this question edited Dec 28, 2017 at 13:13 Steve -Cutter- Blades asked Dec 20, 2017 at 18:19 Steve -Cutter- BladesSteve -Cutter- Blades 5,4423 gold badges29 silver badges45 bronze badges 2-
1
Try
export default
instead of your presentexport
– David R Commented Jan 2, 2018 at 13:50 -
6
Where I'm trying to make multiple classes available from my module, I'm not sure how
export default
would help me?... – Steve -Cutter- Blades Commented Jan 2, 2018 at 13:55
1 Answer
Reset to default 3There seems to be a long-standing feature request in Webpack to resolve this.
In the meantime, you may be able to do a simple import './classA'
, which should add the variables to the global scope (this is the default behaviour of the libraryTarget: 'umd'
option)
本文标签: javascriptHow to properly Webpack library exportStack Overflow
版权声明:本文标题:javascript - How to properly Webpack library export? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741947505a2406508.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论