Django网页包在全新的Django/ReactJS应用程序中编译misc JS,生成21500行文件
为了让Django和ReactJS正常工作,我一直在遵循以下教程: 我启动了一个全新的Django项目,添加了一个名为Django网页包在全新的Django/ReactJS应用程序中编译misc JS,生成21500行文件,django,reactjs,webpack,Django,Reactjs,Webpack,为了让Django和ReactJS正常工作,我一直在遵循以下教程: 我启动了一个全新的Django项目,添加了一个名为home的应用程序,但除了教程中概述的内容之外,我什么也没做 无论如何,当我编译JS时,它创建了一个大约21500行和800kb的文件。我的ReactJS文件只有大约20行,Django应用程序中没有其他JS。它似乎是在virtualenv或其他地方编译依赖项。为了防止这种情况 webpack.config.js //require our dependencies var p
home
的应用程序,但除了教程中概述的内容之外,我什么也没做
无论如何,当我编译JS时,它创建了一个大约21500行和800kb的文件。我的ReactJS文件只有大约20行,Django应用程序中没有其他JS。它似乎是在virtualenv
或其他地方编译依赖项。为了防止这种情况
webpack.config.js
//require our dependencies
var path = require('path')
var webpack = require('webpack')
var BundleTracker = require('webpack-bundle-tracker')
module.exports = {
//the base directory (absolute path) for resolving the entry option
context: __dirname,
//the entry point we created earlier. Note that './' means
//your current directory. You don't have to specify the extension now,
//because you will specify extensions later in the `resolve` section
entry: './assets/js/index',
output: {
//where you want your compiled bundle to be stored
path: path.resolve('./assets/bundles/'),
//naming convention webpack should use for your files
filename: '[name]-[hash].js',
},
plugins: [
//tells webpack where to store data about your bundles.
new BundleTracker({filename: './webpack-stats.json'}),
//makes jQuery available in every module
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery'
})
],
module: {
loaders: [
//a regexp that tells webpack use the following loaders on all
//.js and .jsx files
{test: /\.jsx?$/,
//we definitely don't want babel to transpile all the files in
//node_modules. That would take a long time.
exclude: /node_modules/,
//use the babel loader
loader: 'babel-loader',
query: {
//specify that we will be dealing with React code
presets: ['react']
}
}
]
},
resolve: {
//tells webpack where to look for modules
modulesDirectories: ['node_modules'],
//extensions that should be used to resolve modules
extensions: ['', '.js', '.jsx']
}
}
有几件事
process.env
的计算结果是“开发”还是“生产”执行不同的操作。其思想是在开发过程中使开发更容易,构建时间更快。在React的例子中,您得到的开发版本包含大量注释和额外检查(更多KB),这仅仅是因为您的环境没有设置为“生产”plugins: [
...(process.env === 'production' ? [
// set webpack process env to production
new webpack.DefinePlugin({
'process.env': { NODE_ENV: JSON.stringify('production') },
}),
new webpack.optimize.DedupePlugin(), // webpack 1.x only
new webpack.optimize.UglifyJsPlugin({ comments: false }),
] : []),
//tells webpack where to store data about your bundles.
new BundleTracker({filename: './webpack-stats.json'}),
//makes jQuery available in every module
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery'
}),
],