B. *_*unt 26 javascript node.js typescript reactjs 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 files
Run Code Online (Sandbox Code Playgroud)
这会将所有模块(包括库依赖项放在大型捆绑文件中)放置.我希望最终结果如下所示:
dist
-favicon.ico
-index.html
-appName.js
-appName.min.js
-react.js
-react.min.js
-reactDOM.js
-reactDOM.min.js
Run Code Online (Sandbox Code Playgroud)
我引用了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"
// }
};
Run Code Online (Sandbox Code Playgroud)
更新:结束找到符合我需求的解决方案,尽管再次以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
})
]
Run Code Online (Sandbox Code Playgroud)
我还必须通过变量名参数化入口点(例如见下文),以便我可以将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
},
Run Code Online (Sandbox Code Playgroud)
bas*_*rat 15
将output设置更改为名称驱动,例如
entry: {
dash: 'app/dash.ts',
home: 'app/home.ts',
},
output: {
path: './public',
filename: 'build/[name].js',
sourceMapFilename: 'build/[name].js.map'
},
Run Code Online (Sandbox Code Playgroud)
Mik*_*cci 11
要扩展@basarat 的答案,您可以使用glob节点标准库中的包来构建“条目”配置:
const glob = require("glob");
module.exports = [
{
target: "node",
entry: glob.sync("./src/**/*.test.{ts,tsx}").reduce((acc, file) => {
acc[file.replace(/^\.\/src\//, "")] = file;
return acc;
}, {}),
output: {
filename: "[name].js",
chunkFilename: "[name]-[id].js",
path: __dirname + "/dist"
},
//...
}
];
Run Code Online (Sandbox Code Playgroud)
这将构建与其源同名的文件.ts,.tsx用.js.
| 归档时间: |
|
| 查看次数: |
23657 次 |
| 最近记录: |