I'm using webpack with vuejs. Webpack does its thing, but when I look at the outputted app.js file, it gives me this error.
'import' and 'export' may only appear at the top level
I'm assuming it's a problem with babel not converting the code? Because I'm getting this in the browser when viewing the application.
Unexpected token import
Here's my entry.js for my vuejs application:
/*jshint esversion: 6 */
import Vue from 'vue';
import App from './App.vue';
import VueRouter from 'vue-router';
Vue.use(VueRouter);
require('./css/style.scss');
// Export the vue router
export var router = new VueRouter({
hashbang: false,
root: '/'
// history: true
});
// Set up routing and match routes to components
router.map({
'/': {
component: require('./components/home/Home.vue')
}
});
// Redirect to the home route if any routes are unmatched
router.redirect({
'*': '/'
});
// Start the app on the #app div
router.start(App, '#app');
Here's my webpack.config.js:
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var path = require('path');
module.exports = {
entry: './src/entry.js',
output: {
filename: './public/js/app.js'
},
devtool: 'source-map',
plugins: [
new ExtractTextPlugin('./public/css/style.css')
],
module: {
preLoaders: [{
test: /\.js$/,
exclude: /node_modules/,
loader: 'jshint-loader'
}],
loaders: [{
test: /\.scss$/,
loader: ExtractTextPlugin.extract(
'style',
'css!sass'
),
},
{
test: /\.vue$/,
loader: 'vue'
},
{
test: /\.js$/,
exclude: /node_modules/,
include: [
path.resolve(__dirname, "src/services"),
],
loader: 'babel-loader',
query: {
presets: ['es2015']
}
}]
}
};
Here's my packages.json file:
{
"name": "test-webpack",
"version": "1.0.0",
"description": "Myapp",
"main": "entry.js",
"scripts": {
"watch": "webpack-dev-server --host $IP --port $PORT --hot --inline --config webpack.config.js",
"dev": "webpack",
"build": ""
},
"author": "Dev",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.9.1",
"babel-loader": "^6.2.4",
"babel-plugin-transform-class-properties": "^6.10.2",
"babel-plugin-transform-runtime": "^6.9.0",
"babel-preset-es2015": "^6.9.0",
"babel-runtime": "^6.9.2",
"css-loader": "^0.23.1",
"extract-text-webpack-plugin": "^1.0.1",
"jshint": "^2.9.2",
"jshint-loader": "^0.8.3",
"node-sass": "^3.8.0",
"path": "^0.12.7",
"sass-loader": "^3.2.0",
"style-loader": "^0.13.1",
"vue-hot-reload-api": "^1.3.2",
"vue-html-loader": "^1.2.2",
"vue-loader": "^8.5.2",
"vue-style-loader": "^1.0.0",
"webpack": "^1.13.1",
"webpack-dev-server": "^1.14.1"
},
"dependencies": {
"vue": "^1.0.25",
"vue-router": "^0.7.13"
}
}
The error "import and export may only appear at the top level" occurs when we forget a closing brace when declaring a function or class, or mix up default and named exports and imports. To solve the error, make sure you haven't forgotten a closing brace in a function's definition.
Top level import is a static import that is located at the very top of the file. However it's called "top-level" not because it is located at the top of the file, but because there are dynamic imports that are not top level: import foo from 'foo' // top level import, static one import('foo').
I got this error when I was missing a closing bracket.
Simplified recreation:
const foo = () => {
return (
'bar'
);
}; <== this bracket was missing
export default foo;
'import' and 'export' may only appear at the top level
This means that webpack is bundling the non-transpiled ES6 code, which is why these import
/export
statements are being found. babel-loader
must therefore not be transpiling what you expect.
If you simply remove the include
and exclude
rules from its loader config, the default behavior of transpiling everything besides what's in node_modules
will kick in. For some reason or another, the current rules are causing some/all files to be skipped.
module.exports = {
entry: './src/entry.js',
output: {
filename: './public/js/app.js'
},
devtool: 'source-map',
plugins: [
new ExtractTextPlugin('./public/css/style.css')
],
module: {
preLoaders: [{
test: /\.js$/,
exclude: /node_modules/,
loader: 'jshint-loader'
}],
loaders: [{
test: /\.scss$/,
loader: ExtractTextPlugin.extract(
'style',
'css!sass'
),
},
{
test: /\.vue$/,
loader: 'vue'
},
{
test: /\.js$/,
loader: 'babel-loader',
query: {
presets: ['es2015']
}
}]
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With