Владимир Кузнецов (@mistakster), Graph
var shuffle = require("lodash/collection/shuffle");
var camelCase = require("lodash/string/camelCase");
module.exports = function (list) {
return shuffle(list).map(camelCase);
};
module.exports = {
entry: "./app.js",
output: {
path: __dirname + "/output",
filename: "app.bundle.js"
}
};
$ webpack --config ./configs/site.js
$ webpack --config ./configs/admin.js
Such automation
So webpack
entry: {
"site": "./site",
"admin": "./admin"
}
output: {
path: require("path").resolve("./public/_/"),
publicPath: "/assets/",
filename: "[name].js",
chunkFilename: "[id].chunk.js"
}
plugins: [
new webpack.optimize.CommonsChunkPlugin({
name: "commons",
filename: "[name].js"
})
]
<-- site.html -->
<script src="/assets/commons.js"></script>
<script src="/assets/site.js"></script>
require.ensure(["jquery", "lodash"],
function (require) {
var jQuery = require("jquery");
}
);
var waitForChunk = require('bundle!./page');
waitForChunk(function (page) {
page.init();
});
// moment.js
require("./locale/" + name);
Папка — ./locale
Регулярное выражение — /^.*$/
plugins: [
new webpack.ContextReplacementPlugin(
/moment[\/\\]locale$/,
/^(en-gb|ru)\.js$/
)
]
new webpack.DefinePlugin({
"DEBUG": process.env.NODE_ENV != "production",
"API_ENDPOINT": "'" + URL + "'",
"helloWorldFunc": function () {
return 'Hello, world!';
}
})
if (DEBUG) {
alert(API_ENDPOINT);
}
helloWorldFunc();
if (false) {
alert(('http://example.com'));
}
(function () {
return 'Hello, world!';
})();
$ npm i -g webpack-dev-server
$ webpack-dev-server
Можно встроить в существующий сервер на Node.js или пробросить доступ к нему через прокси-сервер.
var ExtractText = require("extract-text-webpack-plugin");
var extractStyles = new ExtractText("main.css");
var loader = extractStyles.extract("style", "css!less");
plugins: [ extractStyles ]
module: {
loaders: [
{ test: /\.less$/, loader: loader }
]
}
var MyTaskRunner = function () {};
MyTaskRunner.prototype.apply = function (compiler) {
// задачи
}
module.exports = MyTaskRunner;
compiler.plugin("run", function (params, callback) {
console.log("*** Run ***");
callback();
});
compiler.plugin("done", function () {
console.log("*** Done ***");
});
$ npm run clean
"scripts": {
"clean": "rm -rf ./public/_"
}
$ npm run build:site
$ npm run build:admin
"scripts": {
"build:site": "./node_modules/.bin/webpack --config site…
"build:admin": "./node_modules/.bin/webpack …
}
$ npm run myCoolJavaScript
"scripts": {
"myCoolJavaScript": "node my-cool-javascript.js"
}
@mistakster (English)
@mista_k (больше про жизнь)
Слайды презентации: mistakster.github.io/fronttalks-webpack