获取Webpack不要捆绑文件

所以现在我正在使用一个原型,我们正在使用webpack(用于构build.tsx文件和复制.html文件)和webpack-dev-server(用于开发服务)之间的组合。 正如你可以假设我们也使用React和ReactDOM作为一些库依赖关系。 我们目前的构build输出结构如下:

dist -favicon.ico -index.html -main.js -main.js.map // for source-mapping between tsx / js files 

这将所有模块(包括库依赖关系放在大捆绑文件上)。 我想最终的结果是这样的:

 dist -favicon.ico -index.html -appName.js -appName.min.js -react.js -react.min.js -reactDOM.js -reactDOM.min.js 

我引用了index.html中的每个库和.tsx文件中的import语句。 所以我的问题是这个…我如何从webpack生产这个巨大的捆绑.js文件到单个.js文件(包括库,而不必单独指定每个)? **奖金:我知道如何做prod / dev环境标志,那么我怎样才能简化这些单独的文件(再次捆绑它们)呢?

当前webpack.config:

 var webpack = require("webpack"); // Assigning node package of webpack dependency to var for later utilization var path = require("path"); // // Assigning node package of path dependency to var for later utilization module.exports = { entry: [ "./wwwroot/app/appName.tsx", // Starting point of linking/compiling Typescript and dependencies, will need to add separate entry points in case of not deving SPA "./wwwroot/index.html", // Starting point of including HTML and dependencies, will need to add separate entry points in case of not deving SPA "./wwwroot/favicon.ico" // Input location for favicon ], output: { path: "./dist/", // Where we want to host files in local file directory structure publicPath: "/", // Where we want files to appear in hosting (eventual resolution to: https://localhost:4444/) filename: "appName.js" // What we want end compiled app JS file to be called }, // Enable sourcemaps for debugging webpack's output. devtool: "source-map", devServer: { contentBase: './dist', // Copy and serve files from dist folder port: 4444, // Host on localhost port 4444 // https: true, // Enable self-signed https/ssl cert debugging colors: true // Enable color-coding for debugging (VS Code does not currently emit colors, so none will be present there) }, resolve: { // Add '.ts' and '.tsx' as resolvable extensions. extensions: [ "", ".ico", ".js", ".ts", ".tsx", ".web.js", ".webpack.js" ] }, module: { loaders: [ // This loader copies the index.html file & favicon.ico to the output directory. { test: /\.(html|ico)$/, loader: 'file?name=[name].[ext]' }, // All files with a '.ts' or '.tsx' extension will be handled by 'ts-loader'. { test: /\.tsx?$/, loaders: ["ts-loader"] } ], preLoaders: [ // All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'. { test: /\.js$/, loader: "source-map-loader" } ] }, // When importing a module whose path matches one of the following, just // assume a corresponding global variable exists and use that instead. // This is important because it allows us to avoid bundling all of our // dependencies, which allows browsers to cache those libraries between builds. // externals: { // "react": "React", // "react-dom": "ReactDOM", // "redux": "Redux" // } }; 

更新:结束了find一个适合我的需求的解决scheme,但是,再次,以该webpack-y的方式,需要一些额外的configuration。 还是想让它更有活力一点,但是会在以后的时候完善它。 我一直在寻找的解决scheme是“块”的通用模块的能力,但我表示它作为文件名给予“入口”点提供的webpack。 我不介意将某些文件合并在一起,但合理的地方在于,如果项目不是SPA(单一页面应用程序),则需要整体文件处于组件级别。

额外的代码最终是:

 plugins: [ new webpack.optimize.CommonsChunkPlugin({ // This plugin is for extracting and created "chunks" (extracted parts of the code that are common and aren't page specific) // One of these instances of plugins needs to be specified for EACH chunk file output desired filename: "common.js", // Filename for this particular set of chunks to be stored name: "common", // Entry point name given for where to pull all of the chunks minChunks: 3 // Minimum number of chunks to be created }) ] 

我还必须通过variables名来参数化入口点(例如参见下面的内容),以便我可以将react,react-dom和redux模块分配给common.js文件。

 entry: { main: "./wwwroot/app/appName.tsx", // Starting point of linking/compiling Typescript and dependencies, will need to add separate entry points in case of not deving SPA index: "./wwwroot/index.html", // Starting point of including HTML and dependencies, will need to add separate entry points in case of not deving SPA favicon: "./wwwroot/favicon.ico", // Input location for favicon common: [ "react", "react-dom", "redux" ] // All of the "chunks" to extract and place in common file for faster loading of common libraries between pages }, 

       

网上收集的解决方案 "获取Webpack不要捆绑文件"

output设置更改为名称驱动,例如

  entry: { dash: 'app/dash.ts', home: 'app/home.ts', }, output: { path: './public', filename: 'build/[name].js', sourceMapFilename: 'build/[name].js.map' }, 

模块打包器replace模块加载器。

但是,与模块加载器相反,模块捆绑器在构build时运行

所以你可以在开发时使用模块加载器而不是模块打包器:)!