【发布时间】:2017-02-27 00:46:45
【问题描述】:
所以现在我正在使用一个原型,我们正在使用 webpack(用于构建 .tsx 文件和复制 .html 文件)和 webpack-dev-server 之间的组合用于开发服务。你可以假设我们也使用 React 和 ReactDOM 作为两个库依赖项。我们当前的构建输出是以下结构:
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"
// }
};
【问题讨论】:
-
Webpack 的唯一目的是做捆绑;你确定你不想要任何捆绑,或者你只是想要一些东西捆绑而其他东西不?如果是后一种选择,您可以了解为什么需要单独指定每个文件。
-
所以我喜欢使用 webpack 将 Typescript 转换为 Javascript 的事实。我知道我们仍然需要一个构建系统,但不想仅仅依赖 gulp 或(就像很多人习惯使用的那样)grunt。我也不知道使用 npm 脚本之类的东西是否会提供足够的功能,除非那里的单个包可以满足相同的需求。鉴于这些情况,您能否提供进一步的建议?
-
我过去已经发现webpack允许基于通用模块进行分块......如果有人可以根据上述提供更多信息,那也可以解决我的问题。谢谢!
-
就我个人而言,我认为 webpack 只是一个很好的打包构建工具,如果你不想打包,我会坚持使用 gulp 或 NPM 脚本。
-
@Jacob Webpack 的亮点还在于它允许您需要非 JS 文件。如果你正在尝试构建一个同构的应用程序,你必须在客户端和服务器上都使用 webpack。但是,服务器实际上并没有从捆绑中受益。
标签: javascript node.js reactjs typescript webpack