a)前端代码打包
b)javascript预处理,将es2015,jsx等新特性的语法,转换为浏览器能够识别的旧语法。
...
3.1)创建文件夹app和public,分别在public中创建index.html并写下你的html代码
3.2)在app文件夹下,放入你的原始数据和js代码文件
4.1)执行shell命令:webpack entryFile outputFile 如下:
./node_modules/.bin/webpack app/main.js public/bundle.js
那么在public中生成bundle.js便是打包后生成的文件。
该demo我已执行过上面的所有步骤,所以,已生成了public/bundle.js文件;当然使用webpack前你必须确保您已经安装了nodejs
3.1)创建文件夹app和public,分别在public中创建index.html并写下你的html代码
3.2)在app文件夹下,放入你的原始数据和js代码文件
module.exports = {
entry: __dirname + "/app/main.js",//打包的入口文件
output: {
path: __dirname + "/public",//打包后文件存放的目录
filename: "bundle.js"//打包输出的文件
}
}
webpack将会找到webpack.config.js文件,进行打包;由于是全局安装,所以可以直接执行webpack命令;在public中生成bundle.js便是打包后生成的文件。
loaders:[
{
test:/\.json$/,
loader: "json"
}
]
{
"helloMsg": "hello2 webpack form json"
}
var config = require("./config.json");
module.exports = function() {
var hello = document.createElement('div');
hello.textContent = config.helloMsg;
return hello;
};
loaders:[
{
test: /\.js$/, //正则表达式,匹配的文件名的文件将是使用该loader处理
exclude: /node_modules/,//不loader处理的黑名单,也就是在改目录下的文件和子文件夹下的文件都不会被loader处理
loader: 'babel',//loader名称
query:{//The query setting can be used to pass Additional options to the loader.在query的参数将会传给loader使用
presets: ['es2015']
}
}
]
//es2015 class
class HelloClass{
sayHello(){
var hello = document.createElement('div');
hello.textContent = "say hello by es2015 class";
return hello;
}
}
document.getElementById('root').appendChild(new HelloClass().sayHello());
参考资料: http://webpack.github.io/ https://segmentfault.com/a/1190000006178770 http://www.pro-react.com/materials/appendixA/