fix: native module fuse plubin

This commit is contained in:
calebboyd
2017-09-11 01:20:37 -05:00
parent bc2a637cc4
commit 79bcb753b4
10 changed files with 39 additions and 2291 deletions
+7 -8
View File
@@ -20,9 +20,8 @@ export function embedDotNode(
file: { contents: string; absPath: string }
) {
const contents = fs.readFileSync(file.absPath)
const module = Object.keys(options).find(x =>
Boolean(~file.absPath.indexOf(path.join('node_modules', x)))
)!
const modulePathParts = file.absPath.split(path.sep).reverse()
const module = modulePathParts[modulePathParts.findIndex(x => x === 'node_modules') - 1]
const bindingName = path.basename(file.absPath)
const settings = options[module]
const moduleDir = hashName(contents)
@@ -31,7 +30,7 @@ export function embedDotNode(
'base64'
)}';function mkdirp(r,t){t=t||null,r=path.resolve(r);try{fs.mkdirSync(r),t=t||r}catch(c){if("ENOENT"===c.code)t=mkdirp(path.dirname(r),t),mkdirp(r,t);else{var i;try{i=fs.statSync(r)}catch(r){throw c}if(!i.isDirectory())throw c}}return t};`
if (settings === true) {
if (!settings || settings === true) {
file.contents += `
mkdirp('${moduleDir}');
var bindingPath = path.join(process.cwd(), '${moduleDir}', '${bindingName}')
@@ -105,13 +104,13 @@ export class BindingsRewrite {
public nativeModulePaths: string[] = []
public rewrite = false
isRequireBindings(node: any) {
return node.callee.name === 'require' && node.arguments[0].value === 'bindings'
isRequire(node: any, moduleName: string) {
return node.callee.name === 'require' && node.arguments[0].value === moduleName
}
onNode(absolutePath: string, node: any, parent: any) {
if (node.type === 'CallExpression') {
if (this.isRequireBindings(node) && parent.type === 'VariableDeclarator') {
if (this.isRequire(node, 'bindings') && parent.type === 'VariableDeclarator') {
/**
* const loadBindings = require('bindings');
* -> const loadBindings = String('');
@@ -123,7 +122,7 @@ export class BindingsRewrite {
return
}
if (this.isRequireBindings(node) && parent.type === 'CallExpression') {
if (this.isRequire(node, 'bindings') && parent.type === 'CallExpression') {
/**
*const bindings = require('bindings')('native-module')....
* -> const bindings = require('./path/to/native/module.node').....
+5 -6
View File
@@ -8,6 +8,7 @@ export interface FuseBoxFile {
dependencies: string[]
requiresRegeneration: boolean
}
consume(): void
loadContents(): void
makeAnalysis(
parsingOptions?: any,
@@ -20,19 +21,16 @@ export interface FuseBoxFile {
): void
}
export default function(options: ExtractNodeModuleOptions) {
export default function(options: ExtractNodeModuleOptions = {}) {
return new NativeModulePlugin(options)
}
export class NativeModulePlugin {
public test: RegExp
public test = /node_modules.*(\.js|\.node)$|\.node$/
public limit2Project = false
private modules: (keyof ExtractNodeModuleOptions)[]
constructor(public options: ExtractNodeModuleOptions) {
this.options = options
this.test = new RegExp(`node_modules\/(${Object.keys(options).join('|')}).*\.js|\.node$`)
}
constructor(public options = {}) {}
init(context: any) {
context.allowExtension('.node')
@@ -47,6 +45,7 @@ export class NativeModulePlugin {
}
const bindingsRewrite = new BindingsRewrite()
file.makeAnalysis(null, {
plugins: [
{
+3 -1
View File
@@ -30,6 +30,7 @@ export interface NexeOptions {
enableNodeCli: boolean
bundle: boolean | string
patches: (string | NexePatch)[]
native: any
empty: boolean
sourceUrl?: string
python?: string
@@ -161,7 +162,8 @@ function extractName(options: NexeOptions) {
if (!name && typeof options.input === 'string') {
name = basename(options.input).replace(extname(options.input), '')
}
name = name || tryResolveMainFileName(options.cwd)
name = name === 'index' ? tryResolveMainFileName(options.cwd) : name
return name.replace(/\.exe$/, '')
}
+7 -10
View File
@@ -1,11 +1,12 @@
import { NexeCompiler } from '../compiler'
import { FuseBox, JSONPlugin, CSSPlugin, HTMLPlugin, QuantumPlugin } from 'fuse-box'
import { readFileAsync, writeFileAsync } from '../util'
//import NativeModulePlugin from './fuse-native-module-plugin'
import NativeModulePlugin from '../bundling/fuse-native-module-plugin'
import { NexeOptions } from '../options'
function createBundle(filename: string, options: { name: string; minify: any; cwd: string }) {
const plugins: any = [JSONPlugin(), CSSPlugin(), HTMLPlugin()]
if (options.minify) {
function createBundle(options: NexeOptions) {
const plugins: any = [JSONPlugin(), CSSPlugin(), HTMLPlugin(), NativeModulePlugin(options.native)]
if (options.compress) {
plugins.push(
QuantumPlugin({
target: 'server',
@@ -24,7 +25,7 @@ function createBundle(filename: string, options: { name: string; minify: any; cw
target: 'server',
plugins
})
fuse.bundle(options.name).instructions(`> ${filename}`)
fuse.bundle(options.name).instructions(`> ${options.input}`)
return fuse.run().then(x => {
let output = ''
x.bundles.forEach(y => (output = y.context.output.lastPrimaryOutput.content!.toString()))
@@ -48,11 +49,7 @@ export default async function bundle(compiler: NexeCompiler, next: any) {
producer = require(compiler.options.bundle).createBundle
}
compiler.input = await producer(compiler.options.input, {
cwd: compiler.options.cwd,
name: compiler.options.name,
minify: compiler.options.compress
})
compiler.input = await producer(compiler.options)
if ('string' === typeof compiler.options.debugBundle) {
await writeFileAsync(compiler.options.debugBundle, compiler.input)