简体   繁体   English

Webpack创建两个没有入口点的包

[英]Webpack creating two bundles without entry point

I have the following source setup: 我具有以下源设置:

src
|__ client
|  |__ a.ts
|  |__ b.js
|
|__ common
|  |__ c.ts
|  |__ d.js
|
|__ server
|  |__ e.ts
|  |__ f.js

Both typescript and javascript files can exist in the sources. 源文件中可以同时包含打字稿和javascript文件。 However, there is no entrypoint (no 'main file'). 但是,没有入口点(没有“主文件”)。

My goal is that I get two bundles: 我的目标是得到两个捆绑包:

  • bundle_client.js bundle_client.js
  • bundle_server.js bundle_server.js

Each bundle contains their appropriate code (typescript transpiled, and JS just passing through however the typescript transpiler can do that using allowJs ), as well as both bundles containing the code from common. 每个捆绑软件都包含其适当的代码(已翻译的打字稿和刚刚通过的JS,但是打字稿翻译可以使用allowJs做到这allowJs ),以及两个捆绑包都包含来自通用的代码。

This was easy with gulp, but I want to migrate to webpack and I can't figure it out. 用gulp进行操作很容易,但是我想迁移到webpack,却无法解决。

This is the broken config I currently have: 这是我目前有损坏的配置:

const path = require('path');
const glob = require("glob");

const config = {
    mode: 'development',
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                use: 'ts-loader',
                exclude: /node_modules/
            }
        ],
        resolve: {
            extensions: ['.tsx', '.ts', '.js']
        }
    },
};

const clientConfig = Object.assign({}, config, {
    name: "client",
    //entry: glob.sync("./src/client/**/*.ts"),
    module: {
        rules: [
            {
                include: ['/src/client/**/*', '/src/common/**/*'],
                // test: /\.tsx?$/,
                use: 'ts-loader',
                exclude: /node_modules/
            }
        ]
    },
    output: {
        filename: 'bundle_client.js',
        path: path.resolve(__dirname, 'out')
    }
});

const serverConfig = Object.assign({}, config, {
    name: "server",
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                use: 'ts-loader',
                exclude: /node_modules/,
                include: '/src/server/**/*'
            }
        ]
    },
    output: {
        filename: 'bundle_server.js',
        path: path.resolve(__dirname, 'out')
    }
});

module.exports = [
    clientConfig, serverConfig,
];

Googling around I found a couple of things that should help: 到处搜寻,我发现了几件事应该有所帮助:

  • Exporting multiple configs allows me to create separate bundles 导出多个配置可让我创建单独的捆绑包
  • I can use blob to put all files as entry point 我可以使用Blob将所有文件作为入口点

Reading up some more, I suddenly remembered you can have child tsconfig.json files in subdirectories of your project structure. 阅读更多内容后,我突然想起您可以在项目结构的子目录中拥有子tsconfig.json文件。 Also it was made clear that webpack really, really wants an entry point, so I made a compromise, and found a quite elegant solution. 另外,很明显Webpack确实非常想要一个切入点,因此我做出了让步,找到了一个非常优雅的解决方案。

I added a main.ts to both client and server directories, as well as a tsconfig.json . 我将main.ts添加到客户端和服务器目录,以及tsconfig.json The main (entry point), imports the rest (so webpack can create its dependency graph) and I have 3 tsconfig.json files. 主要(入口点),导入其余部分(以便webpack可以创建其依赖关系图),我有3个tsconfig.json文件。

src/tsconfig.json : src/tsconfig.json

{
  "compilerOptions": {
    "module": "es6",
    "noImplicitAny": true,
    "removeComments": true,
    "preserveConstEnums": true,
    "allowJs": true,
    "target": "es2017"
  },
  "include": [
    "src/client/tsconfig.json",
    "src/server/tsconfig.json"
  ]
}

src/client/tsconfig.json : src/client/tsconfig.json

{
  "extends": "../../tsconfig",
  "include": [
    "./**/*.ts",
    "../common/**/*.ts",
    "../../typings/natives_universal.d.ts",
    "../../typings/index.d.ts"
  ]
}

And the same for server's tsconfig. 与服务器的tsconfig相同。

And finally, this is my webpack config: 最后,这是我的webpack配置:

const path = require('path');

const clientConfig = {
    entry: './src/client/main.ts',
    mode: 'development',
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                use: 'ts-loader',
                exclude: /node_modules/
            }
        ]
    },
    resolve: {
        extensions: [ '.tsx', '.ts', '.js' ]
    },
    output: {
        filename: 'bundle_client.js',
        path: path.resolve(__dirname, 'out')
    }
};

const serverConfig = {
    entry: './src/server/main.ts',
    mode: 'development',
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                use: 'ts-loader',
                exclude: /node_modules/
            }
        ]
    },
    resolve: {
        extensions: [ '.tsx', '.ts', '.js' ]
    },
    output: {
        filename: 'bundle_server.js',
        path: path.resolve(__dirname, 'out')
    }
};
module.exports = [
    clientConfig, serverConfig
];

This setup has several advantages: 此设置具有以下优点:

  1. Client has code completion (depending on editor) for client code and common code (and whatever external stuff is loaded), and same for server. 客户端具有客户端代码和通用代码(以及加载的外部内容)的代码完成(取决于编辑器),服务器具有相同的代码完成功能。
  2. It creates two bundles in the out directory, one for client and one for server. 它在out目录中创建两个捆绑包,一个捆绑包用于客户端,一个捆绑包用于服务器。
  3. Only the necessary files are bundled, and following a normal workflow, client code and server code will never mix. 仅捆绑必要的文件,并且按照正常的工作流程,客户端代码和服务器代码将永远不会混合在一起。

The only "downside" if you can call it that, is that I need an entry point, being main.ts which imports/executes other things throughout my code. 如果可以调用的话,唯一的“缺点”是我需要一个入口点,即main.ts,它可以在我的代码中导入/执行其他操作。 Additionally, it is only softly enforced that client and server code doesn't get mixed. 此外,仅轻柔地强制客户端和服务器代码不混淆。 You can mix them, if you import them, as opposed to just not being able to locate the other's files when building. 如果导入它们,则可以混合它们,而不是在构建时无法找到其他文件。

But I am glad to make that compromise over having to violate webpack. 但是我很高兴在不得不违反Webpack的情况下做出了让步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM