Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export build time not found packages on runtime #518

Merged
merged 2 commits into from
Mar 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ dist/**/*.js
!test/integration/*.json
!test/integration/*.js
!test/integration/*.ts
!test/integration/node-path
!test/integration/node-path/*.js
!test/unit
!test/unit/**
!dist/
Expand Down
2 changes: 1 addition & 1 deletion src/@@notfound.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__non_webpack_require__('UNKNOWN');
module.exports = __non_webpack_require__('UNKNOWN');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are cases that the packages are missing at ncc build time, yet presented at global scope or runtime. Without this fix, the eval("require")('dep') can succeed at runtime but the result is not exported.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are cases that the packages are missing at ncc build time, yet presented at global scope or runtime.

Which cases? Can you add a test that fails but succeeds with this PR?

Copy link
Contributor Author

@legendecas legendecas Mar 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's say we have a project referencing an optional dependency using

try {
  foo = require('foo');
} catch {}

Package foo is not installed on build time since it's totally optional and should not be bundled into the products. So package foo will be transformed by loader runtime-notfound to eval("require")("foo") without assigning return value to module.exports. Since then the require('foo') can succeed without any error at runtime but the return value is undefined, not the dynamically required package module.

The test case test/unit/runtime-notfound/output.js is already presented in the repo, and is identical to the case above, which has been updated in the PR to reflect the correct result.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I should have been more clear. Can you add an integration test that fails with the latest ncc but succeeds with this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Integration test added, PTAL :)

1 change: 1 addition & 0 deletions test/integration/node-path/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'foo'
24 changes: 24 additions & 0 deletions test/integration/notfound-eval.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const assert = require('assert');
const childProcess = require('child_process');
const path = require('path');

(function main() {
if (process.env.CHILD !== 'notfound-eval') {
const cp = childProcess.fork(__filename, [], {
stdio: 'inherit',
env: Object.assign({}, process.env, {
CHILD: 'notfound-eval',
NODE_PATH: path.join(process.cwd(), 'test/integration/node-path')
})
});
cp.on('exit', (code) => {
if (code == null) {
code = 1;
}
process.exit(code);
})
return;
}
const foo = require('foo');
assert.strictEqual(foo, 'foo');
})();
8 changes: 4 additions & 4 deletions test/unit/runtime-notfound/output-coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,17 @@ __webpack_require__(497);
/***/ }),

/***/ 464:
/***/ (function() {
/***/ (function(module) {

eval("require")("./not-found.js");
module.exports = eval("require")("./not-found.js");


/***/ }),

/***/ 497:
/***/ (function() {
/***/ (function(module) {

eval("require")("./not-foud2.js");
module.exports = eval("require")("./not-foud2.js");


/***/ })
Expand Down
8 changes: 4 additions & 4 deletions test/unit/runtime-notfound/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,17 @@ __webpack_require__(695);
/***/ }),

/***/ 367:
/***/ (function() {
/***/ (function(module) {

eval("require")("./not-found.js");
module.exports = eval("require")("./not-found.js");


/***/ }),

/***/ 695:
/***/ (function() {
/***/ (function(module) {

eval("require")("./not-foud2.js");
module.exports = eval("require")("./not-foud2.js");


/***/ })
Expand Down
4 changes: 2 additions & 2 deletions test/unit/tsconfig-paths-allowjs/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ module.exports =
/******/ ({

/***/ 17:
/***/ (function() {
/***/ (function(module) {

eval("require")("@module");
module.exports = eval("require")("@module");


/***/ }),
Expand Down
4 changes: 2 additions & 2 deletions test/unit/tsconfig-paths-conflicting-external/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ module.exports =
/******/ ({

/***/ 17:
/***/ (function() {
/***/ (function(module) {

eval("require")("@module");
module.exports = eval("require")("@module");


/***/ }),
Expand Down
4 changes: 2 additions & 2 deletions test/unit/tsconfig-paths/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ module.exports =
/******/ ({

/***/ 17:
/***/ (function() {
/***/ (function(module) {

eval("require")("@module");
module.exports = eval("require")("@module");


/***/ }),
Expand Down