node.js - How to force Babel to perform a transformation? -
i have following files:
./client/.babelrc ./client/index.js ./server/.babelrc ./server/index.js
client
./client/index.js
code is:
import 'test';
./client/.babelrc
is:
{ "presets": [ "es2015-webpack", "stage-0", "react" ] }
client code consumed webpack 2. webpack 2 supports es6 modules. es2015-webpack
babel preset transforms code down es5, not transform es6 modules commonjs. therefore, transpiled code remains unchanged.
server
./server/index.js
code is:
import './../client';
./server/.babelrc
is:
{ "presets": [ "es2015", "stage-0" ] }
node.js v5.5.0 not support es6 modules. therefore, entire code needs transpiled down es5. however, node script needs include parts of client code (e.g., config, initial state).
i try run server code using babel-node
, e.g.
cd ./server babel-node ./index.js
this produces error:
./client/index.js:1 import 'test'; ^^^^^^ syntaxerror: unexpected token
the reason because babel-node
(and babel
) respect ./client/.babelrc
.
how force babel use babel-plugin-transform-es2015-modules-commonjs
plugin (or es2015
preset) entire code base when compiling server code?
i have tried using --no-babelrc
option. ignores .babelrc
in current working directory.
have been working on similar problem
i ended removing presets [.babelrc]
adding client presets via webpack [webpack.config.js]
module: { loaders: [ { test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader', query: { presets: ["react", "es2015-webpack"] } } ] },
and running server presets via [babel-node]
"scripts": { "start": "set node_env=development; webpack --progress && babel-node --presets es2015,react modules/server.js" },
Comments
Post a Comment