前提・実現したいこと
vue.jsの単一ファイルコンポーネント内でモジュールのimport、exportを行いたい。
発生している問題・エラーメッセージ
単一ファイルコンポーネント内のscriptタグ内でimport、exportを行うとビルド時に以下のようなメッセージが出力される。
'import' and 'export' may only appear at the top level (10:0)
該当のソースコード
npm
1{ 2 "name": "kensyo", 3 "version": "1.0.0", 4 "description": "", 5 "main": "index.js", 6 "scripts": { 7 "buildA": "browserify ./src/index.js -o ./dist/index.js" 8 }, 9 "author": "", 10 "license": "ISC", 11 "dependencies": { 12 "axios": "^0.19.0", 13 "jquery": "^3.4.1", 14 "vue.js": "^0.3.2" 15 }, 16 "devDependencies": { 17 "@babel/core": "^7.5.5", 18 "@babel/preset-env": "^7.5.5", 19 "@babel/plugin-transform-runtime": "^7.5.5", 20 "@babel/runtime": "^7.5.5", 21 "babelify": "^10.0.0", 22 "browserify": "^16.5.0", 23 "vueify": "^9.4.1" 24 }, 25 "browserify": { 26 "transform": [ 27 "babelify", 28 "vueify" 29 ] 30 }, 31 "babel": { 32 "presets": [ 33 "@babel/preset-env" 34 ], 35 "plugins": [ 36 "@babel/plugin-transform-runtime" 37 ] 38 } 39}
JavaScript
1import Vue from 'vue' 2import index from './components/indexComponent.vue' 3 4new Vue({ 5 6 el: '#app', 7 render: h => h(index), 8})
sfc
1<template> 2 <div> 3 {{message}} 4 </div> 5</template> 6 7<script> 8//module.exports = { 9export default { 10 11 data : function() { 12 13 return { 14 15 message: 'Hello,Vue.js!' 16 } 17 } 18} 19</script>
試したこと
export defaultをmodule.exportsに書き換えるとビルドは正常終了する。
補足情報(FW/ツールのバージョンなど)
上記のnpm-scriptsに記載
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。