Author: Heart Leaf Time: 20:08 on January 24, 2019

specifications

When we develop the code, we don’t write it all in one file, only when we package it together, and sometimes, in addition to merging the code, we may need a more flexible merging scheme.

The plug-in inserts fragments into a code file at a specified location.

How to use

First, you need to install the Node package:

npm install grunt-plug-insert --save-dev

Once installed, you can use this plugin by adding the following code to gruntfile.js:

grunt.loadNpmTasks('grunt-plug-insert');

As with other Grunt plugins, let’s focus on how to configure tasks.

grunt.initConfig({
  insert: {
    options: {
      banner: "",
      link: "\n",
      // Place of segmentation
      separator: '@CODE inserts compiled test here',
      // Insert the target file
      target: 'test/fixtures/test'
    },
    files: {
      // Target and fragmentation files
      'tmp/test': ['test/fixtures/insert1', 'test/fixtures/insert2']
    },
  },
});

The above is an example of configuration. Like other plugins, you can also configure the task name, etc. Without going into details here, let’s discuss the meaning of a few configuration options.

  • Separator: Configure the slot. That is, we will replace the string in the “object file” with a series of “fragment files”, which will be saved in the “packaged object file”.
  • Options.target: “target file”
  • Files: We can configure multiple key-value pairs here, the key is a string, which is the “packaged object file”, and the value is an array, which is the “fragmented file”.

As you can see from the above configuration, we can configure more than one merge task at a time, but only one is configured above.

Illustrate to example

For better understanding, let’s illustrate the above example:

Measure /fixtures/test replaced the string “@code inserts compiled test here” with test/fixtures/insert1 and test/fixtures/insert2, Save the merged results in the file TMP /test.