1

Is it possible to take code from an external JS file, then paste it inline into script tags (in index.html), once the application is built?

For example, both files below are intended to be identical but I'd like the JS to be implemented externally in the src/dir and inline within the build/dir:

src/index.html

<head>
  <script src="long/minified/code.js"></script>
</head>

build/index.html

<head>
  <script>
    // long... minified code to be added inline here
  </script>
</head>

long/minified/code.js

(function(){var this,isSomeLong='minifiedCode';})();

Gruntfile.js

grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
        dist: {
            src: [
                'long/minified/code.js',

                  ],
            dest: 'build/index.html',
         }
});

It's possible I'm completely off and need something like grunt-include-source

1 Answer 1

3

You can use grunt-processhtml to easily inline your scrips:

HTML

<head>
<!-- build:js inline -->
<script src="long/minified/code.js"></script>
<!-- /build -->
</head>

Gruntfile task:

grunt.initConfig({
  processhtml: {
    dist: {
      files: {
        'build/index.html': ['src/index.html']
      }
    }
  }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.