Error: expected file.path to export a function or instance
Error: expected file.path to export a function or instance
我为 generate
生成器工具创建了一个名为 generate-swap-project
的生成器插件。
先来看看:
- https://github.com/generate: A new command line tool and developer framework for scaffolding out GitHub projects. A more powerful, composable, functional alternative to Yeoman.
- https://github.com/generate/generate: A new command line tool and developer framework for scaffolding out GitHub projects. Generate offers the robustness and configurability of Yeoman, the expressiveness and simplicity of Slush, and more powerful flow control and composability than either.
- https://github.com/rbecheras/generate-swap-project A generator for SWAP projects (the generator I'm working on)
- https://github.com/rbecheras/generate-swap-project/tree/1-convert-to-standard-code-style: A generator for SWAP projects (the readme on master branch)
- https://github.com/rbecheras/generate-swap-project/tree/1-convert-to-standard-code-style: (the readme on problematic branch)
master 分支工作正常,测试通过了 travis 和本地开发主机。
通过以下 yarn 脚本(在 master 上)进行测试 运行:
$ yarn test
不过在PR #2Resolve #1 "Convert to ESNext/StandardJS code style"
中,我做了如下改动:
- 已将
ES5
代码库转换为 ESNext
- 将代码库移动到
src/lib
和 src/tests
- 在根级别添加了一个
generator.js
文件,只需要 ./dist/lib/generator.js
- 添加了 yarn 脚本:clear + lint + build + test (cf. Readme section about the build pipeline)
Ref. to the branch 1-convert-to-standard-code-style
通过以下 yarn 脚本(在分支 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"
}
这里是:
我不明白为什么在 ESNext 转换和 babel 转译之后那个测试(运行s 生成器在 CLI 模式下)失败了。
我的 PR 有什么问题?
那个bug确实与babel转译的后果有关。
根目录下的文件generator.js
使用CommonJS
模块加载,这是全局generate
二进制文件直接调用所需要的。
这里是./generator.js
的代码:
module.exports = require('./dist/lib/generator')
但是:
- 因为模块
./dist/lib/generator.js
是 ./src/lib/generator.js
的 babel 转译的结果,它将生成器导出为默认的匿名函数,
- 并且由于模块
./generator.js
是一个 CJS
模块并且使用 require()
那么所需的模块不是导出的生成器函数,而是一个包含 属性 default
的对象;这个 属性 的值是生成器函数。
因此,./generator.js
的正确实现方式如下:
module.exports = require('./dist/lib/generator').default
就这些了!
Here is the PR that fix the issue, and here is the passing travis build for that PR.
我为 generate
生成器工具创建了一个名为 generate-swap-project
的生成器插件。
先来看看:
- https://github.com/generate: A new command line tool and developer framework for scaffolding out GitHub projects. A more powerful, composable, functional alternative to Yeoman.
- https://github.com/generate/generate: A new command line tool and developer framework for scaffolding out GitHub projects. Generate offers the robustness and configurability of Yeoman, the expressiveness and simplicity of Slush, and more powerful flow control and composability than either.
- https://github.com/rbecheras/generate-swap-project A generator for SWAP projects (the generator I'm working on)
- https://github.com/rbecheras/generate-swap-project/tree/1-convert-to-standard-code-style: A generator for SWAP projects (the readme on master branch)
- https://github.com/rbecheras/generate-swap-project/tree/1-convert-to-standard-code-style: (the readme on problematic branch)
master 分支工作正常,测试通过了 travis 和本地开发主机。 通过以下 yarn 脚本(在 master 上)进行测试 运行:
$ yarn test
不过在PR #2Resolve #1 "Convert to ESNext/StandardJS code style"
中,我做了如下改动:
- 已将
ES5
代码库转换为ESNext
- 将代码库移动到
src/lib
和src/tests
- 在根级别添加了一个
generator.js
文件,只需要./dist/lib/generator.js
- 添加了 yarn 脚本:clear + lint + build + test (cf. Readme section about the build pipeline)
Ref. to the branch 1-convert-to-standard-code-style
通过以下 yarn 脚本(在分支 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"
}
这里是:
我不明白为什么在 ESNext 转换和 babel 转译之后那个测试(运行s 生成器在 CLI 模式下)失败了。
我的 PR 有什么问题?
那个bug确实与babel转译的后果有关。
根目录下的文件generator.js
使用CommonJS
模块加载,这是全局generate
二进制文件直接调用所需要的。
这里是./generator.js
的代码:
module.exports = require('./dist/lib/generator')
但是:
- 因为模块
./dist/lib/generator.js
是./src/lib/generator.js
的 babel 转译的结果,它将生成器导出为默认的匿名函数, - 并且由于模块
./generator.js
是一个CJS
模块并且使用require()
那么所需的模块不是导出的生成器函数,而是一个包含 属性 default
的对象;这个 属性 的值是生成器函数。
因此,./generator.js
的正确实现方式如下:
module.exports = require('./dist/lib/generator').default
就这些了!
Here is the PR that fix the issue, and here is the passing travis build for that PR.