我可以用咖啡稿写npm包吗?

时间:2021-04-04 22:57:46

I have used coffeescript for a while. Now I need to write a npm package, can I write it in coffeescript, or I should compile coffeescript into javascript?

我用咖啡稿已经有一段时间了。现在我需要编写一个npm包,我可以用coffeescript写,或者我应该把coffeescript编译成javascript吗?

5 个解决方案

#1


99  

I'm going to suggest that you write your package in coffeescript, but only publish it in javascript. I do it like this:

我建议您使用coffeescript编写包,但是只使用javascript发布包。我是这样做的:

  • coffeescript code goes in src
  • coffeescript代码在src中
  • code is compiled to lib
  • 代码编译为lib
  • src is committed to my git repo, lib is in my .gitignore
  • src对我的git repo负责,lib在我的.gitignore
  • lib is published to npm, src is in my .npmignore
  • lib被发布到npm, src在我的。npmignore中
  • the coffee-script package is in my devDependencies
  • 咖啡脚本包在我的开发依赖项中

You can take a look at a simple package of mine, refix, for inspiration:

你可以看看我的一个简单的包,refix,作为灵感:

#2


10  

You can write NPM modules in coffeescript, but in order for them to be usable by JS users they must be compiled to JS before you publish on NPM.

您可以在coffeescript中编写NPM模块,但是为了让JS用户能够使用它们,必须在NPM发布之前将它们编译成JS。

package.json makes this easy with their prepublish script hook which runs the specified script before you publish. Heres an example of a prepublish NPM hook in zombie.js

包中。json使用它们的预发布脚本挂钩使这变得很容易,该脚本在发布之前运行指定的脚本。这里有一个在zombi .js中预先发布NPM钩子的例子

https://github.com/assaf/zombie/blob/master/package.json#L16

https://github.com/assaf/zombie/blob/master/package.json L16

#3


1  

I have written npm packages in CoffeeScript from scratch. I encourage you to use CoffeScript for node as well as for the Browser. However, before you can use or publish your module, you have to compile the source CoffeeScript to JavaScript. That should not hold you back from using CoffeeScript, though.

我已经从头开始写了npm软件包。我鼓励您将CoffeScript用于node以及浏览器。但是,在使用或发布模块之前,必须将源CoffeeScript编译为JavaScript。不过,这不应该妨碍你使用咖啡。

Tip: While developing, use coffee -cw yourfile.coffee (command line) to watch the file for changes and compile on save.

提示:在开发过程中,使用咖啡-cw你的文件。coffee(命令行)监视文件的更改并在保存时进行编译。

#4


0  

While I'm not sure if it's the best approach, technically it is possible to write your package mostly in CoffeeScript.

虽然我不确定这是否是最好的方法,但从技术上讲,主要是用coffecript书写软件包是可能的。

Basically, you can write a JS file that simply wraps the coffee command, like so:

基本上,您可以编写一个简单地包装coffee命令的JS文件,如下所示:

bin/howl.coffee

bin / howl.coffee

console.log 'Awwwooooo!'

bin/howl.js

bin / howl.js

#!/usr/bin/env node

var path    = require('path');
var exec    = require('child_process').exec;
var coffee  = path.resolve(__dirname, '../node_modules/coffee-script/bin/coffee');
var howl    = path.resolve(__dirname, './howl.coffee');
var command = coffee + ' ' + howl;

exec(command, function(error, stdout) {
  if (error) { throw error };
  console.log(stdout);
});

