用于运行作为依赖项安装的命令的预期方法

时间:2023-01-04 13:42:30

I'm working on a Django project using Django Pipline to process CSS and JavaScript files when deploying the static files of the application. I would like to use Babel to compile JSX source to JavaScript in that process. For this I will need to write a pipeline plugin which calls babel and passes it the JSX file.

我正在使用Django Pipline处理Django项目,以便在部署应用程序的静态文件时处理CSS和JavaScript文件。我想使用Babel在该过程中将JSX源代码编译为JavaScript。为此,我需要编写一个管道插件,它调用babel并将其传递给JSX文件。

To install Babel and its dependencies, I've set up a minimal package.json:

要安装Babel及其依赖项,我已经设置了一个最小的package.json:

{
    "name": "my-project",
    "version": "1.0.0",
    "description": "",
    "dependencies": [
        "babel-cli@6.11.4",
        "babel-preset-react@6.11.1"
    ]
}

When I run npm install it will install all the necessary dependencies into node_modules.

当我运行npm install时,它会将所有必要的依赖项安装到node_modules中。

This all works fine but there is one part which eludes me. I have not found a straightforward way to run the babel binary. npm installs those binaries under node_modules/.bin, which is also returned by npm bin:

这一切都很好,但有一部分让我望而却步。我还没有找到一种直接运行babel二进制文件的方法。 npm在node_modules / .bin下安装这些二进制文件,这也是由npm bin返回的:

$ find node_modules -name babel
node_modules/.bin/babel
node_modules/babel-cli/lib/babel

$ npm bin
[...]/node_modules/.bin

What is the intended way to then e.g. run the babel binary?

那时的预期方式是什么?运行babel二进制文件?

Should I put that directory on my $PATH? Is there a script akin to the activate script from virtualenv? Should I hard-code the path in my project instead?

我应该把这个目录放在我的$ PATH上吗?是否有类似于virtualenv的激活脚本的脚本?我应该在项目中硬编码路径吗?

I'm sure the authors of npm have thought of that use case. Otherwise projects like Babel would live in a gray area of intended usages of npm.

我确定npm的作者已经想到了这个用例。否则像Babel这样的项目会生活在npm的预期用途的灰色区域。

1 个解决方案

#1


1  

If you use npm scripts, the binaries in node_modules/.bin will get added to your PATH. So, you could create a, say, "build" script like so:

如果使用npm脚本,则node_modules / .bin中的二进制文件将添加到PATH中。所以,你可以像这样创建一个“构建”脚本:

{
    "name": "my-project",
    "version": "1.0.0",
    "description": "",
    "dependencies": [
        "babel-cli@6.11.4",
        "babel-preset-react@6.11.1"
    ],
    "scripts": {
        "build": "babel script.js"
    }
}

And then do npm run build to execute the script.

然后执行npm run build来执行脚本。

#1


1  

If you use npm scripts, the binaries in node_modules/.bin will get added to your PATH. So, you could create a, say, "build" script like so:

如果使用npm脚本,则node_modules / .bin中的二进制文件将添加到PATH中。所以,你可以像这样创建一个“构建”脚本:

{
    "name": "my-project",
    "version": "1.0.0",
    "description": "",
    "dependencies": [
        "babel-cli@6.11.4",
        "babel-preset-react@6.11.1"
    ],
    "scripts": {
        "build": "babel script.js"
    }
}

And then do npm run build to execute the script.

然后执行npm run build来执行脚本。