{
"name": "abc",
"version": "1.0.0",
"description": "",
"main": "webpack.config.js",
"dependencies": {
"webpack": "^4.42.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack -p",
"watch": "webpack --watch"
},
"author": "",
"license": "ISC"
}
var path = require('path');
module.exports = {
mode: 'development',
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname)
}
};
//src/index.js
const sum = require('./math');
const total = sum(1,5);
console.log(total);
//src/math.js
const sum = (a,b) => {
return a + b;
}
module.exports = sum;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script src="bundle.js"></script>
</body>
</html>
$npm init
$npm install -g webpack
// Thay đổi package.json
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack -p",
"watch": "webpack --watch"
},
// webpack.config.js
var path = require('path');
module.exports = {
mode: 'development',
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname)
}
};
$npm i -g webpack-cli
$webpack
npm install --save-dev babel-loader babel-core babel-preset-env
var path = require('path');
module.exports = {
mode: 'development',
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname,'build')
},
module: {
rules: [
{
use: 'babel-loader',
test: /\.js$/
}
]
}
};