【发布时间】:2018-02-21 08:33:06
【问题描述】:
我有以下 index.js 代码:
import {Asp} from './src/asp.js';
export default Asp;
以及以下 run.js 代码:
import Asp from './dist/bundle.js'; // Uncaught SyntaxError: The requested module does not provide an export named 'default'
import Asp from './index.js'; // works
const myASP = new Asp();
和 webpack (3.11) 配置文件:
const path = require('path');
module.exports = {
entry: './index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
},
module: {
rules: [
{ test: /\.js$/, exclude: /node_modules/, loader: "babel-loader" }
]
}
};
我不明白为什么在我导入时使用 bundle.js 不起作用...帮助...
更新:
我放弃了 webpack,转而使用以下配置解决了我的问题:
import uglify from 'rollup-plugin-uglify';
export default {
input: 'src/index.js',
output:{
file: 'dist/index.js',
name: 'Asp',
format: 'es'
},
plugins: [
uglify()
]
}
我不知道在 webpack 中有什么相当于 this - 但它完成了工作!
【问题讨论】:
-
为什么要在导入之前捆绑它?通常你只会在最后捆绑它。
-
loganfsmyth - 你是什么意思?我不明白你的评论。
-
为什么你有一个
run.js来导入bundle.js,而不是让run.js在你的webpack 配置中成为entry?然后你根本不会导入包,只需将它作为一个整体运行。 -
Loganfsmyth - 我使用 run.js 只是为了测试 'Asp' 作为一个模块。就像“Asp”是您可以导入任何项目的第 3 方对象一样。
-
我很困惑。你说
import Asp from './index.js'; // works,所以我假设你有第二个设置来实际处理这些导入,但是你现在看到的SyntaxError: Unexpected token import错误是因为这些导入,而不是因为 Webpack。您的意思是让run.js使用require?
标签: webpack ecmascript-6