简体   繁体   中英

How to run my node script as one of the ember's build tasks?

I am working in an ember application. From what I understood, it builds the application using Broccoli. I have a requirement where I need to process some files in the application by running a node script before the building process starts. Now I am running the node script separately and then I start the ember server. What is the right way to achieve it? Can I make it as one of the tasks during ember build process? Where should I maintain the node file in the directory?

Well one solution would be to leverage an in-repo addon since the addon hooks provide alot of extra points for customization than I'm aware than ember-cli-build.js does (as far as I'm aware).

If you want to go beyond the built in customizations or want/need more advanced control in general, the following are some of the hooks (keys) available for your addon Object in the index.js file. All hooks expect a function as the value.

includedCommands: function() {},
blueprintsPath: // return path as String
preBuild:
postBuild:
treeFor:
contentFor:
included:
postprocessTree:
serverMiddleware:
lintTree:

In your case, preBuild sounds like the ticket:

This hook is called before a build takes place.

You can require() whatever files you need to from index.js

I would recommend an in-repo addon that implements preBuild or postBuild Ember CLI Addon hooks. Addon hooks are badly documented but there are some usage examples by other addons. Eg ember-cli-deploy-build-plus uses postBuild hook to remove some files from build output.

An more advanced option would be implementing a broccoli plugin and using that one in a treeFor* hook. This makes especially sense if your custom script needs to add / remove files from the build. ember-cli-addon-docs is a great example for that usage.

A simpler solution may be to call a function from your build script in ember-cli-build.js somewhere before return app.toTree();

let my_build_script = require('./lib/my-build-script.js');

await my_build_script();

return app.toTree();

Some disadvantages to this approach include:

  1. That it will not be run as one of many parallel processes if that is possible on your machine.
  2. It will not be run asynchronously with the rest of the build, instead you will have to wait until it is done to start building.

You will likely have to modify your build script to return a function you can call and have it return a promise when it is complete.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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