javascript - How to use webpack CLI output a module which can be imported? -
there 3 files here.
file 1: src/module/a/index.js
import b './b.js'; import c '../c/index.js'; const d = () => 'd'; export default { b, c, d }; file 2: src/module/a/b.js
export default () => 'b'; file 3: src/module/c/index.js
export default () => 'c' i want use webpack cli package code file.
// file dist/lib/a.js const b = () => 'b'; const c = () => 'c'; const d = () => 'd'; export default { b, c, d };
because using es6 code, need transpiler babel, need webpack.config.js file in root directory.
here minimal setup webpack.config.js
const path = require('path'); const config = { entry: './src/index.js', // path index.js output: { path: path.resolve(__dirname, 'build'), filename: 'bundle.js', // output file publicpath: 'build/' // output dir }, module: { rules: [ { use: 'babel-loader', test: /\.js$/ } ] } }; module.exports = config; here minimal package.json file
add script, can run later via npm run build command
"scripts": { "build": "webpack" }, add dev dependencies, can later install using npm install, or install separately using command
npm --save-dev babel-core babel-loader babel-preset-env webpack
"devdependencies": { "babel-core": "^6.21.0", "babel-loader": "^6.2.10", "babel-preset-env": "^1.1.4", "webpack": "^2.2.0-rc.0" } depending on needs can later add other babel presets, es2015 , stage-0
npm install --save-dev babel-preset-es2015 npm install --save-dev babel-preset-stage-0 now add .babelrc file root directory, paste code
{ "presets": ["babel-preset-env"] }
Comments
Post a Comment