Skip to content

Commit

Permalink
Rely on local Terser for minify, disable conditionals
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Bullington committed Sep 5, 2019
1 parent df96942 commit bd41b07
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 35 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ packages

# Include when developing application packages.
pubspec.lock

node_modules
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@ main() {

Credit goes to the Dart team for creating Dart, dart2js, and the original
preamble which this is derived from.

## Contributing

The only file that we directly modify is `preamble.js`. After that, to reflect your changes in
both `preamble.dart` and `preamble.min.js`, run `npm install && npm run minify`
2 changes: 1 addition & 1 deletion lib/preamble.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library node_preamble;

final _minified = r"""var dartNodePreambleSelf="undefined"!=typeof global?global:window,self=Object.create(dartNodePreambleSelf);if(self.scheduleImmediate=self.setImmediate?function(e){dartNodePreambleSelf.setImmediate(e)}:function(e){setTimeout(e,0)},self.require=require,self.exports=exports,"undefined"!=typeof process&&(self.process=process),"undefined"!=typeof __dirname&&(self.__dirname=__dirname),"undefined"!=typeof __filename&&(self.__filename=__filename),!dartNodePreambleSelf.window){var url="undefined"!=typeof __non_webpack_require__?__non_webpack_require__("url"):require("url");self.location={get href(){return url.pathToFileURL?url.pathToFileURL(process.cwd()).href+"/":"file://"+(e=process.cwd(),"win32"!=process.platform?e:"/"+e.replace(/\\/g,"/"))+"/";var e}},function(){function e(){try{throw new Error}catch(t){var e=t.stack,r=new RegExp("^ *at [^(]*\\((.*):[0-9]*:[0-9]*\\)$","mg"),l=null;do{var n=r.exec(e);null!=n&&(l=n)}while(null!=n);return l[1]}}var r=null;self.document={get currentScript(){return null==r&&(r={src:e()}),r}}}(),self.dartDeferredLibraryLoader=function(e,r,l){try{load(e),r()}catch(e){l(e)}}}""";
final _minified = r"""var dartNodePreambleSelf="undefined"!=typeof global?global:window,self=Object.create(dartNodePreambleSelf);if(self.scheduleImmediate=self.setImmediate?function(e){dartNodePreambleSelf.setImmediate(e)}:function(e){setTimeout(e,0)},self.require=require,self.exports=exports,"undefined"!=typeof process)self.process=process;if("undefined"!=typeof __dirname)self.__dirname=__dirname;if("undefined"!=typeof __filename)self.__filename=__filename;if(!dartNodePreambleSelf.window){var url="undefined"!=typeof __non_webpack_require__?__non_webpack_require__("url"):require(1?"url":null);self.location={get href(){if(url.pathToFileURL)return url.pathToFileURL(process.cwd()).href+"/";else return"file://"+function(){var e=process.cwd();if("win32"!=process.platform)return e;else return"/"+e.replace(/\\/g,"/")}()+"/"}},function(){function e(){try{throw new Error}catch(t){var e=t.stack,r=new RegExp("^ *at [^(]*\\((.*):[0-9]*:[0-9]*\\)$","mg"),l=null;do{var n=r.exec(e);if(null!=n)l=n}while(null!=n);return l[1]}}var r=null;self.document={get currentScript(){if(null==r)r={src:e()};return r}}}(),self.dartDeferredLibraryLoader=function(e,r,l){try{load(e),r()}catch(e){l(e)}}}""";

final _normal = r"""
// make sure to keep this as 'var'
Expand Down
2 changes: 1 addition & 1 deletion lib/preamble.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"dependencies": {
"terser": "^4.2.1"
},
"scripts": {
"minify": "node tool/minify.js"
}
}
33 changes: 0 additions & 33 deletions tool/minify.dart

This file was deleted.

43 changes: 43 additions & 0 deletions tool/minify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const fs = require("fs");
const path = require("path");

const Terser = require("terser");

const LIB_DIR = path.join(__dirname, "..", "lib");

const PATH = path.join(LIB_DIR, "preamble.js");

const MIN_PATH = path.join(LIB_DIR, "preamble.min.js");
const DART_PATH = path.join(LIB_DIR, "preamble.dart");

const preamble = fs.readFileSync(PATH).toString();

const { code: minified, error } = Terser.minify(preamble, {
// Needed for Webpack require override.
compress: {
conditionals: false
}
});

if (error) {
throw error;
}

fs.writeFileSync(MIN_PATH, minified);

fs.writeFileSync(DART_PATH, `library node_preamble;
final _minified = r\"""${minified}\""";
final _normal = r\"""
${preamble}\""";
/// Returns the text of the preamble.
///
/// If [minified] is true, returns the minified version rather than the
/// human-readable version.
String getPreamble({bool minified: false, List<String> additionalGlobals: const []}) =>
(minified ? _minified : _normal) +
(additionalGlobals == null ? "" :
additionalGlobals.map((global) => "self.\$global=\$global;").join());
`);
35 changes: 35 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


buffer-from@^1.0.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.1.tgz#32713bc028f75c02fdb710d7c7bcec1f2c6070ef"
integrity sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==

commander@^2.20.0:
version "2.20.0"
resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.0.tgz#d58bb2b5c1ee8f87b0d340027e9e94e222c5a422"
integrity sha512-7j2y+40w61zy6YC2iRNpUe/NwhNyoXrYpHMrSunaMG64nRnaf96zO/KMQR4OyN/UnE5KLyEBnKHd4aG3rskjpQ==

source-map-support@~0.5.12:
version "0.5.13"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.13.tgz#31b24a9c2e73c2de85066c0feb7d44767ed52932"
integrity sha512-SHSKFHadjVA5oR4PPqhtAVdcBWwRYVd6g6cAXnIbRiIwc2EhPrTuKUBdSLvlEKyIP3GCf89fltvcZiP9MMFA1w==
dependencies:
buffer-from "^1.0.0"
source-map "^0.6.0"

source-map@^0.6.0, source-map@~0.6.1:
version "0.6.1"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==

terser@^4.2.1:
version "4.2.1"
resolved "https://registry.yarnpkg.com/terser/-/terser-4.2.1.tgz#1052cfe17576c66e7bc70fcc7119f22b155bdac1"
integrity sha512-cGbc5utAcX4a9+2GGVX4DsenG6v0x3glnDi5hx8816X1McEAwPlPgRtXPJzSBsbpILxZ8MQMT0KvArLuE0HP5A==
dependencies:
commander "^2.20.0"
source-map "~0.6.1"
source-map-support "~0.5.12"

0 comments on commit bd41b07

Please sign in to comment.