简体   繁体   English

使用package.json构建一个js项目

[英]Build a js project with package.json

I've two js projects for say A and B, In the Project A's packge.json I've following scripts 我有两个js项目,分别说A和B,在项目A的packge.json中,我正在跟踪脚本

 "scripts": {
"start": "webpack-dev-server --https --content-base dist/",
"doc": "n-clean docs && jsdoc --package package.json -c 
 ./jsdoc.config.json",
"clean": "n-clean dist",
"build:dev": "webpack --config webpack.config.js",
"build:prod": "webpack --config webpack.config.js --env.production",
"build": "npm run clean && npm run build:dev && npm run build:prod && npm run doc"

} }

when I run npm run build , I got a bundle js in dist folder of project A. 当我运行npm run build时,我在项目A的dist文件夹中得到了一个捆绑包js。

In B's project , I've the following package.json for scripts. 在B的项目中,我具有以下用于脚本的package.json。

"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"serve": "grunt serve --port=9090 --https=true",
"build": "grunt build --production=true"   

} }

So I can build project B by running npm run build. 因此,我可以通过运行npm run build来构建项目B。 But can I build Project B after building Poject A(ie it should create bundle in A's dist folder),with in Project B's package.json by adding commands like "build":"npm run build ../A" along with project B's build. 但是我可以在构建Poject A之后构建Project B(即它应该在A的dist文件夹中创建捆绑包),并在Project B的package.json中通过添加“ build”:“ npm run build ../A”命令与项目B一起添加。建立。 So Please help me how to achieve this. 因此,请帮助我如何实现这一目标。

Yes, you can absolutely build Project B's files into Project A's dist folder. 是的,您绝对可以将Project B的文件构建到Project A的dist文件夹中。

You'll have to modify your webpack's config file, it is typically called webpack.config.js 您必须修改webpack的配置文件,通常称为webpack.config.js

In Project B's webpack.config file, you'll have to tell webpack the output path. 在项目B的webpack.config文件中,您必须告诉webpack输出路径。 You can do so by adding this line of code: 您可以通过添加以下代码行来实现:

module.exports = {
  //...
  output: {
    path: path.resolve(__dirname, '<Path to Project A dist folder>')
  }
};

Hope this helps! 希望这可以帮助!

Edit in response to building Project A in Project B: 根据项目B中的项目A进行编辑:

Yes, you'll need to modify your Project B's webpack.config.js file. 是的,您需要修改Project B的webpack.config.js文件。

You can define multiple entry points in that file as 您可以在该文件中将多个入口点定义为

module.exports = {
  entry: {
    ProjectA: './path/to/my/entry/fileA.js',
    ProjectB: './path/to/my/entry/fileB.js',
  }
};

Likely you can copy the entry point from your ProjectA's webpack.config.js file into Project B's. 可能您可以将入口点从ProjectA的webpack.config.js文件复制到Project B的文件中。

暂无
暂无

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

相关问题 没有 package.json 的 Node.js 项目 - Node.js project with no package.json package.json的构建脚本 - Build script of package.json 有没有办法在package.json中自动构建依赖项? - Is there a way to automatically build a dependency in package.json? 部署到 Heroku:如何在 NextJS 项目的 package.json 文件中更改构建脚本的文件路径 - Deploying to Heroku: How to change filepath for build script in package.json file for NextJS project 如果 package.json 文件中的版本未更改/升级,如何限制构建反应项目 - How to restrict to make a build of react project if version is not changed/upgraded in package.json file JS:nvmrc 与 package.json 引擎? - JS: nvmrc vs package.json engines? 创建 React App(npm run build) 生成包含不需要的 package.json 内容的 chunk.js 文件 - Create React App(npm run build) generates chunk.js file with unwanted content of package.json 如何仅使用 index.js 和 package.json 和节点模块文件启动 nodejs 项目 - How to start a nodejs project with only index.js and package.json and node modules files 如果第二个项目的package.json不在项目的根目录中,如何在package.json中添加github依赖关系 - How to add github dependency in package.json if package.json of second project not in the root of the project vue.js: `vue create` 在项目文件夹中只创建 3 个文件(package.json、package-lock.json、README.md) - vue.js: `vue create` creates only 3 files in project folder (package.json, package-lock.json, README.md)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM