首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在webpack中包含多部件库?

如何在webpack中包含多部件库?
EN

Stack Overflow用户
提问于 2016-10-26 16:36:10
回答 1查看 668关注 0票数 0

我创建了一个类似于webpack/webpack/.../multi-part-library中的示例的多部件库。在我的应用程序中,我希望能够像这样导入我的库的一部分:

代码语言:javascript
复制
ìmport Button from 'myLib/atoms/button';

// or

import { Button } from 'myLib/atoms';

我的应用程序的webpack配置如下所示,我得到了一个错误(Cannot resolve module 'myLib/atoms'Cannot resolve module 'myLib/atoms/button'):

代码语言:javascript
复制
module.exports = {
    'entry': {
        'app': './client.js',
    },
    'output': {
        'filename': 'bundle.js',
    },
    'externals': {
        'react': true,
        'react-dom': true,
        'myLib': true,
    },
    'module': {
        'loaders': [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: 'babel',
            },
        ]
    },
};

库的webpack配置如下所示:

代码语言:javascript
复制
const files = glob.sync('**/*.js', {
    cwd: path.join(__dirname, 'atomic-design'),
    absolute: true,
});

let entries = {};

files.forEach((file) => {
    const name = path.basename(path.dirname(file));
    const newName = `atoms/${name}`;
    entries[newName] = file;
});

module.exports = {
    'entry': entries,
    'output': {
        'path': path.join(__dirname, 'lib'),
        'filename': 'myLib.[name].js',
        'library': ['myLib', '[name]'],
        'libraryTarget': 'umd',
        'umdNamedDefine': 'myLib',
    },
    'externals': {
        'react': true,
        'react-dom': true,
    },
    'module': {
        'loaders': [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: 'babel'
            },
        ]
    }
};

这些文件的结构如下:

代码语言:javascript
复制
- app
    - client.js
    - webpack.config.js
- myLib
    - webpack.config.js
    - atomic-design
        - button
            - index.js
        - text-field
            - index.js

到目前为止,我只能找到使用webpack创建库的教程,在这些教程中,他们只使用库的小示例。

提前感谢您的帮助!

致以最好的问候,JBrieske

EN

回答 1

Stack Overflow用户

发布于 2016-10-27 15:47:36

我认为您需要为您尝试导入resolve.modulesDirectories的模块添加路径(并相应地调整from 'path' )。

相关文档:https://webpack.github.io/docs/configuration.html#resolve-modulesdirectories

请记住this will change in Webpack2,它功能齐全,只是缺少发布的文档。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40257442

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档