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

update upstream branch #1

Merged
merged 4 commits into from
Dec 22, 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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@vercel/ncc",
"description": "Simple CLI for compiling a Node.js module into a single file, together with all its dependencies, gcc-style.",
"version": "0.25.1",
"version": "0.26.1",
"repository": "vercel/ncc",
"license": "MIT",
"main": "./dist/ncc/index.js",
Expand All @@ -25,7 +25,7 @@
"@sentry/node": "^4.3.0",
"@slack/web-api": "^5.13.0",
"@tensorflow/tfjs-node": "^0.3.0",
"@zeit/webpack-asset-relocator-loader": "0.8.0",
"@vercel/webpack-asset-relocator-loader": "1.0.0",
"analytics-node": "^3.3.0",
"apollo-server-express": "^2.2.2",
"arg": "^4.1.0",
Expand Down
5 changes: 3 additions & 2 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ async function main() {
{
filename: "ts-loader.js",
minify,
v8cache: true
}
v8cache: true,
noAssetBuilds: true
},
);
checkUnknownAssets('ts-loader', Object.keys(tsLoaderAssets).filter(asset => !asset.startsWith('lib/') && !asset.startsWith('typescript/lib')));

Expand Down
2 changes: 2 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ async function runCmd (argv, stdout, stderr) {
"-s": "--source-map",
"--no-cache": Boolean,
"-C": "--no-cache",
"--no-asset-builds": Boolean,
"--no-source-map-register": Boolean,
"--quiet": Boolean,
"-q": "--quiet",
Expand Down Expand Up @@ -235,6 +236,7 @@ async function runCmd (argv, stdout, stderr) {
externals: args["--external"],
sourceMap: args["--source-map"] || run,
sourceMapRegister: args["--no-source-map-register"] ? false : undefined,
noAssetBuilds: args["--no-asset-builds"] ? true : false,
cache: args["--no-cache"] ? false : undefined,
watch: args["--watch"],
v8cache: args["--v8-cache"],
Expand Down
102 changes: 87 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ const defaultPermissions = 0o666;

const relocateLoader = eval('require(__dirname + "/loaders/relocate-loader.js")');

module.exports = (
module.exports = ncc;
function ncc (
entry,
{
cache,
Expand All @@ -40,16 +41,18 @@ module.exports = (
sourceMap = false,
sourceMapRegister = true,
sourceMapBasePrefix = '../',
noAssetBuilds = false,
watch = false,
v8cache = false,
filterAssetBase = process.cwd(),
existingAssetNames = [],
quiet = false,
debugLog = false,
transpileOnly = false,
license = '',
target
} = {}
) => {
) {
process.env.__NCC_OPTS = JSON.stringify({
quiet
});
Expand All @@ -69,7 +72,7 @@ module.exports = (
const shebangMatch = fs.readFileSync(resolvedEntry).toString().match(shebangRegEx);
const mfs = new MemoryFS();

const existingAssetNames = [filename];
existingAssetNames.push(filename);
if (sourceMap) {
existingAssetNames.push(`${filename}.map`);
existingAssetNames.push(`sourcemap-register${ext}`);
Expand Down Expand Up @@ -132,10 +135,15 @@ module.exports = (

let watcher, watchHandler, rebuildHandler;

const compilationStack = [];

var plugins = [
{
apply(compiler) {
compiler.hooks.compilation.tap("relocate-loader", compilation => relocateLoader.initAssetCache(compilation));
compiler.hooks.compilation.tap("relocate-loader", compilation => {
compilationStack.push(compilation);
relocateLoader.initAssetCache(compilation);
});
compiler.hooks.watchRun.tap("ncc", () => {
if (rebuildHandler)
rebuildHandler();
Expand Down Expand Up @@ -286,7 +294,10 @@ module.exports = (
});
});
})
.then(finalizeHandler);
.then(finalizeHandler, function (err) {
compilationStack.pop();
throw err;
});
}
else {
if (typeof watch === 'object') {
Expand All @@ -296,12 +307,16 @@ module.exports = (
watch.inputFileSystem = compiler.inputFileSystem;
}
let cachedResult;
watcher = compiler.watch({}, (err, stats) => {
if (err)
watcher = compiler.watch({}, async (err, stats) => {
if (err) {
compilationStack.pop();
return watchHandler({ err });
if (stats.hasErrors())
}
if (stats.hasErrors()) {
compilationStack.pop();
return watchHandler({ err: stats.toString() });
const returnValue = finalizeHandler(stats);
}
const returnValue = await finalizeHandler(stats);
if (watchHandler)
watchHandler(returnValue);
else
Expand Down Expand Up @@ -334,16 +349,17 @@ module.exports = (
};
}

function finalizeHandler (stats) {
async function finalizeHandler (stats) {
const assets = Object.create(null);
getFlatFiles(mfs.data, assets, relocateLoader.getAssetPermissions);
getFlatFiles(mfs.data, assets, relocateLoader.getAssetMeta);
// filter symlinks to existing assets
const symlinks = Object.create(null);
for (const [key, value] of Object.entries(relocateLoader.getSymlinks())) {
const resolved = join(dirname(key), value);
if (resolved in assets)
symlinks[key] = value;
}

// Webpack only emits sourcemaps for .js files
// so we need to adjust the .cjs extension handling
delete assets[filename + (ext === '.cjs' ? '.js' : '')];
Expand Down Expand Up @@ -428,22 +444,78 @@ module.exports = (
map.mappings = ";" + map.mappings;
}

// for each .js / .mjs / .cjs file in the asset list, build that file with ncc itself
if (!noAssetBuilds) {
const compilation = compilationStack[compilationStack.length - 1];
let existingAssetNames = Object.keys(assets);
existingAssetNames.push(`${filename}${ext === '.cjs' ? '.js' : ''}`);
const subbuildAssets = [];
for (const asset of Object.keys(assets)) {
if (!asset.endsWith('.js') && !asset.endsWith('.cjs') && !asset.endsWith('.ts') && !asset.endsWith('.mjs') ||
asset.endsWith('.cache.js') || asset.endsWith('.cache.cjs') || asset.endsWith('.cache.ts') || asset.endsWith('.cache.mjs') || asset.endsWith('.d.ts')) {
existingAssetNames.push(asset);
continue;
}
const assetMeta = relocateLoader.getAssetMeta(asset, compilation);
if (!assetMeta || !assetMeta.path) {
existingAssetNames.push(asset);
continue;
}
subbuildAssets.push(asset);
}
for (const asset of subbuildAssets) {
const assetMeta = relocateLoader.getAssetMeta(asset, compilation);
const path = assetMeta.path;
const { code, assets: subbuildAssets, symlinks: subbuildSymlinks, stats: subbuildStats } = await ncc(path, {
cache,
externals,
filename: asset,
minify,
sourceMap,
sourceMapRegister,
sourceMapBasePrefix,
// dont recursively asset build
// could be supported with seen tracking
noAssetBuilds: true,
v8cache,
filterAssetBase,
existingAssetNames,
quiet,
debugLog,
transpileOnly,
license,
target
});
Object.assign(symlinks, subbuildSymlinks);
Object.assign(stats, subbuildStats);
for (const subasset of Object.keys(subbuildAssets)) {
assets[subasset] = subbuildAssets[subasset];
if (!existingAssetNames.includes(subasset))
existingAssetNames.push(subasset);
}
assets[asset] = { source: code, permissions: assetMeta.permissions };
}
}

compilationStack.pop();

return { code, map: map ? JSON.stringify(map) : undefined, assets, symlinks, stats };
}
};
}

// this could be rewritten with actual FS apis / globs, but this is simpler
function getFlatFiles(mfsData, output, getAssetPermissions, curBase = "") {
function getFlatFiles(mfsData, output, getAssetMeta, curBase = "") {
for (const path of Object.keys(mfsData)) {
const item = mfsData[path];
const curPath = `${curBase}/${path}`;
// directory
if (item[""] === true) getFlatFiles(item, output, getAssetPermissions, curPath);
if (item[""] === true) getFlatFiles(item, output, getAssetMeta, curPath);
// file
else if (!curPath.endsWith("/")) {
const meta = getAssetMeta(curPath.substr(1)) || {};
output[curPath.substr(1)] = {
source: mfsData[path],
permissions: getAssetPermissions(curPath.substr(1))
permissions: meta.permissions
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/loaders/relocate-loader.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('@zeit/webpack-asset-relocator-loader');
module.exports = require('@vercel/webpack-asset-relocator-loader');
8 changes: 7 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,17 @@ for (const unitTest of fs.readdirSync(`${__dirname}/unit`)) {
// find the name of the input file (e.g input.ts)
const inputFile = fs.readdirSync(testDir).find(file => file.includes("input"));
await ncc(`${testDir}/${inputFile}`, Object.assign({
transpileOnly: true,
externals: {
'piscina': 'piscina',
'externaltest': 'externalmapped'
}
}, opts)).then(
async ({ code, assets, map }) => {
if (unitTest.startsWith('bundle-subasset')) {
expect(assets['pi-bridge.js']).toBeDefined();
expect(assets['pi-bridge.js'].source.toString()).toContain('Math.PI');
}
const actual = code
.trim()
// Windows support
Expand Down Expand Up @@ -138,7 +144,7 @@ for (const integrationTest of fs.readdirSync(__dirname + "/integration")) {
const stdout = new StoreStream();
const stderr = new StoreStream();
try {
await nccRun(["run", "--no-cache", `${__dirname}/integration/${integrationTest}`], stdout, stderr);
await nccRun(["run", "--no-cache", "--no-asset-builds", `${__dirname}/integration/${integrationTest}`], stdout, stderr);
}
catch (e) {
if (e.silent) {
Expand Down
9 changes: 9 additions & 0 deletions test/unit/bundle-subasset/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import path from 'path';

const file = path.resolve(__dirname, './pi-bridge.js');

const obscureRequire = eval(`function obscureRequire (file) {
require(file);
}`);

console.log(obscureRequire(file));
112 changes: 112 additions & 0 deletions test/unit/bundle-subasset/output-coverage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
module.exports =
/******/ (() => { // webpackBootstrap
/******/ "use strict";
/******/ var __webpack_modules__ = ({

/***/ 929:
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {

__webpack_require__.r(__webpack_exports__);
/* harmony import */ var path__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(622);
/* harmony import */ var path__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(path__WEBPACK_IMPORTED_MODULE_0__);


const file = __webpack_require__.ab + "pi-bridge.js";

const obscureRequire = eval(`function obscureRequire (file) {
require(file);
}`);

console.log(obscureRequire(__webpack_require__.ab + "pi-bridge.js"));


/***/ }),

/***/ 622:
/***/ ((module) => {

module.exports = require("path");

/***/ })

/******/ });
/************************************************************************/
/******/ // The module cache
/******/ var __webpack_module_cache__ = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(__webpack_module_cache__[moduleId]) {
/******/ return __webpack_module_cache__[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = __webpack_module_cache__[moduleId] = {
/******/ // no module.id needed
/******/ // no module.loaded needed
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ var threw = true;
/******/ try {
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
/******/ threw = false;
/******/ } finally {
/******/ if(threw) delete __webpack_module_cache__[moduleId];
/******/ }
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/************************************************************************/
/******/ /* webpack/runtime/compat get default export */
/******/ (() => {
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = (module) => {
/******/ var getter = module && module.__esModule ?
/******/ () => module['default'] :
/******/ () => module;
/******/ __webpack_require__.d(getter, { a: getter });
/******/ return getter;
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/define property getters */
/******/ (() => {
/******/ // define getter functions for harmony exports
/******/ __webpack_require__.d = (exports, definition) => {
/******/ for(var key in definition) {
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ }
/******/ }
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/hasOwnProperty shorthand */
/******/ (() => {
/******/ __webpack_require__.o = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop)
/******/ })();
/******/
/******/ /* webpack/runtime/make namespace object */
/******/ (() => {
/******/ // define __esModule on exports
/******/ __webpack_require__.r = (exports) => {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/compat */
/******/
/******/ __webpack_require__.ab = __dirname + "/";/************************************************************************/
/******/ // module exports must be returned from runtime so entry inlining is disabled
/******/ // startup
/******/ // Load entry module and return exports
/******/ return __webpack_require__(929);
/******/ })()
;
Loading