模块导入而不是相对路径导入webpack

时间:2018-12-03 11:47:10

标签: javascript reactjs webpack

我的第二个本地程序包具有通用实用程序,可用于我的主应用程序。 我配置了webpack和package.json,但是问题是当我在主应用程序中安装本地包时,无法通过模块导入来导入内容,例如:

import {Thing} from "my-commons";

相反,我必须这样写:

import {Thing} from "node_modules/my-commons/dist/lib/index";

此本地包中的我的webpack:

const path = require('path');
const nodeExternals = require('webpack-node-externals');

module.exports = {
  entry: path.resolve(__dirname, './src/lib/index.js'),
  output: {
    path: path.resolve(__dirname, './dist/lib'),
    filename: 'index.js',
    library: '',
    libraryTarget: 'commonjs'
  },
  externals: [nodeExternals()],
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /(node_modules|bower_components)/,
        loader: 'babel-loader',
        query: {
          plugins: ['transform-decorators-legacy', 'lodash'],
          presets: ['es2015', 'stage-2', 'react', 'stage-0']
        }
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader']
      }
    ],

  },
  resolve: {
    alias: {
      uiutils$: path.resolve(__dirname, './dist/lib/index.js')
    }
  },

};

然后  src/lib/Thing/index.js我通过导出实现了React组件:

export class Thing extends..等等

src/lib/index.js中,我有:

export {Thing} from "./Thing";

问候所有提示!

0 个答案:

没有答案