当前位置: 首页 > 面试题库 >

使Webpack不捆绑文件

西门洛城
2023-03-14
问题内容

因此,现在我正在使用一个原型,其中我们将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语句中都有对每个库的引用。所以我的问题是…我该如何从生成此巨大捆绑包.js文件的webpack转换成单个.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"
    // }
};

问题答案:

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

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


 类似资料:
  • 问题内容: 我有一个简单的应用程序,其中要求用户提供以下某些信息。 请提供您的域名。 **用户:www.google.com** 请提供您庞大的网址。 **用户:www.vast.xx.com** 请选择职位。a)左下。b)右下。 用户: b)右下 用户提供了这些信息后,按钮出现,用户单击以生成代码。他得到以下代码。 这是我完整的webpack配置文件:webpack config 使用此脚本,用

  • 问题内容: 我正在尝试将webpack中的每个角度模块捆绑在一起。我的目标是拥有一个app.js,它将通过以下配置与webpack捆绑在一起: 我将这个捆绑脚本放到我的index.html中,这样它将成为我应用程序的入口。我的文件夹中也有很多模块。文件夹结构如下: 我已要求这样做,所以当我加载其所有需要的文件时,都将加载。问题是我有几个类似的组件,我想告诉webpack分别捆绑每个组件,并用其包含

  • 问题内容: 我正在尝试使用webpack开发angular2应用,但最终在浏览器控制台中显示错误:Uncaught ReferenceError:未定义系统。 当我查看捆绑的js时,发现它正在使用System.register,如下所示: 我的webpack.config.js非常简单,如下所示: 谁能为我修复?谢谢。 问题答案: 正如@Ron建议的 如果您使用Webpack捆绑应用程序,则必须将

  • 我有一个使用react开发的网站,它只有一个页面,但产品包大小是1.11MIB。我正在使用firestore、firebase storage、material UI、react redux,该应用程序运行良好,除了捆绑包大小外,一切正常。 我的网页包配置文件 我不知道我在这里错过了什么来减小节点模块的大小。如果我们有任何其他选择,以减少捆绑大小,请给我一个建议。

  • 问题内容: 我不确定将Angular Cli从SystemJs切换到Webpack后如何包括JS文件(供应商)。 例如 选项A 我有一些通过npm安装的js文件。 这样无法将脚本标签添加到head标签。似乎也不是最好的方法。 选项B 将这些js文件作为webpack捆绑包的一部分包含在内。这似乎是应该完成的方式。但是我不确定如何执行此操作,因为所有webpack代码似乎都隐藏在angular-cl

  • 我最近不得不考虑一个新的软件的部署方法,它是用以下代码编写的: 雀巢6/Express 该软件将部署在160多台服务器上,分布在整个欧洲,其中一些服务器的互联网连接非常糟糕。 我做了一些研究,很多人明确建议不要捆绑销售。主要的论点是本机扩展将失败与捆绑器,如或(剧透:这是真的,但有一个解决方案)。在我看来,这在很大程度上是由于人们不关心这个事实:的作者在这个用例中使用了几乎相同的词。所以通常,我被