Running node howl.js (or simply howl when it's installed globally) will now output Awwooooo!. You can do things like require other CoffeeScript files and access arguments by passing them from the JavaScript "wrapper" to the CoffeeScript.

运行节点嚎叫。js(或在全局安装时简单地发出嚎叫)现在将输出Awwooooo!您可以通过将其他的CoffeeScript文件从JavaScript“包装器”传递到CoffeeScript来做一些事情,比如需要其他的coffecript文件和访问参数。

Anyway, there may be reasons not to do this, but it has worked for me so far so figured I'd submit this for an additional perspective.

不管怎样,也许有理由不这样做,但到目前为止,它对我很有用,因此我认为我应该提交这个以获得更多的视角。

For a simple example project using this technique, check out https://www.github.com/joshuabc/packdown.

对于一个使用此技术的简单示例项目,请查看https://www.github.com/joshuabc/packdown。

#5


0  

If a lot of your modules have coffee-script in their devDependencies, it's useful to just globally install coffee-script instead of install it for each module (which takes much longer).

如果您的许多模块在它们的devDependencies中都有咖啡吧脚本,那么最好是全局地安装咖啡吧脚本,而不是为每个模块安装它(这会花费更长的时间)。

coffee-build is a global version manager for coffee-script.

coffee-build是咖啡脚本的全球版本经理。

Just add these 2 scripts to your package.json:

只需将这两个脚本添加到您的包中。

{
  "name": "my-coffee-module",
  "scripts": {
    "build": "coffee-build -v 1.11.x -b -o js src",
    "postinstall": "npm run build"
  }
}

Notice how -v 1.11.x is not an exact version, which allows implicit upgrades.

注意- v 1.11。x不是一个确切的版本,它允许隐式升级。

The only downfall is that users must npm install -g coffee-build before they can install your module.

唯一的缺点是用户必须在安装模块之前安装-g coffee-build。

#1


99  

I'm going to suggest that you write your package in coffeescript, but only publish it in javascript. I do it like this:

我建议您使用coffeescript编写包,但是只使用javascript发布包。我是这样做的:

  • coffeescript code goes in src
  • coffeescript代码在src中
  • code is compiled to lib
  • 代码编译为lib
  • src is committed to my git repo, lib is in my .gitignore
  • src对我的git repo负责,lib在我的.gitignore
  • lib is published to npm, src is in my .npmignore
  • lib被发布到npm, src在我的。npmignore中
  • the coffee-script package is in my devDependencies
  • 咖啡脚本包在我的开发依赖项中

You can take a look at a simple package of mine, refix, for inspiration:

你可以看看我的一个简单的包,refix,作为灵感:

#2


10  

You can write NPM modules in coffeescript, but in order for them to be usable by JS users they must be compiled to JS before you publish on NPM.

您可以在coffeescript中编写NPM模块,但是为了让JS用户能够使用它们,必须在NPM发布之前将它们编译成JS。

package.json makes this easy with their prepublish script hook which runs the specified script before you publish. Heres an example of a prepublish NPM hook in zombie.js

包中。json使用它们的预发布脚本挂钩使这变得很容易,该脚本在发布之前运行指定的脚本。这里有一个在zombi .js中预先发布NPM钩子的例子

https://github.com/assaf/zombie/blob/master/package.json#L16

https://github.com/assaf/zombie/blob/master/package.json L16

#3


1  

I have written npm packages in CoffeeScript from scratch. I encourage you to use CoffeScript for node as well as for the Browser. However, before you can use or publish your module, you have to compile the source CoffeeScript to JavaScript. That should not hold you back from using CoffeeScript, though.

我已经从头开始写了npm软件包。我鼓励您将CoffeScript用于node以及浏览器。但是,在使用或发布模块之前,必须将源CoffeeScript编译为JavaScript。不过,这不应该妨碍你使用咖啡。

Tip: While developing, use coffee -cw yourfile.coffee (command line) to watch the file for changes and compile on save.

提示:在开发过程中,使用咖啡-cw你的文件。coffee(命令行)监视文件的更改并在保存时进行编译。

#4


0  

While I'm not sure if it's the best approach, technically it is possible to write your package mostly in CoffeeScript.

虽然我不确定这是否是最好的方法,但从技术上讲,主要是用coffecript书写软件包是可能的。

Basically, you can write a JS file that simply wraps the coffee command, like so:

基本上,您可以编写一个简单地包装coffee命令的JS文件,如下所示:

bin/howl.coffee

bin / howl.coffee

console.log 'Awwwooooo!'

bin/howl.js

bin / howl.js

#!/usr/bin/env node

var path    = require('path');
var exec    = require('child_process').exec;
var coffee  = path.resolve(__dirname, '../node_modules/coffee-script/bin/coffee');
var howl    = path.resolve(__dirname, './howl.coffee');
var command = coffee + ' ' + howl;

exec(command, function(error, stdout) {
  if (error) { throw error };
  console.log(stdout);
});

Running node howl.js (or simply howl when it's installed globally) will now output Awwooooo!. You can do things like require other CoffeeScript files and access arguments by passing them from the JavaScript "wrapper" to the CoffeeScript.

运行节点嚎叫。js(或在全局安装时简单地发出嚎叫)现在将输出Awwooooo!您可以通过将其他的CoffeeScript文件从JavaScript“包装器”传递到CoffeeScript来做一些事情,比如需要其他的coffecript文件和访问参数。

Anyway, there may be reasons not to do this, but it has worked for me so far so figured I'd submit this for an additional perspective.

不管怎样,也许有理由不这样做,但到目前为止,它对我很有用,因此我认为我应该提交这个以获得更多的视角。

For a simple example project using this technique, check out https://www.github.com/joshuabc/packdown.

对于一个使用此技术的简单示例项目,请查看https://www.github.com/joshuabc/packdown。

#5


0  

If a lot of your modules have coffee-script in their devDependencies, it's useful to just globally install coffee-script instead of install it for each module (which takes much longer).

如果您的许多模块在它们的devDependencies中都有咖啡吧脚本,那么最好是全局地安装咖啡吧脚本,而不是为每个模块安装它(这会花费更长的时间)。

coffee-build is a global version manager for coffee-script.

coffee-build是咖啡脚本的全球版本经理。

Just add these 2 scripts to your package.json:

只需将这两个脚本添加到您的包中。

{
  "name": "my-coffee-module",
  "scripts": {
    "build": "coffee-build -v 1.11.x -b -o js src",
    "postinstall": "npm run build"
  }
}

Notice how -v 1.11.x is not an exact version, which allows implicit upgrades.

注意- v 1.11。x不是一个确切的版本,它允许隐式升级。

The only downfall is that users must npm install -g coffee-build before they can install your module.

唯一的缺点是用户必须在安装模块之前安装-g coffee-build。