From 1434ba313fb5cc665f432f928be34c1048c4a798 Mon Sep 17 00:00:00 2001 From: Isiah Meadows Date: Tue, 9 Jul 2019 19:26:00 -0400 Subject: [PATCH] Fix `m.version` to not depend on `package.json` (#2464) * Fix `m.version` to not depend on `package.json` Created an accidental breaking change. * Update changelog * Fix a failing test --- .npmignore | 1 + bundler/bundle.js | 4 ---- bundler/tests/test-bundler.js | 7 ------- docs/change-log.md | 2 -- index.js | 2 +- package.json | 2 +- scripts/update-version.js | 10 ++++++++++ 7 files changed, 13 insertions(+), 15 deletions(-) create mode 100644 scripts/update-version.js diff --git a/.npmignore b/.npmignore index d81ad888..bde01f50 100644 --- a/.npmignore +++ b/.npmignore @@ -7,3 +7,4 @@ .travis.yml CONTRIBUTING.md yarn.lock +scripts diff --git a/bundler/bundle.js b/bundler/bundle.js index 58ada442..6af16f87 100644 --- a/bundler/bundle.js +++ b/bundler/bundle.js @@ -24,10 +24,6 @@ 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) { diff --git a/bundler/tests/test-bundler.js b/bundler/tests/test-bundler.js index 9c14ba82..2d447e0f 100644 --- a/bundler/tests/test-bundler.js +++ b/bundler/tests/test-bundler.js @@ -320,11 +320,4 @@ 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") - }) }) diff --git a/docs/change-log.md b/docs/change-log.md index cf3e00f9..900be74d 100644 --- a/docs/change-log.md +++ b/docs/change-log.md @@ -86,8 +86,6 @@ - Previously, this was possible to do in `m.route.set`, `m.request`, and `m.jsonp`, but it was wholly untested for and also undocumented. - API: `m.buildPathname` and `m.parsePathname` added. ([#2361](https://github.com/MithrilJS/mithril.js/pull/2361)) - route: Use `m.mount(root, null)` to unsubscribe and clean up after a `m.route(root, ...)` call. ([#2453](https://github.com/MithrilJS/mithril.js/pull/2453)) -- version: `m.version` returns the previous version string for what's in `next`. ([#2453](https://github.com/MithrilJS/mithril.js/pull/2453)) - - If you're using `next`, you should hopefully know what you're doing. If you need stability, don't use `next`. (This is also why I'm not labelling it as a breaking change.) - render: new `redraw` parameter exposed any time a child event handler is used ([#2458](https://github.com/MithrilJS/mithril.js/pull/2458) [@isiahmeadows](https://github.com/isiahmeadows)) #### Bug fixes diff --git a/index.js b/index.js index 4fdf3d9e..e308e1e9 100644 --- a/index.js +++ b/index.js @@ -18,7 +18,7 @@ m.parseQueryString = require("./querystring/parse") m.buildQueryString = require("./querystring/build") m.parsePathname = require("./pathname/parse") m.buildPathname = require("./pathname/build") -m.version = require("./package.json").version +m.version = "2.0.0-rc.7" m.vnode = require("./render/vnode") m.PromisePolyfill = require("./promise/polyfill") diff --git a/package.json b/package.json index 35d352d7..d2792860 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "cover": "istanbul cover --print both ospec/bin/ospec", "release": "npm version -m 'v%s'", "preversion": "npm run test", - "version": "npm run build && git add mithril.js mithril.min.js", + "version": "node scripts/update-version && npm run build && git add index.js mithril.js mithril.min.js", "postversion": "git push --follow-tags" }, "devDependencies": { diff --git a/scripts/update-version.js b/scripts/update-version.js new file mode 100644 index 00000000..7de01395 --- /dev/null +++ b/scripts/update-version.js @@ -0,0 +1,10 @@ +"use strict" + +const fs = require("fs") +const version = require("../package.json").version +const index = require.resolve("../index") + +fs.writeFile(index, + fs.readFile(index, "utf-8") + .replace(/(version\s*=\s*)(['"]).*?\2/, `$1$2${version}$2`) +)