简体   繁体   English

Webpack包名为requirejs modules

[英]Webpack bundle named requirejs modules

In my company we have a significant AMD RequireJS code base and I am trying to use webpack to bundle some of it. 在我的公司,我们有一个重要的AMD RequireJS代码库,我正在尝试使用webpack捆绑其中一些。 Here is what the architecture looks like: 这是架构的样子:

somedir
│   app.js
│
└───anotherdir
    |
    ├─── module1
    │       file1.js
    │       file2.js
    │
    ├─── module2
    │       file3.js
    │
    └─── module3
            file4.js
            file5.js
            file6.js

Each file is written like that : 每个文件都是这样写的:

define('ATAG/MODULE/ID, ['somedeps'], function (somedeps) {});

So for instance file1.js could look like 所以例如file1.js看起来像

define('ATAG/module1/file1, [], function () {});

And we have a RequireJS config which maps ATAG to anotherdir and we possibly have some more config for different tags. 我们有一个RequireJS配置,它将ATAG映射到anotherdir ,我们可能有更多的配置用于不同的标签。

Now I am trying to create a bundle from app.js with webpack but I have no idea of how to replicate the behavior we have with require.config({ paths: { ATAG: 'anotherdir' } }) . 现在我正在尝试使用webpack从app.js创建一个包,但我不知道如何使用require.config({ paths: { ATAG: 'anotherdir' } })复制我们的行为。

So far my attempts with resolve.alias have not been successful. 到目前为止,我对resolve.alias尝试都没有成功。

Is it even possible to achieve something like this with webpack based on our usage of RequireJS (not requiring relative paths) ? 根据我们对RequireJS的使用(不需要相对路径),甚至可以使用webpack实现这样的功能吗?

Thank you. 谢谢。

You were on the right path with resolve.alias : 你使用resolve.alias走在正确的道路上:

// foo.js
var a = require('ATAG'); // resolved to anotherdir/index.js
var b = require('ATAG/bar'); // resolved to anotherdir/bar.js

// webpack.config.js
const path = require('path');

module.exports = {
    entry: './foo',
    output: {
        filename: 'bundle.js'
    },
    resolve: {
        alias: {
            ATAG: path.join(__dirname, '/anotherdir')
        }
    }
};

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

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