Browse Source

a few simple examples

update-to-0.20.1
Rich-Harris 9 years ago
parent
commit
9a1ec340cf
  1. 3
      src/examples/00/modules/foo.js
  2. 4
      src/examples/00/modules/main.js
  3. 12
      src/examples/00/modules/maths.js
  4. 2
      src/examples/00/name
  5. 1
      src/examples/01/modules/answer.js
  6. 9
      src/examples/01/modules/lib.js
  7. 8
      src/examples/01/modules/main.js
  8. 2
      src/examples/01/name
  9. 16
      src/examples/02/modules/main.js
  10. 1
      src/examples/02/modules/qux.js
  11. 10
      src/examples/02/modules/tags.js
  12. 12
      src/examples/02/modules/unbind.js
  13. 2
      src/examples/02/name
  14. 5
      src/examples/03/modules/main.js
  15. 1
      src/examples/03/name

3
src/examples/00/modules/foo.js

@ -1,3 +0,0 @@
export function foo () {
return 42;
}

4
src/examples/00/modules/main.js

@ -1,2 +1,2 @@
import { foo } from './foo';
console.log( foo() );
import { cube } from './maths.js';
console.log( cube( 5 ) ); // 125

12
src/examples/00/modules/maths.js

@ -0,0 +1,12 @@
// This function isn't used anywhere, so
// Rollup excludes it from the bundle...
export function square ( x ) {
return x * x;
}
// This function gets included
export function cube ( x ) {
// rewrite this as `square( x ) * x`
// and see what happens!
return x * x * x;
}

2
src/examples/00/name

@ -1 +1 @@
first example
Tree-shaking

1
src/examples/01/modules/answer.js

@ -0,0 +1 @@
export default 42;

9
src/examples/01/modules/lib.js

@ -1,9 +0,0 @@
export const sqrt = Math.sqrt;
export function square( x ) {
return x * x;
}
export function diag ( x, y ) {
return sqrt( square( x ) + square( y ) );
}

8
src/examples/01/modules/main.js

@ -1,3 +1,7 @@
import { diag } from './lib';
import answer from './answer.js';
console.log( diag( 3, 4 ) ); // 5
// Default exports from the 'entry module' are
// exported from the bundle
export default function () {
console.log( 'the answer is ' + answer );
}

2
src/examples/01/name

@ -1 +1 @@
second example
Default exports

16
src/examples/02/modules/main.js

@ -1,4 +1,14 @@
import { getStringTag } from './unbind';
import { arrayTag } from './tags';
// There are many ways to export bindings
// from an ES2015 module
export var foo = 1;
console.log( getStringTag( [] ) === arrayTag ); // true
export function bar () {
return foo; // try changing this to `foo++`
}
function baz () {
return bar();
}
export { baz };
export * from './qux';

1
src/examples/02/modules/qux.js

@ -0,0 +1 @@
export var qux = 'QUX';

10
src/examples/02/modules/tags.js

@ -1,10 +0,0 @@
// A number of StringTags.
export const
arrayTag = '[object Array]',
dateTag = '[object Date]',
functionTag = '[object Function]',
mathTag = '[object Math]',
nullTag = '[object Null]',
regexTag = '[object RegExp]',
stringTag = '[object String]',
undefinedTag = '[object Undefined]';

12
src/examples/02/modules/unbind.js

@ -1,12 +0,0 @@
// Create functions from prototype methods, to avoid using call.
const unbind = Function.prototype.bind.bind(Function.prototype.call);
export const slice = unbind(Array.prototype.slice);
// Check if the first argument has a given property name.
export const hasOwn = unbind(Object.prototype.hasOwnProperty);
// Get the string tag of an object.
export const getStringTag = unbind(Object.prototype.toString);
export default unbind;

2
src/examples/02/name

@ -1 +1 @@
deadcode-elimination
Named exports

5
src/examples/03/modules/main.js

@ -0,0 +1,5 @@
// You can import external modules into your bundle –
// it doesn't matter if they're ES2015 or legacy
import $ from 'jquery';
$( 'body' ).html( '<h1>Hello world!</h1>' );

1
src/examples/03/name

@ -0,0 +1 @@
External imports
Loading…
Cancel
Save