2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
2014-05-05 18:04:09 -04:00
|
|
|
require 'execjs'
|
2016-05-19 08:25:08 -04:00
|
|
|
require 'mini_racer'
|
2014-05-05 18:04:09 -04:00
|
|
|
|
2020-03-11 09:43:55 -04:00
|
|
|
class DiscourseJsProcessor
|
2014-05-05 18:04:09 -04:00
|
|
|
|
2020-04-13 15:05:46 -04:00
|
|
|
def self.plugin_transpile_paths
|
|
|
|
@@plugin_transpile_paths ||= Set.new
|
|
|
|
end
|
|
|
|
|
2020-03-11 09:43:55 -04:00
|
|
|
def self.call(input)
|
|
|
|
root_path = input[:load_path] || ''
|
|
|
|
logical_path = (input[:filename] || '').sub(root_path, '').gsub(/\.(js|es6).*$/, '').sub(/^\//, '')
|
|
|
|
data = input[:data]
|
2014-05-05 18:04:09 -04:00
|
|
|
|
2020-03-11 09:43:55 -04:00
|
|
|
if should_transpile?(input[:filename])
|
|
|
|
data = transpile(data, root_path, logical_path)
|
|
|
|
end
|
2017-04-17 10:11:51 -04:00
|
|
|
|
2020-03-11 09:43:55 -04:00
|
|
|
# add sourceURL until we can do proper source maps
|
|
|
|
unless Rails.env.production?
|
|
|
|
data = "eval(#{data.inspect} + \"\\n//# sourceURL=#{logical_path}\");\n"
|
2017-04-17 10:11:51 -04:00
|
|
|
end
|
|
|
|
|
2020-03-11 09:43:55 -04:00
|
|
|
{ data: data }
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.transpile(data, root_path, logical_path)
|
|
|
|
transpiler = Transpiler.new(skip_module: skip_module?(data))
|
|
|
|
transpiler.perform(data, root_path, logical_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.should_transpile?(filename)
|
|
|
|
filename ||= ''
|
|
|
|
|
|
|
|
# es6 is always transpiled
|
|
|
|
return true if filename.end_with?(".es6") || filename.end_with?(".es6.erb")
|
|
|
|
|
|
|
|
# For .js check the path...
|
|
|
|
return false unless filename.end_with?(".js") || filename.end_with?(".js.erb")
|
|
|
|
|
|
|
|
relative_path = filename.sub(Rails.root.to_s, '').sub(/^\/*/, '')
|
2020-03-26 12:22:33 -04:00
|
|
|
|
|
|
|
js_root = "app/assets/javascripts"
|
|
|
|
test_root = "test/javascripts"
|
|
|
|
|
|
|
|
return false if relative_path.start_with?("#{js_root}/locales/")
|
|
|
|
return false if relative_path.start_with?("#{js_root}/plugins/")
|
|
|
|
|
|
|
|
return true if %w(
|
2020-05-29 14:37:02 -04:00
|
|
|
start-discourse
|
2020-03-26 12:22:33 -04:00
|
|
|
wizard-start
|
|
|
|
onpopstate-handler
|
|
|
|
google-tag-manager
|
2020-03-26 13:12:17 -04:00
|
|
|
google-universal-analytics
|
|
|
|
activate-account
|
2020-03-26 13:21:04 -04:00
|
|
|
auto-redirect
|
2020-03-26 12:22:33 -04:00
|
|
|
embed-application
|
2020-05-01 15:18:41 -04:00
|
|
|
app-boot
|
2020-03-26 12:22:33 -04:00
|
|
|
).any? { |f| relative_path == "#{js_root}/#{f}.js" }
|
|
|
|
|
2020-04-13 15:05:46 -04:00
|
|
|
return true if plugin_transpile_paths.any? { |prefix| relative_path.start_with?(prefix) }
|
|
|
|
|
2020-03-26 12:47:10 -04:00
|
|
|
!!(relative_path =~ /^#{js_root}\/[^\/]+\// ||
|
|
|
|
relative_path =~ /^#{test_root}\/[^\/]+\//)
|
2020-03-11 09:43:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.skip_module?(data)
|
|
|
|
!!(data.present? && data =~ /^\/\/ discourse-skip-module$/)
|
|
|
|
end
|
|
|
|
|
|
|
|
class Transpiler
|
|
|
|
@mutex = Mutex.new
|
|
|
|
@ctx_init = Mutex.new
|
|
|
|
|
|
|
|
def self.mutex
|
|
|
|
@mutex
|
2014-05-05 18:04:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.create_new_context
|
2016-04-21 19:52:12 -04:00
|
|
|
# timeout any eval that takes longer than 15 seconds
|
2020-05-15 00:01:54 -04:00
|
|
|
ctx = MiniRacer::Context.new(timeout: 15000, ensure_gc_after_idle: 2000)
|
2017-07-04 16:34:55 -04:00
|
|
|
ctx.eval("var self = this; #{File.read("#{Rails.root}/vendor/assets/javascripts/babel.js")}")
|
2017-06-29 16:22:19 -04:00
|
|
|
ctx.eval(File.read(Ember::Source.bundled_path_for('ember-template-compiler.js')))
|
2018-12-03 22:48:13 -05:00
|
|
|
ctx.eval("module = {}; exports = {};")
|
2016-05-19 08:25:08 -04:00
|
|
|
ctx.attach("rails.logger.info", proc { |err| Rails.logger.info(err.to_s) })
|
|
|
|
ctx.attach("rails.logger.error", proc { |err| Rails.logger.error(err.to_s) })
|
|
|
|
ctx.eval <<JS
|
|
|
|
console = {
|
|
|
|
prefix: "",
|
|
|
|
log: function(msg){ rails.logger.info(console.prefix + msg); },
|
|
|
|
error: function(msg){ rails.logger.error(console.prefix + msg); }
|
|
|
|
}
|
2017-06-29 16:22:19 -04:00
|
|
|
|
2016-05-19 08:25:08 -04:00
|
|
|
JS
|
2020-03-27 12:06:14 -04:00
|
|
|
source = File.read("#{Rails.root}/lib/javascripts/widget-hbs-compiler.js")
|
2017-06-29 16:22:19 -04:00
|
|
|
js_source = ::JSON.generate(source, quirks_mode: true)
|
DEV: updates js transpiler to use babel 7 (#10627)
Updates our js transpiler code to use Babel 7.11.6
List of changes in this commit:
- Updates plugins, babel plugins all have a new version which doesn't contain -es2015- anymore
- Drops [transform-es2015-classes](https://babeljs.io/docs/en/babel-plugin-transform-classes) this plugin shouldn't be needed now that we don't support IE
- Drops check-es2015-constants, checking constants is now part of babel and the check-constants plugin is deprecated. As a result the behavior slightly changed, and is now wrapping every const call in a readOnlyError function which would throw if assigned a new value. This explains the modified spec.
- Adds [proposal-optional-chaining](https://babeljs.io/docs/en/babel-plugin-proposal-optional-chaining)
```javascript
const obj = {
foo: {
bar: {
baz: 42,
},
},
};
const baz = obj?.foo?.bar?.baz; // 42
```
- Adds [proposal-json-strings](https://babeljs.io/docs/en/babel-plugin-proposal-json-strings)
```javascript
// IN
const ex = "before
after";
// ^ There's a U+2028 char between 'before' and 'after'
// OUT
const ex = "before\u2028after";
// ^ There's a U+2028 char between 'before' and 'after'
```
- Adds [proposal-nullish-coalescing-operator](https://babeljs.io/docs/en/babel-plugin-proposal-nullish-coalescing-operator)
```javascript
var object = {};
var foo = object.foo ?? "default"; // default
```
- Adds [proposal-logical-assignment-operators](https://babeljs.io/docs/en/babel-plugin-proposal-logical-assignment-operators)
```javascript
let a;
let b = 2;
a ||= b; // 2
```
- Adds [proposal-numeric-separator](https://babeljs.io/docs/en/babel-plugin-proposal-numeric-separator)
```javascript
let budget = 1_000_000_000_000;
console.log(budget === 10 ** 12); // true
```
- Adds proposal-object-rest-spread https://babeljs.io/docs/en/babel-plugin-proposal-object-rest-spread
```javascript
let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 };
console.log(x); // 1
console.log(y); // 2
console.log(z); // { a: 3, b: 4 }
```
- Adds proposal-optional-catch-binding https://babeljs.io/docs/en/babel-plugin-proposal-optional-catch-binding
```javascript
try {
} catch {
} finally {
// ensures finally is available in every browsers
}
```
- Adds improved regex support for firefox through (transform-dotall-regex](https://babeljs.io/docs/en/next/babel-plugin-transform-dotall-regex.html) and (proposal-unicode-property-regex](https://babeljs.io/docs/en/babel-plugin-proposal-unicode-property-regex)
- Drops async/generator stuff, the browser we target should allow to use this (excepts iterable async)
2020-09-15 03:26:33 -04:00
|
|
|
js = ctx.eval("Babel.transform(#{js_source}, { ast: false, plugins: ['transform-arrow-functions', 'transform-block-scoped-functions', 'transform-block-scoping', 'transform-computed-properties', 'transform-destructuring', 'transform-duplicate-keys', 'transform-for-of', 'transform-function-name', 'transform-literals', 'transform-object-super', 'transform-parameters', 'transform-shorthand-properties', 'transform-spread', 'transform-sticky-regex', 'transform-template-literals', 'transform-typeof-symbol', 'transform-unicode-regex'] }).code")
|
2017-06-29 16:22:19 -04:00
|
|
|
ctx.eval(js)
|
|
|
|
|
2014-05-05 18:04:09 -04:00
|
|
|
ctx
|
|
|
|
end
|
|
|
|
|
2016-11-01 22:34:20 -04:00
|
|
|
def self.reset_context
|
2017-07-20 00:17:45 -04:00
|
|
|
@ctx&.dispose
|
2016-11-01 22:34:20 -04:00
|
|
|
@ctx = nil
|
|
|
|
end
|
|
|
|
|
2014-05-05 18:04:09 -04:00
|
|
|
def self.v8
|
|
|
|
return @ctx if @ctx
|
|
|
|
|
|
|
|
# ensure we only init one of these
|
|
|
|
@ctx_init.synchronize do
|
|
|
|
return @ctx if @ctx
|
|
|
|
@ctx = create_new_context
|
|
|
|
end
|
|
|
|
|
|
|
|
@ctx
|
|
|
|
end
|
|
|
|
|
2020-03-11 09:43:55 -04:00
|
|
|
def initialize(skip_module: false)
|
|
|
|
@skip_module = skip_module
|
2016-03-18 14:41:27 -04:00
|
|
|
end
|
|
|
|
|
2020-03-11 09:43:55 -04:00
|
|
|
def perform(source, root_path = nil, logical_path = nil)
|
2016-06-14 14:31:51 -04:00
|
|
|
klass = self.class
|
2020-03-11 09:43:55 -04:00
|
|
|
klass.mutex.synchronize do
|
2016-06-14 14:31:51 -04:00
|
|
|
klass.v8.eval("console.prefix = 'BABEL: babel-eval: ';")
|
2017-06-29 16:22:19 -04:00
|
|
|
transpiled = babel_source(
|
|
|
|
source,
|
|
|
|
module_name: module_name(root_path, logical_path),
|
|
|
|
filename: logical_path
|
|
|
|
)
|
2017-07-05 14:14:30 -04:00
|
|
|
@output = klass.v8.eval(transpiled)
|
2016-06-14 14:31:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-05 14:14:30 -04:00
|
|
|
def babel_source(source, opts = nil)
|
|
|
|
opts ||= {}
|
|
|
|
|
2016-03-18 14:41:27 -04:00
|
|
|
js_source = ::JSON.generate(source, quirks_mode: true)
|
2017-07-05 14:14:30 -04:00
|
|
|
|
2020-03-11 09:43:55 -04:00
|
|
|
if opts[:module_name] && !@skip_module
|
2017-06-29 16:22:19 -04:00
|
|
|
filename = opts[:filename] || 'unknown'
|
DEV: updates js transpiler to use babel 7 (#10627)
Updates our js transpiler code to use Babel 7.11.6
List of changes in this commit:
- Updates plugins, babel plugins all have a new version which doesn't contain -es2015- anymore
- Drops [transform-es2015-classes](https://babeljs.io/docs/en/babel-plugin-transform-classes) this plugin shouldn't be needed now that we don't support IE
- Drops check-es2015-constants, checking constants is now part of babel and the check-constants plugin is deprecated. As a result the behavior slightly changed, and is now wrapping every const call in a readOnlyError function which would throw if assigned a new value. This explains the modified spec.
- Adds [proposal-optional-chaining](https://babeljs.io/docs/en/babel-plugin-proposal-optional-chaining)
```javascript
const obj = {
foo: {
bar: {
baz: 42,
},
},
};
const baz = obj?.foo?.bar?.baz; // 42
```
- Adds [proposal-json-strings](https://babeljs.io/docs/en/babel-plugin-proposal-json-strings)
```javascript
// IN
const ex = "before
after";
// ^ There's a U+2028 char between 'before' and 'after'
// OUT
const ex = "before\u2028after";
// ^ There's a U+2028 char between 'before' and 'after'
```
- Adds [proposal-nullish-coalescing-operator](https://babeljs.io/docs/en/babel-plugin-proposal-nullish-coalescing-operator)
```javascript
var object = {};
var foo = object.foo ?? "default"; // default
```
- Adds [proposal-logical-assignment-operators](https://babeljs.io/docs/en/babel-plugin-proposal-logical-assignment-operators)
```javascript
let a;
let b = 2;
a ||= b; // 2
```
- Adds [proposal-numeric-separator](https://babeljs.io/docs/en/babel-plugin-proposal-numeric-separator)
```javascript
let budget = 1_000_000_000_000;
console.log(budget === 10 ** 12); // true
```
- Adds proposal-object-rest-spread https://babeljs.io/docs/en/babel-plugin-proposal-object-rest-spread
```javascript
let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 };
console.log(x); // 1
console.log(y); // 2
console.log(z); // { a: 3, b: 4 }
```
- Adds proposal-optional-catch-binding https://babeljs.io/docs/en/babel-plugin-proposal-optional-catch-binding
```javascript
try {
} catch {
} finally {
// ensures finally is available in every browsers
}
```
- Adds improved regex support for firefox through (transform-dotall-regex](https://babeljs.io/docs/en/next/babel-plugin-transform-dotall-regex.html) and (proposal-unicode-property-regex](https://babeljs.io/docs/en/babel-plugin-proposal-unicode-property-regex)
- Drops async/generator stuff, the browser we target should allow to use this (excepts iterable async)
2020-09-15 03:26:33 -04:00
|
|
|
"Babel.transform(#{js_source}, { moduleId: '#{opts[:module_name]}', filename: '#{filename}', ast: false, presets: ['es2015'], plugins: [['transform-modules-amd', {noInterop: true}], ['proposal-decorators', {legacy: true} ], exports.WidgetHbsCompiler] }).code"
|
2017-07-05 14:14:30 -04:00
|
|
|
else
|
DEV: updates js transpiler to use babel 7 (#10627)
Updates our js transpiler code to use Babel 7.11.6
List of changes in this commit:
- Updates plugins, babel plugins all have a new version which doesn't contain -es2015- anymore
- Drops [transform-es2015-classes](https://babeljs.io/docs/en/babel-plugin-transform-classes) this plugin shouldn't be needed now that we don't support IE
- Drops check-es2015-constants, checking constants is now part of babel and the check-constants plugin is deprecated. As a result the behavior slightly changed, and is now wrapping every const call in a readOnlyError function which would throw if assigned a new value. This explains the modified spec.
- Adds [proposal-optional-chaining](https://babeljs.io/docs/en/babel-plugin-proposal-optional-chaining)
```javascript
const obj = {
foo: {
bar: {
baz: 42,
},
},
};
const baz = obj?.foo?.bar?.baz; // 42
```
- Adds [proposal-json-strings](https://babeljs.io/docs/en/babel-plugin-proposal-json-strings)
```javascript
// IN
const ex = "before
after";
// ^ There's a U+2028 char between 'before' and 'after'
// OUT
const ex = "before\u2028after";
// ^ There's a U+2028 char between 'before' and 'after'
```
- Adds [proposal-nullish-coalescing-operator](https://babeljs.io/docs/en/babel-plugin-proposal-nullish-coalescing-operator)
```javascript
var object = {};
var foo = object.foo ?? "default"; // default
```
- Adds [proposal-logical-assignment-operators](https://babeljs.io/docs/en/babel-plugin-proposal-logical-assignment-operators)
```javascript
let a;
let b = 2;
a ||= b; // 2
```
- Adds [proposal-numeric-separator](https://babeljs.io/docs/en/babel-plugin-proposal-numeric-separator)
```javascript
let budget = 1_000_000_000_000;
console.log(budget === 10 ** 12); // true
```
- Adds proposal-object-rest-spread https://babeljs.io/docs/en/babel-plugin-proposal-object-rest-spread
```javascript
let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 };
console.log(x); // 1
console.log(y); // 2
console.log(z); // { a: 3, b: 4 }
```
- Adds proposal-optional-catch-binding https://babeljs.io/docs/en/babel-plugin-proposal-optional-catch-binding
```javascript
try {
} catch {
} finally {
// ensures finally is available in every browsers
}
```
- Adds improved regex support for firefox through (transform-dotall-regex](https://babeljs.io/docs/en/next/babel-plugin-transform-dotall-regex.html) and (proposal-unicode-property-regex](https://babeljs.io/docs/en/babel-plugin-proposal-unicode-property-regex)
- Drops async/generator stuff, the browser we target should allow to use this (excepts iterable async)
2020-09-15 03:26:33 -04:00
|
|
|
"Babel.transform(#{js_source}, { ast: false, plugins: ['proposal-json-strings', 'proposal-nullish-coalescing-operator', 'proposal-logical-assignment-operators', 'proposal-numeric-separator', 'proposal-optional-catch-binding', 'transform-dotall-regex', 'proposal-unicode-property-regex', 'transform-named-capturing-groups-regex', 'proposal-object-rest-spread', 'proposal-optional-chaining', 'transform-arrow-functions', 'transform-block-scoped-functions', 'transform-block-scoping', 'transform-computed-properties', 'transform-destructuring', 'transform-duplicate-keys', 'transform-for-of', 'transform-function-name', 'transform-literals', 'transform-object-super', 'transform-parameters', 'transform-shorthand-properties', 'transform-spread', 'transform-sticky-regex', 'transform-template-literals', 'transform-typeof-symbol', 'transform-unicode-regex', ['proposal-decorators', {legacy: true}], exports.WidgetHbsCompiler] }).code"
|
2017-07-05 14:14:30 -04:00
|
|
|
end
|
2016-03-18 14:41:27 -04:00
|
|
|
end
|
|
|
|
|
2014-05-05 18:04:09 -04:00
|
|
|
def module_name(root_path, logical_path)
|
2014-05-15 16:31:45 -04:00
|
|
|
path = nil
|
|
|
|
|
2014-05-20 16:54:59 -04:00
|
|
|
root_base = File.basename(Rails.root)
|
2014-05-15 16:31:45 -04:00
|
|
|
# If the resource is a plugin, use the plugin name as a prefix
|
2014-05-20 16:54:59 -04:00
|
|
|
if root_path =~ /(.*\/#{root_base}\/plugins\/[^\/]+)\//
|
2014-05-15 16:31:45 -04:00
|
|
|
plugin_path = "#{Regexp.last_match[1]}/plugin.rb"
|
|
|
|
|
|
|
|
plugin = Discourse.plugins.find { |p| p.path == plugin_path }
|
|
|
|
path = "discourse/plugins/#{plugin.name}/#{logical_path.sub(/javascripts\//, '')}" if plugin
|
2014-05-05 18:04:09 -04:00
|
|
|
end
|
|
|
|
|
2017-06-29 16:22:19 -04:00
|
|
|
# We need to strip the app subdirectory to replicate how ember-cli works.
|
2020-04-29 12:18:21 -04:00
|
|
|
path || logical_path&.gsub('app/', '')&.gsub('addon/', '')
|
2014-05-05 18:04:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|