简体   繁体   English

Vueify:'import'和'export'可能只出现'sourceType:module'

[英]Vueify: 'import' and 'export' may appear only with 'sourceType: module'

I am using Vue. 我正在使用Vue。 This is how I'm building in my gulpfile: 这就是我在gulpfile中构建的方式:

browserify('./main.js')
.transform(vueify)
.bundle()
.pipe( fs.createWriteStream('./build/bundle.js') );

The problem is vueify doesn't handle the es6 exports in my .js files. 问题是vueify不处理我的.js文件中的es6导出。 It only works in .vue components. 它只适用于.vue组件。 It works with module.exports , but I'd like to take advantage of es6 code in my .js files. 它适用于module.exports ,但我想利用我的.js文件中的es6代码。

When bundle() is called, I currently get the error: 调用bundle() ,我当前收到错误:

'import' and 'export' may appear only with 'sourceType: module'

Is there any way to modify the gulpfile to handle js files using es6 that my .vue components are importing? 有没有办法修改gulp文件来使用我的.vue组件导入的es6来处理js文件?

After hours of struggling, I finally figured it out. 经过几个小时的挣扎,我终于明白了。

  1. Install babelify: npm install --save-dev babelify 安装babelify: npm install --save-dev babelify
  2. Add this to the top of your gulpfile: var babelify = require( 'babelify' ) ; 将其添加到gulpfile的顶部: var babelify = require( 'babelify' ) ;
  3. Add .transform( babelify ) : 添加.transform( babelify )

     return browserify('./main.js') //tells browserify where to start, but doesn't run the resolve algorithm yet .transform( vueify ) //executes vueify to transform vue components to js .transform( babelify ) //executes babelify to transform es6 to es5 .bundle() //runs the module resolve algorithm on all the require() statements .pipe( fs.createWriteStream('./build/bundle.js') ); 

OK, I've been running into this for several days trying to get Axios to work with Vue and the OP's self-answer saved me . 好吧,我已经跑了好几天试图让Axios与Vue合作,OP的自我回答救了我 That being said, I'm not using Gulp (yet) and had to do several different steps. 话虽这么说,我还没有使用Gulp(还),不得不做几个不同的步骤。

My problem was actually with the import command. 我的问题实际上是使用import命令。 It would error during the Browserify compile and at runtime the error axios is undefined was ocurring. 它在Browserify 编译期间会出错,并且在运行时错误axios is undefined是错误的。

The script section of my .vue file is as follows: 我的.vue文件的脚本部分如下:

<script>
  import axios from 'axios';

  module.exports = {
    name: "address-search",
    data: function() {
      return {
        valid: false,
        hash: '',
        errors: []
      }
    },
    methods: {
      async validateAddress() {
        const query = `./validateAddress/` + this.hash;      
        try {
          const response = await axios.get(query);
          this.valid = response.data;
        } catch (e) {
          this.errors.push(e)
        }
      }
    }
  }
</script>

My bundle command is as follows: 我的bundle命令如下:

browserify -t vueify -e public/javascripts/main.js -o public/javascripts/bundle.js

And during compile I was getting the error stated by the OP: 编译期间,我收到了OP声明的错误:

'import' and 'export' may appear only with 'sourceType: module'

The steps I took included: 我采取的步骤包括:

  1. Install babel-core: npm install --save-dev babel-core 安装babel-core: npm install --save-dev babel-core
  2. Install babelify (per OP): npm install --save-dev babelify 安装babelify(每个OP): npm install --save-dev babelify
  3. Install bable-preset-es2015: npm install --save-dev babel-preset-es2015 babel-plugin-transform-runtime 安装bable-preset-es2015: npm install --save-dev babel-preset-es2015 babel-plugin-transform-runtime
  4. Rerun browserify -t vueify -e public/javascripts/main.js -o public/javascripts/bundle.js 重新运行browserify -t vueify -e public/javascripts/main.js -o public/javascripts/bundle.js

Voila! 瞧! Now the import directive works and Axios is happy. 现在import指令有效,Axios很高兴。

In retrospect, it is somewhat obvious why it didn't work in the first place (no mapping for the newer Javascript syntax) but I certainly hope this helps someone else get to delivering business value instead of spending too much time on library management. 回想起来,有点明显为什么它首先不起作用(没有新的Javascript语法的映射)但我当然希望这有助于其他人提供业务价值,而不是花太多时间在图书馆管理上。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “导入”和“导出”可能仅与“ sourceType:模块”一起出现 - 'import' and 'export' may appear only with 'sourceType: module' Browserify - ParseError: &#39;import&#39; 和 &#39;export&#39; 可能只与 &#39;sourceType: 模块一起出现 - Browserify - ParseError: 'import' and 'export' may appear only with 'sourceType: module 另一个ParseError:&#39;import&#39;和&#39;export&#39;可能只出现&#39;sourceType:module&#39;:( - Another ParseError: 'import' and 'export' may appear only with 'sourceType: module' :( SyntaxError: &#39;import&#39; 和 &#39;export&#39; 可能只与 &#39;sourceType: module&#39; 一起出现 - Gulp - SyntaxError: 'import' and 'export' may appear only with 'sourceType: module' - Gulp Babel 7 不转译 node_modules 中的依赖项:&#39;import&#39; 和 &#39;export&#39; 可能只与 &#39;sourceType: module&#39; 一起出现 - Babel 7 not transpiling dependencies in node_modules: 'import' and 'export' may appear only with 'sourceType: module' SyntaxError:&#39;import&#39;和&#39;export&#39;可能只出现在Gulp + Babel + TypeScript + Source Maps中的&#39;sourceType:module&#39; - SyntaxError: 'import' and 'export' may appear only with 'sourceType: module' in Gulp + Babel + TypeScript + Source Maps ParseError: &#39;import&#39; 和 &#39;export&#39; 可能只出现在 &#39;sourceType: module&#39; - swiper.esm.js - ParseError: 'import' and 'export' may appear only with 'sourceType: module' - swiper.esm.js 升级 Gulp 后,javascript 抛出语法错误“导入声明可能只出现在模块的顶层” - After upgrading Gulp, javascript is throwing an syntax-error “import declarations may only appear at top level of a module” Vueify 和 Babel 7 - Vueify and Babel 7 使用Vueify Typescript的Vue错误 - Vue error using Vueify Typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM