<commit_before>// Copyright 2018 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

import 'dart:convert';
import 'dart:io';

import 'package:charcode/charcode.dart';
import 'package:grinder/grinder.dart';
import 'package:meta/meta.dart';
import 'package:node_preamble/preamble.dart' as preamble;
import 'package:path/path.dart' as p;
import 'package:source_span/source_span.dart';

import 'utils.dart';

@Task('Compile to JS in dev mode.')
js() => _js(release: false);

@Task('Compile to JS in release mode.')
jsRelease() => _js(release: true);

/// Compiles Sass to JS.
///
/// If [release] is `true`, this compiles minified with
/// --trust-type-annotations. Otherwise, it compiles unminified with pessimistic
/// type checks.
void _js({@required bool release}) {
  ensureBuild();
  var destination = File('build/sass.dart.js');

  Dart2js.compile(File('bin/sass.dart'), outFile: destination, extraArgs: [
    '--categories=Server',
    '-Dnode=true',
    '-Dversion=$version',
    '-Ddart-version=$dartVersion',
    // We use O4 because:
    //
    // * We don't care about the string representation of types.
    // * We expect our test coverage to ensure that nothing throws subtypes of
    //   Error.
    // * We thoroughly test edge cases in user input.
    if (release) ...["-O4", "--fast-startup"]
  ]);
  var text = destination.readAsStringSync();

  if (release) {
    // We don't ship the source map, so remove the source map comment.
    text =
        text.replaceFirst(RegExp(r"\n*//# sourceMappingURL=[^\n]+\n*$"), "\n");
  }

  destination.writeAsStringSync("""
${preamble.getPreamble()}
self.fs = require("fs");
self.chokidar = require("chokidar");
$text""");
}

@Task('Build a pure-JS dev-mode npm package.')
@Depends(js)
npmPackage() => _npm(release: false);<commit_msg>Replace dynamic require()s in dependencies with static alternatives<commit_after>// Copyright 2018 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

import 'dart:convert';
import 'dart:io';

import 'package:charcode/charcode.dart';
import 'package:grinder/grinder.dart';
import 'package:meta/meta.dart';
import 'package:node_preamble/preamble.dart' as preamble;
import 'package:path/path.dart' as p;
import 'package:source_span/source_span.dart';

import 'utils.dart';

@Task('Compile to JS in dev mode.')
js() => _js(release: false);

@Task('Compile to JS in release mode.')
jsRelease() => _js(release: true);

/// Compiles Sass to JS.
///
/// If [release] is `true`, this compiles minified with
/// --trust-type-annotations. Otherwise, it compiles unminified with pessimistic
/// type checks.
void _js({@required bool release}) {
  ensureBuild();
  var destination = File('build/sass.dart.js');

  Dart2js.compile(File('bin/sass.dart'), outFile: destination, extraArgs: [
    '--categories=Server',
    '-Dnode=true',
    '-Dversion=$version',
    '-Ddart-version=$dartVersion',
    // We use O4 because:
    //
    // * We don't care about the string representation of types.
    // * We expect our test coverage to ensure that nothing throws subtypes of
    //   Error.
    // * We thoroughly test edge cases in user input.
    if (release) ...["-O4", "--fast-startup"]
  ]);
  var text = destination
      .readAsStringSync()
      // Some dependencies dynamically invoke `require()`, which makes Webpack
      // complain. We replace those with direct references to the modules, which
      // we load explicitly after the preamble.
      .replaceAllMapped(RegExp(r'self\.require\("([a-zA-Z0-9_-]+)"\)'),
          (match) => "self.${match[1]}");

  if (release) {
    // We don't ship the source map, so remove the source map comment.
    text =
        text.replaceFirst(RegExp(r"\n*//# sourceMappingURL=[^\n]+\n*$"), "\n");
  }

  // Reassigning require() makes Webpack complain.
  var preambleText =
      preamble.getPreamble().replaceFirst("self.require = require;\n", "");

  destination.writeAsStringSync("""
$preambleText
self.fs = require("fs");
self.chokidar = require("chokidar");
self.readline = require("readline");
$text""");
}

@Task('Build a pure-JS dev-mode npm package.')
@Depends(js)
npmPackage() => _npm(release: false);
{'fraction_non_alphanumeric': 0.15901789811840294, 'fraction_numerical': 0.003900871959614502, 'mean_word_length': 3.6620320855614974, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1636644', 'n_tokens_mistral': 1431, 'n_tokens_neox': 1391, 'n_words': 406}