01/2017 / Milan "perún" Herda / @moriquend / prezentacie.perunhq.org
Som vývojár populárnej knižnice
Som vývojár populárnej knižnice
Akým spôsobom budem knižnicu poskytovať?
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD
define(['libName'], factory);
} else if (typeof exports === 'object') {
// Node, CommonJS-like
module.exports = factory(require('libName'));
} else {
// Browser globals (root is window)
root.returnExports = factory(root.libName);
}
}(this, function ($) {
// methods
function myFunc(){};
// exposed public method
return myFunc;
}));
Príkazom export
sa určuje časť modulu, ktorá bude dostupná
pre vonkajší svet
Príkaz import
sprístupňuje exportované časti modulu.
<script type="module" src="..."></script>
// module.js
const foo = function () { /*... */};
export default foo;
// main.js
import bar from './module';
// module.js
export default function () { /*... */} // pozor, bez bodkočiarky!
// main.js
import bar from './module';
// module.js
export const PI = 3.14159;
export const square = function (a) {
return a * a;
};
// main.js
import * as myMath from './module'; // myMath.square, myMath.PI
import { square } from './module';
import { square, PI } from './module';
import { square as mySquare, PI } from './module';
Default import musí byť vždy prvý!
// module.js
export const PI = 3.14159;
export default function () { /* ... */ }
export const square = function (a) {
return a * a;
};
// main.js
import def, { square, PI } from './module';
import def, * as myMath from './module';
import from './module';
Napríklad babel-polyfill
Node.js zatiaľ nepodporuje import/export z ES2015.
Viac info o problémoch a krátke zhrnutieTranspilácia cez babel
mkdir -p assets/src
mkdir -p assets/dist
# vytvorenie súboru package.json
npm init
# nainštalovanie gulp a babel ako konzolových nástrojov/príkazov
sudo npm install -g gulp babel-cli
# nainšťalovanie transpilátora ES2015 do ES5
npm install --save-dev babel babel-core babel-preset-es2015
# nainštalovanie task runnera a pluginov
npm install --save-dev gulp gulp-babel run-sequence del
{
"presets": ["es2015"]
}
import gulp from 'gulp';
import babel from 'gulp-babel';
import del from 'del';
import runSequence from 'run-sequence';
const paths = {
jsSrc: 'assets/src/**/*.js',
jsDest: 'assets/dist',
};
gulp.task('clean', () => {
return del(paths.jsDest + '/**/*');
});
gulp.task('babel', () => {
return gulp.src([paths.jsSrc])
.pipe(babel({
presets: ['es2015'],
}))
.pipe(gulp.dest(paths.jsDest));
});
gulp.task('all', (done) => {
runSequence('clean', 'babel', done);
});
gulp.task('default', ['all']);
import gulp from 'gulp';
import babel from 'gulp-babel';
import del from 'del';
import runSequence from 'run-sequence';
const paths = {
jsSrc: 'assets/src/**/*.js',
jsDest: 'assets/dist',
};
gulp.task('clean', () => {
return del(paths.jsDest + '/**/*');
});
gulp.task('babel', () => {
return gulp.src([paths.jsSrc])
.pipe(babel({
presets: ['es2015'],
}))
.pipe(gulp.dest(paths.jsDest));
});
gulp.task('all', (done) => {
runSequence('clean', 'babel', done);
});
gulp.task('default', ['all']);
export const BACKGROUND = '#fff';
export const TEXT = '#000';
export const TITLE = '#000080';
export const WARNING = '#800000';
export const SUCCESS = '#008000';
const INFO = 'slateblue';
export default INFO;
import * as color from './module/colors';
console.log(color.BACKGROUND);
import { TITLE, WARNING, SUCCESS as COLOR_SUCCESS } from './module/colors';
console.log(TITLE);
console.log(COLOR_SUCCESS);
import defaultColor, { TEXT } from './module/colors';
console.log(TEXT);
console.log(defaultColor);
# preklad
gulp
# spustenie
node assets/dist/all.js
node assets/dist/some.js
node assets/dist/combination.js
Január 2017:
Vieme použiť babel spolu s webpack 1
Alebo webpack 2 bez babelu
# webpack ako command line príkaz
sudo npm install -g webpack
# webpack do nášho projektu
npm install --save webpack
# plugin pre webpack, aby robil transpiláciu on
npm install --save babel-loader
# gulp-util pre zobrazenie výstupu z webpacku v gulpe
npm install --save gulp-util
export default {
entry: {
all: './assets/src/all.js',
},
output: {
path: 'assets/dist',
library: '[name]',
libraryTarget: 'umd',
filename: '[name].js'
},
module: {
loaders: [
{
test: /\.js$/,
loader: 'babel-loader',
query: {
presets: ['es2015']
}
}
]
},
resolve: {
extensions: ['', '.js', '.json']
}
};
Vyhodíme import babel
a task babel
import webpack from 'webpack';
import webpackConfig from './webpack.config.babel';
const paths = {
jsDest: 'assets/dist',
finalDest: 'www/assets',
};
Upravený clean task
gulp.task('clean:dist', () => {
return del(paths.jsDest + '/**/*');
});
gulp.task('clean:final', () => {
return del(paths.finalDest + '/**/*');
});
gulp.task('clean', (done) => {
runSequence('clean:final', 'clean:dist', done);
});
webpack task
gulp.task('webpack', (done) => {
let config = Object.create(webpackConfig);
webpack(config, function (err, stats) {
if (err) {
throw new gutil.PluginError('webpack', err);
}
gutil.log('[webpack]', stats.toString({
colors: true,
progress: true
}));
done(); // mimoriadne dôležité, aby gulp vedel, že task skončil
});
});
Upravené tasky copy a all
gulp.task('copy', () => {
return gulp.src([paths.jsDest + '/**/*'])
.pipe(gulp.dest(paths.finalDest));
});
gulp.task('all', (done) => {
runSequence('clean', 'webpack', 'copy', done);
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>es2015 moduly cez webpack babel</title>
</head>
<body>
</body>
<script src="assets/all.js"></script>
</html>
# spustit preklad
gulp
# spustit web server
cd www; php -S localhost:8000
Ostáva už len otvoriť prehliadač a nasmerovať ho na localhost:8000
webpack je možné spúšťať aj bez gulpu
Príklad sme videli na workshope o CommonJS moduloch a tak vieme, že stačí upraviť output.path
a spustiť príkaz webpack
Zoberte si unitFactory a extendByRange z prvého workshopu a urobte z nich ES2015 moduly pre web