Skip to content
This repository has been archived by the owner on Jun 4, 2022. It is now read-only.

Commit

Permalink
Node 11 with Nexe legacy (#465)
Browse files Browse the repository at this point in the history
  • Loading branch information
anmonteiro authored Apr 10, 2019
1 parent c64e773 commit f7bba1f
Show file tree
Hide file tree
Showing 12 changed files with 2,076 additions and 901 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ build-shared: &build-shared
<<: *node-version-file-cmd
- restore_cache:
keys:
- lumo-nogh3Eta-{{ .Branch }}-{{ checksum "node-version" }}-{{ checksum "build.boot" }}-{{ checksum "yarn.lock" }}
- lumo-nogh4Eta-{{ .Branch }}-{{ checksum "node-version" }}-{{ checksum "build.boot" }}-{{ checksum "yarn.lock" }}
# Any branch if there are none on the default branch - this should be unnecessary if you have your default branch configured correctly
- v1-dep-
- run:
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

## [1.10.0](https://github.com/anmonteiro/lumo/compare/1.9.0...1.10.0) (2019-04-09)

**Important Note**: This version is broken and was released by mistake. Do not
use.

### Changes

- Upgrade Node.js to version 11.13.0.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"google-closure-compiler-js": "20170910.0.1",
"jest-cli": "23.6.0",
"jszip": "github:anmonteiro/jszip#patch-1",
"nexe": "github:anmonteiro/nexe#anmonteiro/fs",
"nexe": "github:anmonteiro/nexe#anmonteiro-patch-1",
"node-fetch": "^2.2.1",
"paredit.js": "0.3.4",
"posix-getopt": "github:anmonteiro/node-getopt#master",
Expand Down
6 changes: 3 additions & 3 deletions scripts/lumo_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
(do (println "expected:" (pr-str (:expected m)))
(print " actual:\n")
(println (.-message actual)))
(cljs.test/print-comparison m))
(#'cljs.test/print-comparison m))
(when lumo.test-util/*print-stack-traces*
(println (.-stack actual)))))

(defmethod cljs.test/report [:cljs.test/default :fail] [m]
(cljs.test/inc-report-counter! :error)
(println "\nERROR in" (cljs.test/testing-vars-str m))
(println "\nFAIL in" (cljs.test/testing-vars-str m))
(when (seq (:testing-contexts (cljs.test/get-current-env)))
(println (cljs.test/testing-contexts-str)))
(when-let [message (:message m)] (println message))
Expand All @@ -37,7 +37,7 @@
(do (println "expected:" (pr-str (:expected m)))
(print " actual:\n")
(println (.-message actual)))
(cljs.test/print-comparison m))
(#'cljs.test/print-comparison m))
(when lumo.test-util/*print-stack-traces*
(println (.-stack actual)))))

Expand Down
130 changes: 51 additions & 79 deletions scripts/package.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const nexe = require('nexe');
const fs = require('fs').promises;
const async = require("async");
const nexe = require('../vendor/nexe');
const monkeyPatch = require('../vendor/nexe/monkeypatch');
const fs = require('fs');
const path = require('path');
const os = require('os');
const zlib = require('zlib');
Expand All @@ -8,36 +10,36 @@ const embed = require('./embed');
const argv = process.argv.slice(0);
const nodeVersion = argv[2];

async function getDirContents(dir, accumPath = dir) {
let filenames = await fs.readdir(dir);
function getDirContents(dir, accumPath = dir) {
let filenames = fs.readdirSync(dir);

return filenames.reduce(async (previousPromise, filename) => {
const ret = await previousPromise;
return filenames.reduce((ret, filename) => {
const fname = path.resolve(accumPath, filename);
const fStat = await fs.stat(fname);
const fStat = fs.statSync(fname);

if (fStat.isDirectory()) {
const newAccum = path.join(accumPath, filename);
return ret.concat(await getDirContents(newAccum, newAccum));
return ret.concat(getDirContents(newAccum, newAccum));
}

ret.push(path.join(accumPath, filename));
return ret;
}, Promise.resolve([]));
}, []);
}

async function deflate(fname) {
const input = await fs.readFile(fname);

await fs.writeFile(fname, zlib.deflateSync(input));
return;
function deflate(fname) {
return new Promise((resolve, reject) => {
fs.readFile(fname, (err, input) => {
fs.writeFileSync(fname, zlib.deflateSync(input));
resolve();
});
});
}

const isWindows = /^Windows/.test(os.type());
const outputPath = `build/${isWindows ? 'lumo.exe' : 'lumo'}`;

const resources = getDirContents('target').then(resources =>
resources.filter(
const resources = getDirContents('target').filter(
fname =>
fname.endsWith('.aot.js.map') ||
(!fname.endsWith('main.js') &&
Expand All @@ -47,68 +49,38 @@ const resources = getDirContents('target').then(resources =>
!fname.endsWith('aot.edn') &&
!/target[\\\/]cljs[\\/]core.js/.test(fname) &&
!fname.endsWith('.map')),
),
);
async function moveLibs(compiler, callback) {
const contents = await fs.readFile('target/google-closure-compiler-js.js');

await compiler.writeFileAsync('google-closure-compiler-js.js', contents);

return callback();
}

async function patchNodeGyp(compiler, callback) {
await compiler.replaceInFileAsync(
'node.gyp',
"'deps/node-inspect/lib/internal/inspect_repl.js',",
`'deps/node-inspect/lib/internal/inspect_repl.js',
'google-closure-compiler-js.js',`,
);

return callback();
}

resources.then(resources =>
Promise.all(resources.map(deflate)).then(async () => {
embed(resources, 'target');

try {
await fs.mkdir('build');
} catch (_) {}

nexe.compile(
{
input: 'target/bundle.min.js',
output: outputPath,
build: true,
targets: [nodeVersion],
bundle: false,
temp: 'tmp',
patches: [moveLibs, patchNodeGyp],
configure: [
'--without-dtrace',
'--without-npm',
'--without-inspector',
'--without-etw',
'--with-snapshot',
].concat(isWindows ? ['--openssl-no-asm'] : []),
make: ['-j', '8'],
vcBuild: ['nosign', 'x64', 'noetw', 'vs2017'],
name: 'Lumo',
enableNodeCli: false,
snapshot: 'target/main.js',
warmup: 'target/main.js',
verbose: true,
fs: false,
},
err => {
if (err) {
throw err;
}
console.log(
`Finished bundling. Nexe binary can be found in ${outputPath}`,
);
},
);
}),
);
Promise.all(resources.map(deflate)).then(() => {
embed(resources, 'target');

nexe.compile(
{
input: 'target/bundle.min.js',
output: outputPath,
nodeTempDir: 'tmp',
nodeConfigureArgs: [
'--without-dtrace',
'--without-npm',
'--without-inspector',
'--without-etw',
'--with-snapshot',
].concat(isWindows ? ['--openssl-no-asm'] : []),
nodeMakeArgs: ['-j', '8'],
nodeVCBuildArgs: ['nosign', 'x64', 'noetw'],
flags: true,
startupSnapshot: 'target/main.js',
noBundle: true,
framework: 'node',
nodeVersion,
},
err => {
if (err) {
throw err;
}
console.log(
`Finished bundling. Nexe binary can be found in ${outputPath}`,
);
},
);
});
137 changes: 137 additions & 0 deletions vendor/nexe/bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/**
* Copyright (c) 2013 Craig Condon
* Copyright (c) 2015-2016 Jared Allard
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
**/

'use strict';

let browserify = require('browserify'),
path = require("path"),
spawn = require('child_process').spawn,
insertGlobals = require('insert-module-globals'),
fs = require("fs"),
async = require("async"),
_log = require("./log");

/**
* User browserify to create a "packed" file.
*
* @param {string} input - input file
* @param {string} nc - node compiler dir
* @param {array} options - nexe options
* @param {function} complete - next function to call (async)
**/
function bundle(input, nc, options, complete) {
const bundlePath = path.join(nc, "lib", "nexe.js");
const mapfile = options.output+'.map';
let ws = fs.createWriteStream(bundlePath);

const igv = '__filename,__dirname,_process';
let insertGlobalVars = { isNexe: true },
wantedGlobalVars = igv.split(',');

// parse insertGlobalVars.
Object.keys(insertGlobals.vars).forEach(function (x) {
if (wantedGlobalVars.indexOf(x) === -1) {
insertGlobalVars[x] = undefined;
}
});

let paths = [path.join(nc, 'lib')];

if(options.browserifyPaths) {
paths = paths.concat(options.browserifyPaths);
}


_log('executing browserify via API');
let bproc = browserify([input], {
debug: options.debug,
commondir: false,
paths: paths,
builtins: false,
insertGlobalVars: insertGlobalVars,
detectGlobals: true,
browserField: false
});

if (options.browserifyExcludes && Array.isArray(options.browserifyExcludes)) {
for (let i = 0; i < options.browserifyExcludes.length; i++) {
let lib = options.browserifyExcludes[i];
_log('Excluding \'%s\' from browserify bundle', lib);
bproc.exclude(lib);
}
}

// copy the excludes code for requires for now.
if (options.browserifyRequires && Array.isArray(options.browserifyRequires)) {
for (let i = 0; i < options.browserifyRequires.length; i++) {
let lib = options.browserifyRequires[i];
let name = lib.file || lib; // for object format.
// if `lib` is object then fetch all params without `file`
// otherwise returns empty object
let opts = (lib instanceof Object) && Object.keys(lib)
.filter((key) => key !== 'file')
.reduce((acc, key) => { return acc[key] = lib[key], acc }, {})
|| {};

_log('Force including \'%s\' in browserify bundle', name);
bproc.require(lib, opts);
}
}

if(options.debug) {
bproc.require(require.resolve('source-map-support'))
}

let bprocbun = bproc.bundle() // bundle
.pipe(ws) // pipe to file

// error on require errors, still can't contionue. ffs browserify
bprocbun.on('error', function(err) {
_log('error', '[browserify] '+err);
});

ws.on('error', function(err) {
console.log(err);
_log('error', 'Failed to save stdout to disk');
process.exit(1);
})

ws.on('close', function() {
var source = fs.readFileSync(bundlePath, 'utf8');
source = source.replace(/[^\x00-\x7F]/g, "");

// write the source modified to nexe.js
fs.writeFile(bundlePath, source, 'utf8', function(err) {
if (err) {
_log('error', 'failed to save source');
process.exit(1);
}

complete();
});
});
}

module.exports = bundle;
Loading

0 comments on commit f7bba1f

Please sign in to comment.