显然我在这里遗漏了一些非常简单的东西,所以我提前为这个愚蠢的问题道歉。我没有错误,因此谷歌很难。
我试图从使用babel和webpack编译的ES6编写的npm包中导出一些东西。
我按照http://jamesknelson.com/using-es6-in-the-browser-with-babel-6-and-webpack/进行了我的webpack设置,大部分都是相同的,但在下面找到它以供参考。我做了一个测试导出回购只是为了确保它在我试图导出的模块的代码中没有任何内容;在下面也找到了。任何帮助将不胜感激;在这一点上,我觉得我正在服用疯狂的药片。
src/index.js
const test = "test";
export default test;
webpack.config.js
var path = require("path");
var webpack = require("webpack");
module.exports = {
entry: [
"babel-polyfill",
"./src/index"
],
output: {
//path: path.join(__dirname, "lib"),
//filename: "[name].js"
filename: "./lib/index.js"
},
// import bare, .js, and .jsx files
resolve: {
extensions: ["", ".js", ".jsx"]
},
devtool: "source-map",
module: {
loaders: [
{
loader: "babel-loader",
// only load src
include: [
path.resolve(__dirname, "src")
],
// only compile .js and .jsx files
test: /\.jsx?$/,
query: {
plugins: ["transform-runtime", "transform-decorators-legacy"],
//plugins: ["transform-decorators-legacy"],
presets: ["es2015", "stage-0", "react"]
}
},
]
},
debug: true
};
package.json
{
"name": "test-package",
"version": "0.0.1",
"description": "test",
"main": "lib/index.js",
"repository": {
"type": "git",
"url": "git+https://github.com/xxx/xxx.git"
},
"scripts": {
"start": "webpack-dev-server"
},
"keywords": [
"es6"
],
"author": "me",
"license": "MIT",
"bugs": {
"url": "https://github.com/xxx/xxx/issues"
},
"homepage": "https://github.com/xxx/xxx#readme",
"dependencies": {
"babel-polyfill": "^6.5.0",
"babel-runtime": "^6.5.0"
},
"devDependencies": {
"babel-core": "^6.5.2",
"babel-loader": "^6.2.3",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-runtime": "^6.5.2",
"babel-preset-es2015": "^6.5.0",
"babel-preset-react": "^6.5.0",
"babel-preset-stage-0": "^6.5.0",
"webpack": "^1.12.14",
"webpack-dev-server": "^1.14.1"
}
}
webpack -p
其他项目
npm i ../test-package
(验证实际安装,搜索"测试"在lib / index.js中找到应该导出的内容)
import test from "test-package";
console.log(test);
console.log(Object.keys(test));
输出:空对象,空数组
答案 0 :(得分:0)
为什么不尝试
import * as test from "test-package";
然后
console.log(test);