-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "--target=node" and "--target=electron" option to produce Node/el…
…ectron friendly bundles (#652)
- Loading branch information
1 parent
8c3c4be
commit 420ed63
Showing
15 changed files
with
312 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,130 @@ | ||
const assert = require('assert'); | ||
const fs = require('fs'); | ||
const {bundle, run, assertBundleTree} = require('./utils'); | ||
|
||
describe('fs', function() { | ||
it('should inline a file as a string', async function() { | ||
let b = await bundle(__dirname + '/integration/fs/index.js'); | ||
let output = run(b); | ||
assert.equal(output, 'hello'); | ||
}); | ||
describe('--target=browser', function() { | ||
it('should inline a file as a string', async function() { | ||
let b = await bundle(__dirname + '/integration/fs/index.js'); | ||
let output = run(b); | ||
assert.equal(output, 'hello'); | ||
}); | ||
|
||
it('should inline a file as a buffer', async function() { | ||
let b = await bundle(__dirname + '/integration/fs-buffer/index.js'); | ||
let output = run(b); | ||
assert.equal(output.constructor.name, 'Buffer'); | ||
assert.equal(output.length, 5); | ||
}); | ||
it('should inline a file as a buffer', async function() { | ||
let b = await bundle(__dirname + '/integration/fs-buffer/index.js'); | ||
let output = run(b); | ||
assert.equal(output.constructor.name, 'Buffer'); | ||
assert.equal(output.length, 5); | ||
}); | ||
|
||
it('should inline a file with fs require alias', async function() { | ||
let b = await bundle(__dirname + '/integration/fs-alias/index.js'); | ||
let output = run(b); | ||
assert.equal(output, 'hello'); | ||
}); | ||
it('should inline a file with fs require alias', async function() { | ||
let b = await bundle(__dirname + '/integration/fs-alias/index.js'); | ||
let output = run(b); | ||
assert.equal(output, 'hello'); | ||
}); | ||
|
||
it('should inline a file with fs require inline', async function() { | ||
let b = await bundle(__dirname + '/integration/fs-inline/index.js'); | ||
let output = run(b); | ||
assert.equal(output, 'hello'); | ||
}); | ||
it('should inline a file with fs require inline', async function() { | ||
let b = await bundle(__dirname + '/integration/fs-inline/index.js'); | ||
let output = run(b); | ||
assert.equal(output, 'hello'); | ||
}); | ||
|
||
it('should inline a file with fs require assignment', async function() { | ||
let b = await bundle(__dirname + '/integration/fs-assign/index.js'); | ||
let output = run(b); | ||
assert.equal(output, 'hello'); | ||
}); | ||
it('should inline a file with fs require assignment', async function() { | ||
let b = await bundle(__dirname + '/integration/fs-assign/index.js'); | ||
let output = run(b); | ||
assert.equal(output, 'hello'); | ||
}); | ||
|
||
it('should inline a file with fs require assignment alias', async function() { | ||
let b = await bundle(__dirname + '/integration/fs-assign-alias/index.js'); | ||
let output = run(b); | ||
assert.equal(output, 'hello'); | ||
}); | ||
it('should inline a file with fs require assignment alias', async function() { | ||
let b = await bundle(__dirname + '/integration/fs-assign-alias/index.js'); | ||
let output = run(b); | ||
assert.equal(output, 'hello'); | ||
}); | ||
|
||
it('should inline a file with fs require destructure', async function() { | ||
let b = await bundle(__dirname + '/integration/fs-destructure/index.js'); | ||
let output = run(b); | ||
assert.equal(output, 'hello'); | ||
}); | ||
it('should inline a file with fs require destructure', async function() { | ||
let b = await bundle(__dirname + '/integration/fs-destructure/index.js'); | ||
let output = run(b); | ||
assert.equal(output, 'hello'); | ||
}); | ||
|
||
it('should inline a file with fs require destructure assignment', async function() { | ||
let b = await bundle( | ||
__dirname + '/integration/fs-destructure-assign/index.js' | ||
); | ||
let output = run(b); | ||
assert.equal(output, 'hello'); | ||
it('should inline a file with fs require destructure assignment', async function() { | ||
let b = await bundle( | ||
__dirname + '/integration/fs-destructure-assign/index.js' | ||
); | ||
let output = run(b); | ||
assert.equal(output, 'hello'); | ||
}); | ||
|
||
it('should not evaluate fs calls when package.browser.fs is false', async function() { | ||
let b = await bundle( | ||
__dirname + '/integration/resolve-entries/ignore-fs.js' | ||
); | ||
|
||
assertBundleTree(b, { | ||
name: 'ignore-fs.js', | ||
// empty.js is generated by require('fs'), it gets mocked with an empty module | ||
assets: ['empty.js', 'ignore-fs.js', 'index.js'], | ||
childBundles: [ | ||
{ | ||
type: 'map' | ||
} | ||
] | ||
}); | ||
|
||
let output = run(b); | ||
|
||
assert.equal(typeof output.test, 'function'); | ||
assert.equal(output.test(), 'test-pkg-ignore-fs-ok'); | ||
}); | ||
}); | ||
|
||
it('should not evaluate fs calls when package.browser.fs is false', async function() { | ||
let b = await bundle( | ||
__dirname + '/integration/resolve-entries/ignore-fs.js' | ||
); | ||
|
||
assertBundleTree(b, { | ||
name: 'ignore-fs.js', | ||
// empty.js is generated by require('fs'), it gets mocked with an empty module | ||
assets: ['empty.js', 'ignore-fs.js', 'index.js'], | ||
childBundles: [ | ||
{ | ||
type: 'map' | ||
} | ||
] | ||
describe('--target=node', function() { | ||
it('should leave an attempt to read a file unchanged', async function() { | ||
let b = await bundle(__dirname + '/integration/fs/index.js', { | ||
target: 'node' | ||
}); | ||
|
||
assertBundleTree(b, { | ||
name: 'index.js', | ||
assets: ['index.js'], | ||
childBundles: [ | ||
{ | ||
type: 'map' | ||
} | ||
] | ||
}); | ||
|
||
assert(fs.readFileSync(b.name).includes("require('fs')")); | ||
assert(fs.readFileSync(b.name).includes('readFileSync')); | ||
|
||
fs.writeFileSync(__dirname + '/dist/test.txt', 'hey'); | ||
let output = run(b); | ||
assert.equal(output, 'hey'); | ||
}); | ||
}); | ||
|
||
let output = run(b); | ||
describe('--target=electron', function() { | ||
it('should leave an attempt to read a file unchanged', async function() { | ||
let b = await bundle(__dirname + '/integration/fs/index.js', { | ||
target: 'electron' | ||
}); | ||
|
||
assert.equal(typeof output.test, 'function'); | ||
assert.equal(output.test(), 'test-pkg-ignore-fs-ok'); | ||
assertBundleTree(b, { | ||
name: 'index.js', | ||
assets: ['index.js'], | ||
childBundles: [ | ||
{ | ||
type: 'map' | ||
} | ||
] | ||
}); | ||
|
||
assert(fs.readFileSync(b.name).includes("require('fs')")); | ||
assert(fs.readFileSync(b.name).includes('readFileSync')); | ||
|
||
fs.writeFileSync(__dirname + '/dist/test.txt', 'hey'); | ||
let output = run(b); | ||
assert.equal(output, 'hey'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
exports.b = 2; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
var {a} = require('testmodule'); | ||
var {b} = require('./local'); | ||
|
||
module.exports = function () { | ||
return a + b; | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.