Skip to content

Commit f5d6475

Browse files
committed
Merge branch 'master' of github.com:zeit/ncc
2 parents 01d7547 + 89bcbd1 commit f5d6475

File tree

13 files changed

+293
-5
lines changed

13 files changed

+293
-5
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
"tsconfig-paths-webpack-plugin": "^3.2.0",
9595
"twilio": "^3.23.2",
9696
"typescript": "^3.2.2",
97+
"vm2": "^3.6.6",
9798
"vue": "^2.5.17",
9899
"vue-server-renderer": "^2.5.17",
99100
"webpack": "5.0.0-alpha.0",

src/cli.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ switch (args._[0]) {
171171
{
172172
minify: args["--minify"],
173173
externals: args["--external"],
174-
sourceMap: args["--source-map"] || run && args["--minify"],
174+
sourceMap: args["--source-map"] || run,
175175
cache: args["--no-cache"] ? false : undefined,
176176
watch: args["--watch"]
177177
}
@@ -236,7 +236,13 @@ switch (args._[0]) {
236236
? ["-r", resolve(__dirname, "sourcemap-register")]
237237
: []
238238
});
239-
ps.on("close", () => require("rimraf").sync(outDir));
239+
function exit () {
240+
require("rimraf").sync(outDir);
241+
process.exit();
242+
}
243+
ps.on("exit", exit);
244+
process.on("SIGTERM", exit);
245+
process.on("SIGINT", exit);
240246
}
241247
}
242248
if (args["--watch"]) {

src/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ module.exports = (
4747
assetNames: Object.create(null),
4848
assetPermissions: undefined
4949
};
50+
assetState.assetNames[filename] = true;
51+
if (sourceMap)
52+
assetState.assetNames[filename + '.map'] = true;
5053
nodeLoader.setAssetState(assetState);
5154
relocateLoader.setAssetState(assetState);
5255
// add TsconfigPathsPlugin to support `paths` resolution in tsconfig

src/loaders/relocate-loader.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,6 @@ module.exports = function (code) {
290290
const emitAsset = (assetPath) => {
291291
// JS assets to support require(assetPath) and not fs-based handling
292292
// NB package.json is ambiguous here...
293-
if (assetPath.endsWith('.js') || assetPath.endsWith('.mjs'))
294-
return;
295-
296293
let outName = path.basename(assetPath);
297294

298295
if (assetPath.endsWith('.node')) {
@@ -583,16 +580,28 @@ module.exports = function (code) {
583580
}
584581

585582
// require.main -> __non_webpack_require__.main
583+
// (unless it is a require.main === module check)
586584
else if (!isESM && node.type === 'MemberExpression' &&
587585
node.object.type === 'Identifier' &&
588586
node.object.name === 'require' &&
589587
!shadowDepths.require &&
590588
node.property.type === 'Identifier' &&
591589
node.property.name === 'main' &&
592590
!node.computed) {
591+
if (parent && parent.type === 'BinaryExpression' && (parent.operator === '==' || parent.operator === '===')) {
592+
let other;
593+
other = parent.right === node ? parent.left : parent.right;
594+
if (other.type === 'Identifier' && other.name === 'module')
595+
return;
596+
}
593597
magicString.overwrite(node.object.start, node.object.end, '__non_webpack_require__');
594598
transformed = true;
595599
}
600+
else if (!isESM && node.type === 'Property' && node.value.type === 'Identifier' &&
601+
node.value.name === 'require' && !shadowDepths.require) {
602+
magicString.overwrite(node.value.start, node.value.end, '__non_webpack_require__');
603+
transformed = true;
604+
}
596605

597606
// for now we only support top-level variable declarations
598607
// so "var { join } = require('path')" will only detect in the top scope.

test/integration/vm2.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const {VM} = require('vm2');
2+
new VM().run('console.log("HELLO WORLD")');
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello world
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const fs = require('fs');
2+
console.log(fs.readFileSync(`${__dirname}/asset.txt`));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
module.exports =
2+
/******/ (function(modules, runtime) { // webpackBootstrap
3+
/******/ "use strict";
4+
/******/ // The module cache
5+
/******/ var installedModules = {};
6+
/******/
7+
/******/ // The require function
8+
/******/ function __webpack_require__(moduleId) {
9+
/******/
10+
/******/ // Check if module is in cache
11+
/******/ if(installedModules[moduleId]) {
12+
/******/ return installedModules[moduleId].exports;
13+
/******/ }
14+
/******/ // Create a new module (and put it into the cache)
15+
/******/ var module = installedModules[moduleId] = {
16+
/******/ i: moduleId,
17+
/******/ l: false,
18+
/******/ exports: {}
19+
/******/ };
20+
/******/
21+
/******/ // Execute the module function
22+
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
23+
/******/
24+
/******/ // Flag the module as loaded
25+
/******/ module.l = true;
26+
/******/
27+
/******/ // Return the exports of the module
28+
/******/ return module.exports;
29+
/******/ }
30+
/******/
31+
/******/
32+
/******/
33+
/******/
34+
/******/ // Load entry module and return exports
35+
/******/ return __webpack_require__(717);
36+
/******/ })
37+
/************************************************************************/
38+
/******/ ({
39+
40+
/***/ 66:
41+
/***/ (function(module) {
42+
43+
module.exports = require("fs");
44+
45+
/***/ }),
46+
47+
/***/ 717:
48+
/***/ (function(__unusedmodule, __unusedexports, __webpack_require__) {
49+
50+
const fs = __webpack_require__(66);
51+
console.log(fs.readFileSync(__dirname + '/asset.txt'));
52+
53+
/***/ })
54+
55+
/******/ });
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
module.exports =
2+
/******/ (function(modules, runtime) { // webpackBootstrap
3+
/******/ "use strict";
4+
/******/ // The module cache
5+
/******/ var installedModules = {};
6+
/******/
7+
/******/ // The require function
8+
/******/ function __webpack_require__(moduleId) {
9+
/******/
10+
/******/ // Check if module is in cache
11+
/******/ if(installedModules[moduleId]) {
12+
/******/ return installedModules[moduleId].exports;
13+
/******/ }
14+
/******/ // Create a new module (and put it into the cache)
15+
/******/ var module = installedModules[moduleId] = {
16+
/******/ i: moduleId,
17+
/******/ l: false,
18+
/******/ exports: {}
19+
/******/ };
20+
/******/
21+
/******/ // Execute the module function
22+
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
23+
/******/
24+
/******/ // Flag the module as loaded
25+
/******/ module.l = true;
26+
/******/
27+
/******/ // Return the exports of the module
28+
/******/ return module.exports;
29+
/******/ }
30+
/******/
31+
/******/
32+
/******/
33+
/******/
34+
/******/ // Load entry module and return exports
35+
/******/ return __webpack_require__(717);
36+
/******/ })
37+
/************************************************************************/
38+
/******/ ({
39+
40+
/***/ 66:
41+
/***/ (function(module) {
42+
43+
module.exports = require("fs");
44+
45+
/***/ }),
46+
47+
/***/ 717:
48+
/***/ (function(__unusedmodule, __unusedexports, __webpack_require__) {
49+
50+
const fs = __webpack_require__(66);
51+
console.log(fs.readFileSync(__dirname + '/asset.txt'));
52+
53+
/***/ })
54+
55+
/******/ });

test/unit/main-equal/input.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log(require.main === module);
+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
module.exports =
2+
/******/ (function(modules, runtime) { // webpackBootstrap
3+
/******/ "use strict";
4+
/******/ // The module cache
5+
/******/ var installedModules = {};
6+
/******/
7+
/******/ // The require function
8+
/******/ function __webpack_require__(moduleId) {
9+
/******/
10+
/******/ // Check if module is in cache
11+
/******/ if(installedModules[moduleId]) {
12+
/******/ return installedModules[moduleId].exports;
13+
/******/ }
14+
/******/ // Create a new module (and put it into the cache)
15+
/******/ var module = installedModules[moduleId] = {
16+
/******/ i: moduleId,
17+
/******/ l: false,
18+
/******/ exports: {}
19+
/******/ };
20+
/******/
21+
/******/ // Execute the module function
22+
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
23+
/******/
24+
/******/ // Flag the module as loaded
25+
/******/ module.l = true;
26+
/******/
27+
/******/ // Return the exports of the module
28+
/******/ return module.exports;
29+
/******/ }
30+
/******/
31+
/******/
32+
/******/ // expose the module cache
33+
/******/ __webpack_require__.c = installedModules;
34+
/******/
35+
/******/ // initialize runtime
36+
/******/ runtime(__webpack_require__);
37+
/******/
38+
/******/ // Load entry module and return exports
39+
/******/ return __webpack_require__(__webpack_require__.s = 336);
40+
/******/ })
41+
/************************************************************************/
42+
/******/ ({
43+
44+
/***/ 336:
45+
/***/ (function(module, __unusedexports, __webpack_require__) {
46+
47+
/* module decorator */ module = __webpack_require__.nmd(module);
48+
console.log(__webpack_require__.c[__webpack_require__.s] === module);
49+
50+
/***/ })
51+
52+
/******/ },
53+
/******/ function(__webpack_require__) { // webpackRuntimeModules
54+
/******/ "use strict";
55+
/******/
56+
/******/ /* webpack/runtime/node module decorator */
57+
/******/ !function() {
58+
/******/ __webpack_require__.nmd = function(module) {
59+
/******/ module.paths = [];
60+
/******/ if (!module.children) module.children = [];
61+
/******/ Object.defineProperty(module, 'loaded', {
62+
/******/ enumerable: true,
63+
/******/ get: function() { return module.l; }
64+
/******/ });
65+
/******/ Object.defineProperty(module, 'id', {
66+
/******/ enumerable: true,
67+
/******/ get: function() { return module.i; }
68+
/******/ });
69+
/******/ return module;
70+
/******/ };
71+
/******/ }();
72+
/******/
73+
/******/ }
74+
);

test/unit/main-equal/output.js

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
module.exports =
2+
/******/ (function(modules, runtime) { // webpackBootstrap
3+
/******/ "use strict";
4+
/******/ // The module cache
5+
/******/ var installedModules = {};
6+
/******/
7+
/******/ // The require function
8+
/******/ function __webpack_require__(moduleId) {
9+
/******/
10+
/******/ // Check if module is in cache
11+
/******/ if(installedModules[moduleId]) {
12+
/******/ return installedModules[moduleId].exports;
13+
/******/ }
14+
/******/ // Create a new module (and put it into the cache)
15+
/******/ var module = installedModules[moduleId] = {
16+
/******/ i: moduleId,
17+
/******/ l: false,
18+
/******/ exports: {}
19+
/******/ };
20+
/******/
21+
/******/ // Execute the module function
22+
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
23+
/******/
24+
/******/ // Flag the module as loaded
25+
/******/ module.l = true;
26+
/******/
27+
/******/ // Return the exports of the module
28+
/******/ return module.exports;
29+
/******/ }
30+
/******/
31+
/******/
32+
/******/ // expose the module cache
33+
/******/ __webpack_require__.c = installedModules;
34+
/******/
35+
/******/ // initialize runtime
36+
/******/ runtime(__webpack_require__);
37+
/******/
38+
/******/ // Load entry module and return exports
39+
/******/ return __webpack_require__(__webpack_require__.s = 336);
40+
/******/ })
41+
/************************************************************************/
42+
/******/ ({
43+
44+
/***/ 336:
45+
/***/ (function(module, __unusedexports, __webpack_require__) {
46+
47+
/* module decorator */ module = __webpack_require__.nmd(module);
48+
console.log(__webpack_require__.c[__webpack_require__.s] === module);
49+
50+
/***/ })
51+
52+
/******/ },
53+
/******/ function(__webpack_require__) { // webpackRuntimeModules
54+
/******/ "use strict";
55+
/******/
56+
/******/ /* webpack/runtime/node module decorator */
57+
/******/ !function() {
58+
/******/ __webpack_require__.nmd = function(module) {
59+
/******/ module.paths = [];
60+
/******/ if (!module.children) module.children = [];
61+
/******/ Object.defineProperty(module, 'loaded', {
62+
/******/ enumerable: true,
63+
/******/ get: function() { return module.l; }
64+
/******/ });
65+
/******/ Object.defineProperty(module, 'id', {
66+
/******/ enumerable: true,
67+
/******/ get: function() { return module.i; }
68+
/******/ });
69+
/******/ return module;
70+
/******/ };
71+
/******/ }();
72+
/******/
73+
/******/ }
74+
);

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -11689,6 +11689,11 @@ vlq@^0.2.2:
1168911689
resolved "https://registry.yarnpkg.com/vlq/-/vlq-0.2.3.tgz#8f3e4328cf63b1540c0d67e1b2778386f8975b26"
1169011690
integrity sha512-DRibZL6DsNhIgYQ+wNdWDL2SL3bKPlVrRiBqV5yuMm++op8W4kGFtaQfCs4KEJn0wBZcHVHJ3eoywX8983k1ow==
1169111691

11692+
vm2@^3.6.6:
11693+
version "3.6.6"
11694+
resolved "https://registry.yarnpkg.com/vm2/-/vm2-3.6.6.tgz#169048f0139cf0d0e7c26597ba6ae78af72b28a8"
11695+
integrity sha512-SXC941SEoL0c8+qe+N/PVYsck6vN6fzdkObISlF+YlCHeMCHhHsFD4yuydr1azEYqE8qgxHHkGPOd+iiV9sUyA==
11696+
1169211697
vue-server-renderer@^2.5.17:
1169311698
version "2.5.17"
1169411699
resolved "https://registry.yarnpkg.com/vue-server-renderer/-/vue-server-renderer-2.5.17.tgz#c1f24815a4b12a2797c154549b29b44b6be004b5"

0 commit comments

Comments
 (0)