Javascript 获取网页包以不捆绑文件
所以现在我正在使用一个原型,在这个原型中,我们使用webpack(用于构建.tsx文件和复制.html文件)和webpack dev server之间的组合来提供开发服务。正如您可以假设的那样,我们也将React和ReactDOM用作两个库依赖项。我们当前的构建输出是以下结构:Javascript 获取网页包以不捆绑文件,javascript,node.js,reactjs,typescript,webpack,Javascript,Node.js,Reactjs,Typescript,Webpack,所以现在我正在使用一个原型,在这个原型中,我们使用webpack(用于构建.tsx文件和复制.html文件)和webpack dev server之间的组合来提供开发服务。正如您可以假设的那样,我们也将React和ReactDOM用作两个库依赖项。我们当前的构建输出是以下结构: dist -favicon.ico -index.html -main.js -main.js.map // for source-mapping between tsx / js fil
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语句中引用了每个库。所以我的问题是。。。
如何从生成这个庞大的捆绑.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"
// }
};
将
输出设置更改为名称驱动,例如
要扩展@basarat的答案,您可以使用node的标准库中的glob
包来构建“entry”配置:
const glob=require(“glob”);
module.exports=[
{
目标:“节点”,
条目:glob.sync(“./src/***.test.{ts,tsx}”).reduce((acc,file)=>{
acc[file.replace(/^\.\/src\/,“”)]=file;
返回acc;
}, {}),
输出:{
文件名:“[name].js”,
chunkFilename:“[name]-[id].js”,
路径:uu dirname+“/dist”
},
//...
}
];
这将生成与源文件同名的文件,将.ts
和.tsx
替换为.js
从问题中复制的答案
最终找到了一个适合我的需要的解决方案,尽管同样,在webpack-y方式中,需要一些额外的配置。仍然希望使其更具活力,但将在稍后的时间点完善此功能。我想要的解决方案是能够“分块”通用模块,但我将其称为给定“入口”的文件名,即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
})
]
我还必须通过变量名参数化入口点(见下面的示例),以便将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的唯一目的是进行捆绑;你确定你不想要任何捆绑,还是你只想要一些东西捆绑,而另一些东西不捆绑?如果是后一个选项,您可以看到为什么需要单独指定每个文件。因此,我喜欢这样一个事实:我可以使用webpack将Typescript转换为Javascript。我知道我们仍然需要一个构建系统,但不想仅仅依赖于吞咽或(很多人过去使用的)咕噜。我也不知道使用像npm脚本这样的东西是否能提供足够的功能,除非单独的软件包可以满足同样的需求。考虑到这些情况,您能提供进一步的建议吗?我过去已经发现webpack允许基于常见模块进行分块。。。如果有人能提供进一步的信息,根据上述,这也将解决我的问题。谢谢就我个人而言,我认为webpack只是一个很好的捆绑构建工具,如果你不想捆绑,我会坚持使用gulp或NPM脚本。@Jacob webpack还因为它允许你需要非JS文件而大放异彩。如果你试图构建一个同构的应用程序,你必须在客户端和服务器上使用webpack。然而,服务器实际上并没有从捆绑中受益。经过一些研究,最终的解决方案是最接近的。我在上面的解释中添加了对最终解决方案的评论。使用您的方法的一个缺点是,当指定不同类型的文件作为文件加载程序的入口点(即favicon.ico、index.html)时,它们也将与.js及其原始文件一起输出。您知道这会在项目中节省多少时间吗?如果webpack基本上什么都不做,只是将文件从输入复制到输出,那么在添加传输和其他插件/加载程序转换之前,会有很大的开销吗?您是否估计不绑定与绑定相比节省了多少时间(在分析时间内?)?
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
},