简体   繁体   English

捆绑 npm package 并将其转换为 es 模块

[英]bundle an npm package AND convert it into an es module

I want to import chessground into a project of mine.我想将棋盘导入我的项目中。 It seems it's a CommonJS module, so i used browserify to import it into my web page.它似乎是一个 CommonJS 模块,所以我使用 browserify 将它导入到我的 web 页面中。

browserify -r chessground > chessground.js

In order to use it in my webpage, I use为了在我的网页中使用它,我使用

const Chessground = require('chessground').Chessground

but I saw in this project that they import it like但我在这个项目中看到他们像导入它一样

import {Chessground} from 'chessground'

I know they are douing with webpack, but for the life of me I can't figure out how to bundle an entire npm package into one file AND convert it into an ES module.我知道他们正在使用 webpack,但是对于我的一生,我无法弄清楚如何将整个 npm package 捆绑到一个文件中并将其转换为一个模块。 Can anyone help me?谁能帮我?

There is noway to bundle your packages without using a bundler like webpack or rollup.js .如果不使用像webpackrollup.js这样的捆绑器,就无法捆绑你的包。

If it is necessarily to use task runner you may find a way to make the bundler work with your task runner .如果必须使用任务运行程序,您可能会找到一种方法使捆绑程序与您的任务运行程序一起工作。

I had the same problem with gulpjs and wepack , and it was really painful to make it work.我对gulpjswepack有同样的问题,让它工作真的很痛苦。 The solution was using a webpack and webpack plugins in gulpjs .解决方案是在gulpjs中使用webpack 和 webpack 插件

const webpack = require('webpack');
const webpackStream = require('webpack-stream');
const path = require('path');

const webapckJsConfig = {
  mode: (process.env.APP_ENV === 'dev' && process.env.APP_DEBUG === 'true') ? 'development' : 'production',
  devtool: (process.env.APP_ENV === 'dev' && process.env.APP_DEBUG === 'true') ? 'source-map' : false,
  entry: {
    // Website JS Files
    'website/home.js': './resources/js/website/home.js',
    'website/notfound.js': './resources/js/website/notfound.js',
    'website/error.js': './resources/js/website/error.js',

    // Admin JS Files
    'admin/home.js': './resources/js/admin/home.js',
    'admin/notfound.js': './resources/js/admin/notfound.js',
  },
  output: {
    path: path.resolve(__dirname, 'public/'),
    filename: '[name]',
  },
  module: {
    rules: [
      {
        test: /\.m?js$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: 'babel-loader',
        },
      },
    ],
  },
};

// JavaScript lint Cehck ✅ Convert 🔂 Compresse 🔄 Output ↪ 📁 public/js
async function scripts() {
  return gulp
    .src(`${JAVASCRIPT_DIR}/**/*.js`)
    .pipe(webpackStream(webapckJsConfig), webpack)
    .pipe(gulp.dest(`${JS_PUBLIC_DIR}`));
}
exports.scripts = scripts;

you can also check it here你也可以在这里查看

You can of course take the webpack configuration and put them in external file, but you have to consider that you will confuse other developers because they will think webpack is independent and start to use it as an independent tool, so that's why I keep it in gulp.js .你当然可以把 webpack 配置放到外部文件中,但是你要考虑到你会迷惑其他开发者,因为他们会认为 webpack 是独立的,并开始将它作为一个独立的工具使用,所以这就是我保留它的原因gulp.js

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

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