From bd41b07a4e427a80052fba87cd40f6fe048f47bc Mon Sep 17 00:00:00 2001 From: Michael Bullington Date: Wed, 4 Sep 2019 22:54:37 -0400 Subject: [PATCH] Rely on local Terser for minify, disable conditionals --- .gitignore | 2 ++ README.md | 5 +++++ lib/preamble.dart | 2 +- lib/preamble.min.js | 2 +- package.json | 8 ++++++++ tool/minify.dart | 33 --------------------------------- tool/minify.js | 43 +++++++++++++++++++++++++++++++++++++++++++ yarn.lock | 35 +++++++++++++++++++++++++++++++++++ 8 files changed, 95 insertions(+), 35 deletions(-) create mode 100644 package.json delete mode 100644 tool/minify.dart create mode 100644 tool/minify.js create mode 100644 yarn.lock diff --git a/.gitignore b/.gitignore index ab3cb76..d0a80b8 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,5 @@ packages # Include when developing application packages. pubspec.lock + +node_modules diff --git a/README.md b/README.md index 54d84f3..380ed2c 100644 --- a/README.md +++ b/README.md @@ -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` \ No newline at end of file diff --git a/lib/preamble.dart b/lib/preamble.dart index 5be025c..ab859f6 100644 --- a/lib/preamble.dart +++ b/lib/preamble.dart @@ -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' diff --git a/lib/preamble.min.js b/lib/preamble.min.js index 0f97665..a888a8c 100644 --- a/lib/preamble.min.js +++ b/lib/preamble.min.js @@ -1 +1 @@ -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)}}} \ No newline at end of file +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)}}} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..2807441 --- /dev/null +++ b/package.json @@ -0,0 +1,8 @@ +{ + "dependencies": { + "terser": "^4.2.1" + }, + "scripts": { + "minify": "node tool/minify.js" + } +} diff --git a/tool/minify.dart b/tool/minify.dart deleted file mode 100644 index 3de1da9..0000000 --- a/tool/minify.dart +++ /dev/null @@ -1,33 +0,0 @@ -import 'dart:io'; -import 'dart:isolate'; - -import 'package:http/http.dart' as http; - -main() async { - var packageUrl = Uri.parse('package:node_preamble/'); - var lib = (await Isolate.resolvePackageUri(packageUrl)).toFilePath(); - - var preamble = new File('$lib/preamble.js').readAsStringSync(); - - var response = await http.post("https://javascript-minifier.com/raw", - body: {"input": preamble}); - new File("$lib/preamble.min.js").writeAsStringSync(response.body); - - new File("$lib/preamble.dart").writeAsStringSync(""" -library node_preamble; - -final _minified = r\"""${response.body}\"""; - -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 additionalGlobals: const []}) => - (minified ? _minified : _normal) + - (additionalGlobals == null ? "" : - additionalGlobals.map((global) => "self.\$global=\$global;").join()); -"""); -} diff --git a/tool/minify.js b/tool/minify.js new file mode 100644 index 0000000..dd91596 --- /dev/null +++ b/tool/minify.js @@ -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 additionalGlobals: const []}) => + (minified ? _minified : _normal) + + (additionalGlobals == null ? "" : + additionalGlobals.map((global) => "self.\$global=\$global;").join()); +`); \ No newline at end of file diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 0000000..fdca61a --- /dev/null +++ b/yarn.lock @@ -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"