From ef85bee27b0c88120b3960c0aca2957bb6411e3f Mon Sep 17 00:00:00 2001 From: Rubens Pinheiro Date: Fri, 27 Jan 2017 16:30:55 +0100 Subject: [PATCH] Using ES6 on webpack.config and transpiling it with babel-core --- package.json | 4 ++-- webpack.config.js | 25 ++++++++++++------------- webpack.transpiler.js | 10 ++++++++++ webpack/loaders.js | 2 +- 4 files changed, 25 insertions(+), 16 deletions(-) create mode 100644 webpack.transpiler.js diff --git a/package.json b/package.json index 801000b..ee3cd21 100644 --- a/package.json +++ b/package.json @@ -10,9 +10,9 @@ ], "main": "dist/leaflet-ant-path.js", "scripts": { - "build": "NODE_ENV=production webpack --config webpack.config.js", + "build": "NODE_ENV=production webpack --config webpack.transpiler.js --colors --profile --progress", "test": "karma start --log-level debug --single-run", - "start": "NODE_ENV=development webpack-dev-server -d --inline --host 0.0.0.0", + "start": "NODE_ENV=development webpack-dev-server --config webpack.transpiler.js -d --cache --inline --host 0.0.0.0", "codacy": "cat coverage/lcov/lcov.info | codacy-coverage", "prepublish": "npm run build" }, diff --git a/webpack.config.js b/webpack.config.js index 953cebb..b498ba6 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,12 +1,11 @@ -var path = require("path"); -var webpack = require("webpack"); -var merge = require("merge"); -var loaders = require("./webpack/loaders"); -var development = require("./webpack/development"); -var production = require("./webpack/production"); +import path from "path"; +import merge from "merge"; +import loaders from "./webpack/loaders"; +import development from "./webpack/development"; +import production from "./webpack/production"; +const {NODE_ENV} = process.env; - -var configuration = { +let configuration = { output: { path: path.resolve("./dist"), filename: "[name].js", @@ -19,18 +18,16 @@ var configuration = { hot: true }, module: { - loaders: loaders + loaders } }; -switch (process.env.NODE_ENV) { +switch (NODE_ENV) { case "production": - console.info("Using production configurations"); configuration = merge(configuration, production); break; case "development": - console.info("Using development configurations"); configuration = merge(configuration, development); break; @@ -38,4 +35,6 @@ switch (process.env.NODE_ENV) { throw new Error("Please define your NODE_ENV to development or production!"); } -module.exports = configuration; \ No newline at end of file +console.info(`Using ${NODE_ENV} configurations`); + +export default configuration; \ No newline at end of file diff --git a/webpack.transpiler.js b/webpack.transpiler.js new file mode 100644 index 0000000..9a34e4e --- /dev/null +++ b/webpack.transpiler.js @@ -0,0 +1,10 @@ +/** + * This code transpiles the webpack config from ES6 to ES5 + * While Node not supports 100% ES6 keep using this file as config file for webpack! + */ + +var babel = require("babel-core"); +var path = require("path"); +var transpiled = babel.transformFileSync(path.resolve("./webpack.config.js")); + +module.exports = eval(transpiled.code); \ No newline at end of file diff --git a/webpack/loaders.js b/webpack/loaders.js index 5d869f1..b831c45 100644 --- a/webpack/loaders.js +++ b/webpack/loaders.js @@ -30,4 +30,4 @@ module.exports = [ {test: /\.(woff2?|svg)$/, loader: require.resolve("url-loader")}, {test: /\.(ttf|eot|png|jpge?g)$/, loader: require.resolve("file-loader")}, {test: /\.json$/, exclude: "/node_modules/", loader: require.resolve("json-loader")} -] \ No newline at end of file +]; \ No newline at end of file