Skip to content

Instantly share code, notes, and snippets.

@i-rocky
Created July 8, 2022 17:54
Show Gist options
  • Select an option

  • Save i-rocky/0eaa85ddd1d363a97772eb948b67836e to your computer and use it in GitHub Desktop.

Select an option

Save i-rocky/0eaa85ddd1d363a97772eb948b67836e to your computer and use it in GitHub Desktop.
tasks.fix_generator = function () {
const config = grunt.config('fix_generator');
const files = grunt.file.expand({}, config.dist.src);
files.forEach((file) => {
const contents = fs.readFileSync(file).toString('utf-8');
const match = contents.match(/\ndefine\(\s*(\[|function)/);
if (match) {
const prepended = contents.substring(0, match.index);
let body = contents.substring(match.index);
let append = '';
const sourcemap = body.match(/\/\/#\ssourceMappingURL/);
if (sourcemap) {
append = body.substring(sourcemap.index);
body = body.substring(0, sourcemap.index);
}
fs.writeFileSync(file, `${body}\n${prepended}\n${append}`.trim());
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment