如何为节点和Web发布npm包?

时间:2018-02-03 23:34:41

标签: javascript node.js npm webpack

将webpack模块发布到npm的正确方法是什么,以便节点用户获得webpack config target =“node”的版本,想要在浏览器中使用它的用户获得webpack config target =的缩小版本网“?

我的webpack配置创建lib/markdown-components.js(target:node,unminified)和lib/markdown-components.min.js(target:web,minified)和sourcemap文件。

我在package.json“main”中放了什么?

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

const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
var libraryName = 'markdown-components';

var webConfig = {
  target: 'web',
  entry: path.resolve(__dirname, 'src/index.js'),
  devtool: 'source-map',
  output: {
    path: path.resolve(__dirname, 'lib'),
    filename: libraryName + '.min.js',
    library: libraryName,
    libraryTarget: 'umd',
    umdNamedDefine: true
  },
  module: {
    loaders: [
      {
        test: /(\.jsx|\.js)$/,
        loader: 'babel-loader',
        exclude: /(node_modules|bower_components)/
      },
      {
        test: /(\.jsx|\.js)$/,
        loader: 'eslint-loader',
        exclude: /node_modules/
      }
    ]
  },
  plugins: [new UglifyJSPlugin({})]
};


var nodeConfig = {
  target: 'web',
  entry: path.resolve(__dirname, 'src/index.js'),
  devtool: 'source-map',
  output: {
    path: path.resolve(__dirname, 'lib'),
    filename: libraryName + '.js',
    library: libraryName,
    libraryTarget: 'umd',
    umdNamedDefine: true
  },
  module: {
    loaders: [
      {
        test: /(\.jsx|\.js)$/,
        loader: 'babel-loader',
        exclude: /(node_modules|bower_components)/
      },
      {
        test: /(\.jsx|\.js)$/,
        loader: 'eslint-loader',
        exclude: /node_modules/
      }
    ]
  }
};

module.exports = [webConfig, nodeConfig];

0 个答案:

没有答案