错误:导出函数或实例的预期file.path

时间:2017-12-28 15:29:01

标签: babeljs generategenerator generate

我为generate-swap-project生成器工具创建了一个名为generate的生成器插件。

我们先来看看:

  

主分支工作正常,测试传递给travis和本地开发主机。 测试由以下纱线脚本(在主机上)运行:

$ yarn test

但是在PR #2 Resolve #1 "Convert to ESNext/StandardJS code style"中,我做了以下更改:

  • ES5代码库转换为ESNext
  • 将代码库移至src/libsrc/tests
  • generator.js文件中添加了一个只需要./dist/lib/generator.js
  • 的根级别
  • 添加了纱线脚本:clear + lint + build + test(cf。Readme section about the build pipeline
  

参考。到分支1-convert-to-standard-code-style

测试由以下纱线脚本(在分支1-convert-to-standard-code-style上)运行:

$ yarn travis

或(别名)

$ yarn pipeline

这是yarn clear && yarn lint && yarn build && yarn test

的任务序列

但是一次测试失败(1次测试失败,14次测试通过),出现一个奇怪的错误:Error: expected file.path to export a function or instance

$ yarn pipeline
yarn run v1.3.2
$ yarn clear && yarn lint && yarn build && yarn test
$ yarn rimraf build dist
$ /home/remi/d/dev/generate-swap-project/node_modules/.bin/rimraf build dist
$ yarn lint:esnext
$ yarn standard src/*.js src/**/*.js
$ /home/remi/d/dev/generate-swap-project/node_modules/.bin/standard 'src/*.js' src/lib/generator.js src/tests/plugin.js src/tests/test.js
$ yarn build:lib && yarn build:tests && yarn build:docs
$ yarn babel src/lib/ --out-dir dist/lib --source-maps
$ /home/remi/d/dev/generate-swap-project/node_modules/.bin/babel src/lib/ --out-dir dist/lib --source-maps
src/lib/generator.js -> dist/lib/generator.js
$ yarn babel src/tests/ --out-dir dist/tests --source-maps
$ /home/remi/d/dev/generate-swap-project/node_modules/.bin/babel src/tests/ --out-dir dist/tests --source-maps
src/tests/plugin.js -> dist/tests/plugin.js
src/tests/test.js -> dist/tests/test.js
$ echo WARNING: No documentation build available yet
WARNING: No documentation build available yet
$ yarn mocha dist/tests/**/*.js
$ /home/remi/d/dev/generate-swap-project/node_modules/.bin/mocha 'dist/tests/**/*.js'


  generate-swap-project
    plugin
      ✓ should add tasks to the instance (80ms)
      ✓ should only register the plugin once

  generate-swap-project
    tasks
      ✓ should extend tasks onto the instance
      ✓ should run the `default` task with .build (186ms)
      ✓ should run the `default` task with .generate
    swap-project (CLI)
X =>  1) should run the default task using the `generate-swap-project` name (global install)
      ✓ should run the default task using the `swap-project` generator alias (local generator.js)
    swap-project (API)
      ✓ should run the default task on the generator
      ✓ should run the `swap-project` task
      ✓ should run the `default` task when defined explicitly
    sub-generator
      ✓ should work as a sub-generator
      ✓ should run the `default` task by default
      ✓ should run the `generator:default` task when defined explicitly
      ✓ should run the `generator:swap-project` task
      ✓ should work with nested sub-generators
      ✓ should run tasks as a sub-generator


  15 passing (1s)
  1 failing

  1) generate-swap-project
      swap-project (CLI)
        should run the default task using the `generate-swap-project` name (global install):
    Error: expected file.path to export a function or instance
      at Env.get (node_modules/base-env/lib/file.js:256:11)
      at Env.invoke (node_modules/base-env/lib/env.js:117:19)
      at Generate.<anonymous> (node_modules/base-generators/lib/generator.js:62:30)
      at Generate.getGenerator (node_modules/base-generators/index.js:157:22)
      at Object.exports.process (node_modules/base-generators/lib/tasks.js:127:24)
      at node_modules/base-generators/lib/tasks.js:86:23
      at Array.reduce (<anonymous>)
      at exports.parse (node_modules/base-generators/lib/tasks.js:85:15)
      at Generate.generate (node_modules/base-generators/index.js:490:21)
      at Generate.generate (node_modules/base-generators/index.js:463:23)
      at Context.<anonymous> (dist/tests/test.js:87:11)



error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

这是travis配置

{
  "sudo": false,
  "language": "node_js",
  "node_js": "lts/carbon",
  "script": [
    "npm link",
    "npm install --global generate .",
    "yarn travis"
  ],
  "group": "stable",
  "dist": "trusty",
  "os": "linux"
}

这是:

我无法理解为什么那个测试(在CLI模式下运行生成器的测试)在ESNext转换和babel转换后失败。

我的公关出了什么问题?

1 个答案:

答案 0 :(得分:1)

这个错误确实与巴贝尔翻译的后果有关。

根级目录中的文件generator.js使用CommonJS模块加载,这是全局generate二进制文件直接调用所需的。

以下是./generator.js的代码:

module.exports = require('./dist/lib/generator')

可是:

  • 因为模块./dist/lib/generator.js./src/lib/generator.js的babel转换的结果,它将生成器导出为默认的匿名函数,
  • 由于模块./generator.jsCJS模块并使用require()

然后所需的模块不是导出的生成器函数,而是包含属性default的对象;并且该属性的值是生成器函数。

因此,实现./generator.js的正确方法如下:

module.exports = require('./dist/lib/generator').default

这就是全部!

  

以下是PR that fix the issue,此PR为the passing travis build