atom / electron-link Goto Github PK
View Code? Open in Web Editor NEWA module to bundle your electron app into a single file that can be used for V8 snapshots.
License: MIT License
A module to bundle your electron app into a single file that can be used for V8 snapshots.
License: MIT License
Expected behavior:
Actual behavior:
Reproduces how often:
Following is the error - any help in debugging this further?
Generating snapshot script at "\build\startup.js" (9)MemberExpression MemberExpression CallExpression ExpressionStatement Program [17:27:48] 'makesnapshot' errored after 467 ms [17:27:48] Error: \build\lib\main.js Cannot replace with lazy function because the supplied node does not belong to an assignment expression or a variable declaration! at FileRequireTransform.replaceAssignmentOrDeclarationWithLazyFunction (\node_modules\electron-link\lib\file-require-transform.js:181:11) at Context.recast.types.visit.visitIdentifier (\node_modules\electron-link\lib\file-require-transform.js:72:18) at Context.invokeVisitorMethod (\node_modules\recast\node_modules\ast-types\lib\path-visitor.js:344:49) at Visitor.PVp.visitWithoutReset (\node_modules\recast\node_modules\ast-types\lib\path-visitor.js:196:32) at visitChildren (\node_modules\recast\node_modules\ast-types\lib\path-visitor.js:246:25) at Visitor.PVp.visitWithoutReset (\node_modules\recast\node_modules\ast-types\lib\path-visitor.js:204:20) at visitChildren (\node_modules\recast\node_modules\ast-types\lib\path-visitor.js:246:25) at Visitor.PVp.visitWithoutReset (\node_modules\recast\node_modules\ast-types\lib\path-visitor.js:204:20) at visitChildren (\node_modules\recast\node_modules\ast-types\lib\path-visitor.js:246:25) at Visitor.PVp.visitWithoutReset (\node_modules\recast\node_modules\ast-types\lib\path-visitor.js:204:20) at visitChildren (\node_modules\recast\node_modules\ast-types\lib\path-visitor.js:246:25) at Visitor.PVp.visitWithoutReset (\node_modules\recast\node_modules\ast-types\lib\path-visitor.js:204:20) at NodePath.each (\node_modules\recast\node_modules\ast-types\lib\path.js:101:26) at visitChildren (\node_modules\recast\node_modules\ast-types\lib\path-visitor.js:219:18) at Visitor.PVp.visitWithoutReset (\node_modules\recast\node_modules\ast-types\lib\path-visitor.js:204:20) at visitChildren (\node_modules\recast\node_modules\ast-types\lib\path-visitor.js:246:25) at Visitor.PVp.visitWithoutReset (\node_modules\recast\node_modules\ast-types\lib\path-visitor.js:204:20) at visitChildren (\node_modules\recast\node_modules\ast-types\lib\path-visitor.js:246:25) at Visitor.PVp.visitWithoutReset (\node_modules\recast\node_modules\ast-types\lib\path-visitor.js:204:20) at Visitor.PVp.visit (\node_modules\recast\node_modules\ast-types\lib\path-visitor.js:133:29) at Object.visit (\node_modules\recast\node_modules\ast-types\lib\path-visitor.js:101:55) at FileRequireTransform.replaceDeferredRequiresWithLazyFunctions (\node_modules\electron-link\lib\file-require-transform.js:68:20)
I am trying to update the babel plugin that is used in Atom internally, but Electron-link bundler gives me this error during bundling:
2020-06-29T02:36:43.2059234Z Generating snapshot script at "/__w/1/s/out/startup.js" (3770)Unable to transform source code for module /__w/1/s/out/app/node_modules/@babel/core/lib/config/files/import.js.
2020-06-29T02:36:43.2059872Z SyntaxError: Unexpected token (9:9)
2020-06-29T02:36:43.2060285Z at Parser.pp$4.raise (/__w/1/s/script/node_modules/acorn/dist/acorn.js:2825:15)
2020-06-29T02:36:43.2060817Z at Parser.pp.unexpected (/__w/1/s/script/node_modules/acorn/dist/acorn.js:689:10)
2020-06-29T02:36:43.2061403Z at Parser.pp$3.parseExprAtom (/__w/1/s/script/node_modules/acorn/dist/acorn.js:2266:21)
2020-06-29T02:36:43.2061975Z at Parser.pp$3.parseExprSubscripts (/__w/1/s/script/node_modules/acorn/dist/acorn.js:2089:21)
2020-06-29T02:36:43.2062562Z at Parser.pp$3.parseMaybeUnary (/__w/1/s/script/node_modules/acorn/dist/acorn.js:2066:19)
2020-06-29T02:36:43.2063135Z at Parser.pp$3.parseExprOps (/__w/1/s/script/node_modules/acorn/dist/acorn.js:2010:21)
2020-06-29T02:36:43.2063813Z at Parser.pp$3.parseMaybeConditional (/__w/1/s/script/node_modules/acorn/dist/acorn.js:1993:21)
2020-06-29T02:36:43.2070126Z at Parser.pp$3.parseMaybeAssign (/__w/1/s/script/node_modules/acorn/dist/acorn.js:1968:21)
2020-06-29T02:36:43.2071153Z at Parser.pp$3.parseExpression (/__w/1/s/script/node_modules/acorn/dist/acorn.js:1933:21)
2020-06-29T02:36:43.2073238Z at Parser.pp$1.parseReturnStatement (/__w/1/s/script/node_modules/acorn/dist/acorn.js:1005:33)
2020-06-29T02:36:43.2074002Z at Parser.pp$1.parseStatement (/__w/1/s/script/node_modules/acorn/dist/acorn.js:835:37)
2020-06-29T02:36:43.2075304Z at Parser.pp$1.parseBlock (/__w/1/s/script/node_modules/acorn/dist/acorn.js:1161:23)
2020-06-29T02:36:43.2076941Z at Parser.pp$3.parseFunctionBody (/__w/1/s/script/node_modules/acorn/dist/acorn.js:2671:24)
2020-06-29T02:36:43.2078497Z at Parser.pp$1.parseFunction (/__w/1/s/script/node_modules/acorn/dist/acorn.js:1283:10)
2020-06-29T02:36:43.2080618Z at Parser.pp$1.parseFunctionStatement (/__w/1/s/script/node_modules/acorn/dist/acorn.js:983:17)
2020-06-29T02:36:43.2081360Z at Parser.pp$1.parseStatement (/__w/1/s/script/node_modules/acorn/dist/acorn.js:830:19)
2020-06-29T02:36:43.2090492Z at Parser.pp$1.parseTopLevel (/__w/1/s/script/node_modules/acorn/dist/acorn.js:746:23)
2020-06-29T02:36:43.2091224Z at Parser.parse (/__w/1/s/script/node_modules/acorn/dist/acorn.js:553:17)
2020-06-29T02:36:43.2093874Z at Function.parse (/__w/1/s/script/node_modules/acorn/dist/acorn.js:576:37)
2020-06-29T02:36:43.2094399Z at Object.parse (/__w/1/s/script/node_modules/acorn/dist/acorn.js:4949:19)
2020-06-29T02:36:43.2095688Z at Object.parse (/__w/1/s/script/node_modules/electron-link/node_modules/recast/parsers/acorn.js:14:32)
2020-06-29T02:36:43.2096565Z at Object.parse (/__w/1/s/script/node_modules/electron-link/lib/file-require-transform.js:26:50)
2020-06-29T02:36:43.2097408Z at Object.parse (/__w/1/s/script/node_modules/electron-link/node_modules/recast/lib/parser.js:31:30)
2020-06-29T02:36:43.2098265Z at FileRequireTransform.apply (/__w/1/s/script/node_modules/electron-link/lib/file-require-transform.js:23:23)
2020-06-29T02:36:43.2099091Z at /__w/1/s/script/node_modules/electron-link/lib/generate-snapshot-script.js:55:56
2020-06-29T02:36:43.2099563Z at Generator.next (<anonymous>:null:null)
2020-06-29T02:36:43.2100279Z at step (/__w/1/s/script/node_modules/electron-link/lib/generate-snapshot-script.js:3:191)
2020-06-29T02:36:43.2101049Z at /__w/1/s/script/node_modules/electron-link/lib/generate-snapshot-script.js:3:361
Environment
System:
OS: Windows 10 10.0.19041
Binaries:
apm 2.5.0
npm 6.14.5
node 10.20.1 x64
atom unknown
python 2.7.6
git 1.9.1
npmPackages:
@babel/cli: 7.10.3 => 7.10.3
@babel/core: 7.10.3 => 7.10.3
@babel/plugin-proposal-class-properties: ^7.10.1 => 7.10.1
@babel/plugin-proposal-decorators: ^7.10.3 => 7.10.3
@babel/plugin-proposal-do-expressions: ^7.10.1 => 7.10.1
@babel/plugin-proposal-export-default-from: ^7.10.1 => 7.10.1
@babel/plugin-proposal-export-namespace-from: ^7.10.1 => 7.10.1
@babel/plugin-proposal-function-bind: ^7.10.1 => 7.10.1
@babel/plugin-proposal-function-sent: ^7.10.1 => 7.10.1
@babel/plugin-proposal-json-strings: ^7.10.1 => 7.10.1
@babel/plugin-proposal-logical-assignment-operators: ^7.10.3 => 7.10.3
@babel/plugin-proposal-nullish-coalescing-operator: ^7.10.1 => 7.10.1
@babel/plugin-proposal-numeric-separator: ^7.10.1 => 7.10.1
@babel/plugin-proposal-optional-chaining: ^7.10.3 => 7.10.3
@babel/plugin-proposal-pipeline-operator: ^7.10.1 => 7.10.1
@babel/plugin-proposal-throw-expressions: ^7.10.1 => 7.10.1
@babel/plugin-syntax-dynamic-import: ^7.8.3 => 7.8.3
@babel/plugin-syntax-import-meta: ^7.10.1 => 7.10.1
@babel/plugin-transform-async-to-generator: 7.10.1 => 7.10.1
@babel/preset-env: 7.10.3 => 7.10.3
@babel/preset-flow: ^7.10.1 => 7.10.1
@babel/preset-react: ^7.10.1 => 7.10.1
Possible Solution
Is there a version that babel recommends using here instead of 7.10.3? Can I transform babel itself to make it compatible with Electron 5?
Additional context
I'm the author and maintainer of V8's custom start-up snapshot. I just came across this project recently. I think you would find this design document very interesting.
A few highlights:
FunctionTemplates
and ObjectTemplates
and their instances can now be serialized, provided that the native function address is provided to v8::SnapshotCreator
.FunctionTemplates
and ObjectTemplates
objects can be extracted from the snapshot, so there is no need to set them up from scratch.Please let me know if you are interested and have questions.
The current version of leveldown
(v3.0.0) builds on Node.js perfectly fine, however the old ^1.6.0
dependency specified here breaks the build of Atom.
Versions:
Attempting to build atom/atom@59cb1dd.
Node.js: v10.0.0
npm: v6.0.0
OS: Windows 10 x64
Checking the npm debug log shows this message:
32386 silly install [email protected]
32387 info lifecycle [email protected]~install: [email protected]
32388 verbose lifecycle [email protected]~install: unsafe-perm in lifecycle true
32389 verbose lifecycle [email protected]~install: PATH: C:\Users\abneyl\AppData\Roaming\npm\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin;C:\temp\atom\script\node_modules\leveldown\node_modules\.bin;C:\temp\atom\script\node_modules\.bin;C:\Python27;C:\Program Files\ConEmu\ConEmu\Scripts;C:\Program Files\ConEmu;C:\Program Files\ConEmu\ConEmu;C:\Program Files\Docker\Docker\Resources\bin;C:\Program Files\Docker\Docker\resources\bin;C:\ProgramData\Oracle\Java\javapath;C:\Program Files\Python36\Scripts\;C:\Program Files\Python36\;C:\Program Files\Haskell\bin;C:\Program Files\Haskell Platform\7.10.2-a\lib\extralibs\bin;C:\Program Files\Haskell Platform\7.10.2-a\bin;C:\Windows\System32;C:\Windows;C:\Windows\System32\wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\WebEx\Productivity Tools;C:\Program Files\PuTTY\;C:\Program Files\Haskell Platform\7.10.2-a\mingw\bin;C:\Strawberry\c\bin;C:\Strawberry\perl\site\bin;C:\Strawberry\perl\bin;C:\Program Files (x86)\GnuPG\bin;C:\Ruby24-x64\bin;C:\php7;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit\;C:\Program Files\dotnet\;C:\Program Files\Microsoft\Web Platform Installer\;C:\Program Files\TortoiseSVN\bin;C:\Program Files\nodejs\;C:\Program Files\Git\cmd;C:\Program Files\LLVM\bin;C:\Users\abneyl\AppData\Local\Microsoft\WindowsApps;C:\Users\abneyl\AppData\Local\atom\bin;C:\Users\abneyl\AppData\Local\ComposerSetup\bin;C:\Users\abneyl\AppData\Roaming\Composer\vendor\bin;C:\Users\abneyl\bin;C:\Users\abneyl\AppData\Local\Microsoft\WindowsApps;C:\Users\abneyl\AppData\Roaming\npm;C:\Users\abneyl\AppData\Local\GitHubDesktop\bin
32390 verbose lifecycle [email protected]~install: CWD: C:\temp\atom\script\node_modules\leveldown
32391 silly lifecycle [email protected]~install: Args: [ '/d /s /c', 'prebuild-install || node-gyp rebuild' ]
32392 silly lifecycle [email protected]~install: Returned: code: 1 signal: null
32393 info lifecycle [email protected]~install: Failed to exec install script
And attempting to install it individually on a blank project leads to this message:
c:\temp\foo\node_modules\nan\nan_maybe_43_inl.h(112): error C2039: 'ForceSet': is not a member of 'v8::Object' (compiling source file ..\src\batch.cc) [C:\temp\foo\node_modules\leveldown\build\leveldown.vcxproj]
c:\users\abneyl\.node-gyp\10.0.0\include\node\v8.h(3111): note: see declaration of 'v8::Object' (compiling source file ..\src\batch.cc)
gyp ERR! build error
gyp ERR! stack Error: `C:\Program Files (x86)\MSBuild\14.0\bin\msbuild.exe` failed with exit code: 1
gyp ERR! stack at ChildProcess.onExit (C:\Users\abneyl\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\build.js:258:23)
gyp ERR! stack at ChildProcess.emit (events.js:182:13)
gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:225:12)
gyp ERR! System Windows_NT 10.0.16299
gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\abneyl\\AppData\\Roaming\\npm\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
gyp ERR! cwd C:\temp\atom\script\node_modules\leveldown
gyp ERR! node -v v10.0.0
gyp ERR! node-gyp -v v3.6.2
gyp ERR! not ok
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] install: `prebuild-install || node-gyp rebuild`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\abneyl\AppData\Roaming\npm-cache\_logs\2018-04-26T19_05_31_529Z-debug.log
child_process.js:617
throw err;
^
Error: Command failed: npm.cmd --loglevel=error install
gyp ERR! build error
gyp ERR! stack Error: `C:\Program Files (x86)\MSBuild\14.0\bin\msbuild.exe` failed with exit code: 1
gyp ERR! stack at ChildProcess.onExit (C:\Users\abneyl\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\build.js:258:23)
gyp ERR! stack at ChildProcess.emit (events.js:182:13)
gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:225:12)
gyp ERR! System Windows_NT 10.0.16299
gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\abneyl\\AppData\\Roaming\\npm\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
gyp ERR! cwd C:\temp\atom\script\node_modules\leveldown
gyp ERR! node -v v10.0.0
gyp ERR! node-gyp -v v3.6.2
gyp ERR! not ok
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] install: `prebuild-install || node-gyp rebuild`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\abneyl\AppData\Roaming\npm-cache\_logs\2018-04-26T19_05_31_529Z-debug.log
at checkExecSyncError (child_process.js:596:11)
at Object.execFileSync (child_process.js:614:13)
at module.exports (C:\temp\atom\script\lib\install-script-dependencies.js:9:16)
at Object.<anonymous> (C:\temp\atom\script\bootstrap:28:1)
at Module._compile (internal/modules/cjs/loader.js:678:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:689:10)
at Module.load (internal/modules/cjs/loader.js:589:32)
at tryModuleLoad (internal/modules/cjs/loader.js:528:12)
at Function.Module._load (internal/modules/cjs/loader.js:520:3)
at Module.require (internal/modules/cjs/loader.js:626:17)
indent-string
is required here but it's not listed in the module's dependencies
I noticed that __dirname
returns relative paths instead of absolute when executed in a module that is part of the snapshot.
What is the reason of this happening? Is it possible this to be fixed or is it a limitation of the implementation?
I looked very closely at how Atom implements electron-link, but I for the world cannot get it to work... It also seems like almost none is actually using this, which is a shame, because it's a pretty cool feature. Hence I'm going to document what I have done, so others can learn from it and save themselves a day of work. Anyway, some help would be very much appreciated to get it running. I feel like I'm close, but I'm just not seeing what I'm missing...
I made a snapshot.js
which contains the following:
if(false) {
require("./native");
require("./main");
}
The two files from above are the only two I need a snapshot from. I guarded them with an if(false)
so I have complete control when to load them.
I use the following compile.js
, roughly based on Atom, but very simplied:
const electronLink = require("electron-link");
const fs = require("fs");
const vm = require("vm");
const path = require("path");
const childProcess = require("child_process");
let processedFiles = 0;
const snapshotScriptPath = "./cache/snapshot_generated.js";
electronLink({
baseDirPath: "./app",
mainPath: "./app/snapshot.js",
cachePath: "./cache",
shouldExcludeModule: (modulePath) => {
console.log(modulePath);
return !modulePath.endsWith("native.js") && !modulePath.endsWith("main.js");
}
}).then(function (snapshotScript) {
fs.writeFileSync(snapshotScriptPath, snapshotScript);
vm.runInNewContext(snapshotScript, undefined, { filename: snapshotScriptPath, displayErrors: true });
var mksnapshotPath = path.join('.', 'node_modules', 'electron-mksnapshot', 'bin', 'mksnapshot');
childProcess.execFileSync(mksnapshotPath, [snapshotScriptPath, "--startup_blob", "./app/snapshot_blob.bin"]);
}).catch((error) => {
console.log(error);
});
The two files from the snapshot.js
are of the following format:
module.exports = function() {
//Do stuff
}
My startup.js
, which is set in package.json
, does the following:
var main;
if (typeof snapshotResult !== 'undefined') {
snapshotResult.setGlobals(global, process, global, {}, console, require);
main = snapshotResult.customRequire("./main");
}
else {
main = require("./main");
}
main();
main()
effectively runs the module.exports
from before, creates a BrowserWindow with a preload-script, which in turn requires native.js
in a similar fashion.
Everything works fine when trying out using electron app
from the CLI. But after building, and replacing the default snapshot_blob.bin
, it simply does not work, at all.
Can someone please point out what I'm missing?
PS. I also notice, when I open the snapshot_blob.bin
with notepad, the whole generated snapshot.js
is fully readable, kind of defeating the point of compiling it (other than speed). What's up with that?
I'm interested in using this, but the documentation seems to be hopelessly out-of-date with version 0.2.2. Even the tests don't seem like they could work (snapshotResult
is frequently referenced but never declared, for example).
Most of the time I've been able to fumble through by reading the source and making educated guesses, but in particular I'm being tripped up on the proper way to call setGlobals
, especially the idea of defining window
at compile time when that object doesn't exist until runtime.
What is the best way to bring this into my project right now? Should I simply copy the use of 0.0.21
in Atom's master branch?
I stumbled across this feature because it seems to be used by atom. For some reason it's not documented here. I would submit a PR to document it if I know what it does but I don't
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.