Deduplicate m.route and m.redraw logic (#2453)

- Remove appropriate route change subcriptions when a root is removed
  via `m.mount(root, null)`.
- Don't pollute `onpopstate` and friends - use standard event listeners
  instead.
- Simplify and streamline subscriptions, in preparation of adding a
  `remove` parameter to `m.mount`.
- Change the redraw internals to redraw immediately, with ability to
  cancel via returning a sentinel.
- Change `"bleeding-edge"` for `m.version` in `next` to instead just be
  the latest `m.version`. (If you're using `next`, you should know what
  you're in for.)
- Update tests to be aware of these changes. (Some were failing for
  subtle reasons.)
- Drive-by: remove some uses of `string.charAt(n)` and use `string[n]`
  instead.
This commit is contained in:
Isiah Meadows 2019-07-05 18:52:06 -04:00 committed by GitHub
parent 6c562d2b9b
commit 90bcff0fa7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 397 additions and 192 deletions

View file

@ -15,6 +15,7 @@ function parse(file) {
try {return JSON.parse(json)} catch (e) {throw new Error("invalid JSON: " + json)}
}
var pkg = require("../package.json")
var error
module.exports = function (input) {
var modules = {}
@ -23,6 +24,10 @@ module.exports = function (input) {
var include = /(?:((?:var|let|const|,|)[\t ]*)([\w_$\.\[\]"'`]+)(\s*=\s*))?require\(([^\)]+)\)(\s*[`\.\(\[])?/gm
var uuid = 0
var process = function(filepath, data) {
// HACK: inline Mithril's `package.json` keys without reading the whole file.
data = data.replace(/require\((['"])\.\/package\.json\1\)\.(\w+)/, function (match, quote, key) {
return JSON.stringify(pkg[key])
})
data.replace(declaration, function(match, binding) {bindings[binding] = 0})
return data.replace(include, function(match, def, variable, eq, dep, rest) {
@ -106,13 +111,10 @@ module.exports = function (input) {
+ (rest ? "\n" + def + variable + eq + "_" + uuid : "") // if `rest` is truthy, it means the expression is fluent or higher-order (e.g. require(path).foo or require(path)(foo)
}
var versionTag = "bleeding-edge"
var packageFile = __dirname + "/../package.json"
var code = process(path.resolve(input), read(input))
.replace(/^\s*((?:var|let|const|)[\t ]*)([\w_$\.]+)(\s*=\s*)(\2)(?=[\s]+(\w)|;|$)/gm, "") // remove assignments to self
.replace(/;+(\r|\n|$)/g, ";$1") // remove redundant semicolons
.replace(/(\r|\n)+/g, "\n").replace(/(\r|\n)$/, "") // remove multiline breaks
.replace(versionTag, isFile(packageFile) ? parse(packageFile).version : versionTag) // set version
code = ";(function() {\n" + code + "\n}());"
//try {new Function(code); console.log("build completed at " + new Date())} catch (e) {}

View file

@ -4,6 +4,7 @@ var o = require("../../ospec/ospec")
var bundle = require("../bundle")
var fs = require("fs")
var pkg = require("../../package.json")
var ns = "./"
function write(filepath, data) {
@ -319,4 +320,11 @@ o.spec("bundler", function() {
remove("a.js")
remove("b.js")
})
o("reads package.json keys", function() {
write("a.js", 'var b = require("./package.json").version')
o(bundle(ns + "a.js")).equals(";(function() {\nvar b = " + JSON.stringify(pkg.version) + "\n}());")
remove("a.js")
})
})