1

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

我们先来看看:

主分支工作正常,测试通过 travis 和本地开发主机。测试由以下纱线脚本(在主服务器上)运行:

$ yarn test

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

  • 将代码库转换ES5ESNext
  • 将代码库移至src/libsrc/tests
  • 在根级别添加了一个generator.js文件,只需要./dist/lib/generator.js
  • 添加了纱线脚本:clear + lint + build + test(参见关于构建管道的自述部分

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

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

$ yarn travis

或(它的别名)

$ yarn pipeline

这是任务序列yarn clear && yarn lint && yarn build && yarn test

但是一项测试失败(14 次测试通过时 1 次测试失败)并出现一个奇怪的错误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.

这是特拉维斯配置

{
  "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 转译之后失败。

我的 PR 有什么问题?

4

1 回答 1

1

该错误确实与 babel transpilation 的后果有关。

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

这是代码./generator.js

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

但:

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

那么所需的模块不是导出的生成器函数,而是一个包含属性的对象default;这个属性的值就是生成器函数。

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

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

就这样!

这是解决问题的 PR,这是该 PR的传递 travis 构建

于 2017-12-28T20:22:27.183 回答