【发布时间】:2017-08-11 10:09:16
【问题描述】:
如何创建可以在 ReactJS 中使用的 webpack 库 ES6。喜欢
-----lib.js-----
export function sum(a , b) {
return a + b;
};
export function multiply(a, b) {
return a * b;
};
我想使用以下导入语句来消费:-
import lib from './lib';
console.log(lib.sum(3,5));
我正在使用以下作为 webpack.config.babel.js
export default () => (
{
entry: './src/lib.js',
output: {
path: './dist',
filename: 'lib.js',
libraryTarget: 'umd',
library: 'lib'
},
module: {
rules: [
{test: /\.js$/, exclude: /node_modules/, loader: "babel-loader"}
]
},
}
);
.babelrc 文件包含以下配置
{
"presets": ["es2015"]
}
package.json 内容
{
"name": "lib",
"version": "1.0.0",
"description": "",
"main": "dist/lib.js",
"scripts": {
"build:lib": "webpack"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.24.0",
"babel-loader": "^6.4.1",
"babel-preset-es2015": "^6.24.0",
"babel-preset-es2015-webpack": "^6.4.3",
"webpack": "^2.2.1"
}
}
【问题讨论】:
-
粘贴
package.json的库,如果您的库代码与使用它的 react 组件位于单独的包中。
标签: reactjs webpack ecmascript-6