diff --git a/packages/lwc-compiler/src/__tests__/fixtures/comments/comments.css b/packages/lwc-compiler/src/__tests__/fixtures/comments/comments.css new file mode 100644 index 0000000000..38a0fe05f7 --- /dev/null +++ b/packages/lwc-compiler/src/__tests__/fixtures/comments/comments.css @@ -0,0 +1,9 @@ +:host { + color: blue; +} + +/* css comment */ + +x-comments { + color: green; +} diff --git a/packages/lwc-compiler/src/__tests__/fixtures/comments/comments.html b/packages/lwc-compiler/src/__tests__/fixtures/comments/comments.html new file mode 100644 index 0000000000..82da3e9e3d --- /dev/null +++ b/packages/lwc-compiler/src/__tests__/fixtures/comments/comments.html @@ -0,0 +1,4 @@ + diff --git a/packages/lwc-compiler/src/__tests__/fixtures/comments/comments.js b/packages/lwc-compiler/src/__tests__/fixtures/comments/comments.js new file mode 100644 index 0000000000..9963eb8e54 --- /dev/null +++ b/packages/lwc-compiler/src/__tests__/fixtures/comments/comments.js @@ -0,0 +1,7 @@ +import { LightningElement, track } from 'lwc'; + +export default class Comments extends LightningElement { + // javascript comment + @track + myname = 'default value'; +} diff --git a/packages/lwc-compiler/src/__tests__/fixtures/expected-all-modes.js b/packages/lwc-compiler/src/__tests__/fixtures/expected-all-modes.js deleted file mode 100755 index e69de29bb2..0000000000 diff --git a/packages/lwc-compiler/src/__tests__/fixtures/expected-compat-mode.js b/packages/lwc-compiler/src/__tests__/fixtures/expected-compat-mode.js deleted file mode 100755 index aa793bf123..0000000000 --- a/packages/lwc-compiler/src/__tests__/fixtures/expected-compat-mode.js +++ /dev/null @@ -1,55 +0,0 @@ -define('x-class_and_template', ['proxy-compat/getKey', 'proxy-compat/setKey', 'proxy-compat/callKey1', 'babel-compat/helpers/classCallCheck', 'babel-compat/helpers/createClass', 'babel-compat/helpers/possibleConstructorReturn', 'babel-compat/helpers/inherits', 'lwc'], function (__getKey, __setKey, __callKey1, _classCallCheck, _createClass, _possibleConstructorReturn, _inherits, lwc) { - -var style = undefined; - -function tmpl($api, $cmp, $slotset, $ctx) { - var api_element = __getKey($api, "h"); - - return [api_element("section", { - key: 1 - }, [])]; -} - -if (style) { - __setKey(tmpl, "token", 'x-class_and_template_class_and_template'); - - var style$1 = __callKey1(document, "createElement", 'style'); - __setKey(style$1, "type", 'text/css'); - __setKey(__getKey(style$1, "dataset"), "token", 'x-class_and_template_class_and_template'); - __setKey(style$1, "textContent", style('x-class_and_template_class_and_template')); - __callKey1(__getKey(document, "head"), "appendChild", style$1); -} - -var Test = 1; - -var ClassAndTemplate = -/*#__PURE__*/ -function (_Element) { - _inherits(ClassAndTemplate, _Element); - - function ClassAndTemplate() { - var _this; - _classCallCheck(this, ClassAndTemplate); - - _this = _possibleConstructorReturn(this, __callKey1(__getKey(ClassAndTemplate, "__proto__") || Object.getPrototypeOf(ClassAndTemplate), "call", this)); - - __setKey(_this, "t", Test); - __setKey(_this, "counter", 0); - return _this; - } - - _createClass(ClassAndTemplate, [{ - key: "render", - value: function render() { - return tmpl; - } - }]); - - return ClassAndTemplate; -}(lwc.LightningElement); - -__setKey(ClassAndTemplate, "style", __getKey(tmpl, "style")); - -return ClassAndTemplate; - -}); diff --git a/packages/lwc-compiler/src/__tests__/fixtures/expected-compile-individual-resources.js b/packages/lwc-compiler/src/__tests__/fixtures/expected-compile-individual-resources.js deleted file mode 100755 index 8e9f94819e..0000000000 --- a/packages/lwc-compiler/src/__tests__/fixtures/expected-compile-individual-resources.js +++ /dev/null @@ -1,7 +0,0 @@ -export default function tmpl($api, $cmp, $slotset, $ctx) { - const { - h: api_element - } = $api; - - return [api_element("section", {}, [])]; -} diff --git a/packages/lwc-compiler/src/__tests__/fixtures/expected-compile-with-no-options-and-default-namespace.js b/packages/lwc-compiler/src/__tests__/fixtures/expected-compile-with-no-options-and-default-namespace.js deleted file mode 100755 index 5165c4e59e..0000000000 --- a/packages/lwc-compiler/src/__tests__/fixtures/expected-compile-with-no-options-and-default-namespace.js +++ /dev/null @@ -1,33 +0,0 @@ -import { LightningElement } from 'lwc'; - -const style = undefined; - -function tmpl($api, $cmp, $slotset, $ctx) { - const { - h: api_element - } = $api; - - return [api_element("section", { - key: 1 - }, [])]; -} - -if (style) { - tmpl.hostToken = 'x-default_default-host'; - tmpl.shadowToken = 'x-default_default'; - - const style$$1 = document.createElement('style'); - style$$1.type = 'text/css'; - style$$1.dataset.token = 'x-default_default'; - style$$1.textContent = style('x-default_default'); - document.head.appendChild(style$$1); -} - -class Default extends LightningElement { - render() { - return tmpl; - } - -} - -export default Default; diff --git a/packages/lwc-compiler/src/__tests__/fixtures/expected-external-dependency.js b/packages/lwc-compiler/src/__tests__/fixtures/expected-external-dependency.js deleted file mode 100755 index 4e96a0a550..0000000000 --- a/packages/lwc-compiler/src/__tests__/fixtures/expected-external-dependency.js +++ /dev/null @@ -1,40 +0,0 @@ -import { LightningElement } from 'lwc'; -import externalDep from 'another-module'; - -const style = undefined; - -function tmpl($api, $cmp, $slotset, $ctx) { - const { - h: api_element - } = $api; - - return [api_element("section", { - key: 1 - }, [])]; -} - -if (style) { - tmpl.hostToken = 'x-external_external-host'; - tmpl.shadowToken = 'x-external_external'; - - const style$$1 = document.createElement('style'); - style$$1.type = 'text/css'; - style$$1.dataset.token = 'x-external_external'; - style$$1.textContent = style('x-external_external'); - document.head.appendChild(style$$1); -} - -class Foo extends LightningElement { - constructor(...args) { - var _temp; - - return _temp = super(...args), this.anotherModule = externalDep, _temp; - } - - render() { - return tmpl; - } - -} - -export default Foo; diff --git a/packages/lwc-compiler/src/__tests__/fixtures/expected-mapping-namespace-from-path.js b/packages/lwc-compiler/src/__tests__/fixtures/expected-mapping-namespace-from-path.js deleted file mode 100755 index d81eace024..0000000000 --- a/packages/lwc-compiler/src/__tests__/fixtures/expected-mapping-namespace-from-path.js +++ /dev/null @@ -1,38 +0,0 @@ -import { LightningElement } from 'lwc'; - -const style = undefined; - -function tmpl($api, $cmp, $slotset, $ctx) { - const { - t: api_text, - h: api_element - } = $api; - - return [api_element("p", { - key: 1 - }, [api_text("CMP1")])]; -} - -if (style) { - tmpl.hostToken = 'x-cmp1_cmp1-host'; - tmpl.shadowToken = 'x-cmp1_cmp1'; - - const style$$1 = document.createElement('style'); - style$$1.type = 'text/css'; - style$$1.dataset.token = 'x-cmp1_cmp1'; - style$$1.textContent = style('x-cmp1_cmp1'); - document.head.appendChild(style$$1); -} - -class Cmp1 extends LightningElement { - constructor() { - super(); - } - - render() { - return tmpl; - } - -} - -export default Cmp1; diff --git a/packages/lwc-compiler/src/__tests__/fixtures/expected-minify-no-comments.js b/packages/lwc-compiler/src/__tests__/fixtures/expected-minify-no-comments.js new file mode 100644 index 0000000000..c14eb94e04 --- /dev/null +++ b/packages/lwc-compiler/src/__tests__/fixtures/expected-minify-no-comments.js @@ -0,0 +1 @@ +define("x-comments",["lwc"],function(a){function b(a){return`[${a}-host]{color:blue}x-comments[${a}],[is="x-comments"][${a}]{color:green}`}function c(a,b){const{d:c,h:d}=a;return[d("h1",{key:1},[c(b.myname)])]}if(b){c.hostToken="x-comments_comments-host",c.shadowToken="x-comments_comments";const a=document.createElement("style");a.type="text/css",a.dataset.token="x-comments_comments",a.textContent=b("x-comments_comments"),document.head.appendChild(a)}class d extends a.LightningElement{constructor(...a){var b;return b=super(...a),this.myname="default value",b}render(){return c}}return d.track={myname:1},d}); diff --git a/packages/lwc-compiler/src/__tests__/fixtures/expected-prod-mode.js b/packages/lwc-compiler/src/__tests__/fixtures/expected-prod-mode.js index 33ce13e4a5..aa8c4c4c1f 100755 --- a/packages/lwc-compiler/src/__tests__/fixtures/expected-prod-mode.js +++ b/packages/lwc-compiler/src/__tests__/fixtures/expected-prod-mode.js @@ -1 +1 @@ -define("x-class_and_template",["lwc"],function(a){function b(a){const{h:b}=a;return[b("section",{key:1},[])]}class c extends a.Element{constructor(){super(),this.t=1,this.counter=0}render(){return b}}return c.style=b.style,c}); +define("x-class_and_template",["lwc"],function(a){function b(a){const{h:b}=a;return[b("section",{key:1},[])]}class c extends a.LightningElement{constructor(){super(),this.t=1,this.counter=0}render(){return b}}return c}); diff --git a/packages/lwc-compiler/src/__tests__/fixtures/expected-prod_compat-mode.js b/packages/lwc-compiler/src/__tests__/fixtures/expected-prod_compat-mode.js index 4539e3ad08..c2145661ab 100755 --- a/packages/lwc-compiler/src/__tests__/fixtures/expected-prod_compat-mode.js +++ b/packages/lwc-compiler/src/__tests__/fixtures/expected-prod_compat-mode.js @@ -1 +1 @@ -define("x-class_and_template",["proxy-compat/getKey","proxy-compat/setKey","proxy-compat/callKey1","babel-compat/helpers/classCallCheck","babel-compat/helpers/createClass","babel-compat/helpers/possibleConstructorReturn","babel-compat/helpers/inherits","lwc"],function(a,b,c,d,e,f,g,h){function i(b){var c=a(b,"h");return[c("section",{key:1},[])]}var j;if(j){b(i,"token","x-class_and_template_class_and_template");var k=c(document,"createElement","style");b(k,"type","text/css"),b(a(k,"dataset"),"token","x-class_and_template_class_and_template"),b(k,"textContent",j("x-class_and_template","x-class_and_template_class_and_template")),c(a(document,"head"),"appendChild",k)}var l=function(h){function j(){var e;return d(this,j),e=f(this,c(a(j,"__proto__")||Object.getPrototypeOf(j),"call",this)),b(e,"t",1),b(e,"counter",0),e}return g(j,h),e(j,[{key:"render",value:function(){return i}}]),j}(h.Element);return b(l,"style",a(i,"style")),l}); +define("x-node_env",["proxy-compat/setKey","proxy-compat/callKey1","proxy-compat/getKey","babel-compat/helpers/objectDestructuringEmpty","proxy-compat/callKey2","babel-compat/helpers/classCallCheck","babel-compat/helpers/createClass","babel-compat/helpers/possibleConstructorReturn","babel-compat/helpers/getPrototypeOf","babel-compat/helpers/inherits","lwc"],function(a,b,c,d,e,f,g,h,i,j,k){function l(a){return d(a),[]}var m;if(m){a(l,"hostToken","x-node_env_node_env-host"),a(l,"shadowToken","x-node_env_node_env");var n=b(document,"createElement","style");a(n,"type","text/css"),a(c(n,"dataset"),"token","x-node_env_node_env"),a(n,"textContent",m("x-node_env_node_env")),b(c(document,"head"),"appendChild",n)}var o=function(a){function d(){return f(this,d),h(this,e(i(d),"apply",this,arguments))}return j(d,a),g(d,[{key:"connectedCallback",value:function(){b(c(this,"root"),"querySelector","insideOfProductionCheck"),b(c(this,"root"),"querySelector","outsideOfProductionCheck")}},{key:"render",value:function(){return l}}]),d}(k.LightningElement);return o}); diff --git a/packages/lwc-compiler/src/__tests__/fixtures/expected-relative-import.js b/packages/lwc-compiler/src/__tests__/fixtures/expected-relative-import.js deleted file mode 100755 index 84f9bdd45d..0000000000 --- a/packages/lwc-compiler/src/__tests__/fixtures/expected-relative-import.js +++ /dev/null @@ -1,49 +0,0 @@ -define('myns-relative_import', ['lwc'], function (lwc) { - -const style = undefined; - -function tmpl($api, $cmp, $slotset, $ctx) { - const { - h: api_element - } = $api; - - return [api_element("section", { - key: 1 - }, [])]; -} - -if (style) { - tmpl.hostToken = 'myns-relative_import_relative-host'; - tmpl.shadowToken = 'myns-relative_import_relative'; - - const style$$1 = document.createElement('style'); - style$$1.type = 'text/css'; - style$$1.dataset.token = 'myns-relative_import_relative_import'; - style$$1.textContent = style('myns-relative_import_relative_import'); - document.head.appendChild(style$$1); -} - -function test() {} - -function sibling() {} - -function inner() { - return sibling(); -} - -class RelativeImport extends lwc.LightningElement { - constructor() { - super(); - this.x = test(); - this.y = inner(); - } - - render() { - return tmpl; - } - -} - -return RelativeImport; - -}); diff --git a/packages/lwc-compiler/src/__tests__/fixtures/expected-sources-namespaced-format.js b/packages/lwc-compiler/src/__tests__/fixtures/expected-sources-namespaced-format.js deleted file mode 100755 index 1844ccb4e1..0000000000 --- a/packages/lwc-compiler/src/__tests__/fixtures/expected-sources-namespaced-format.js +++ /dev/null @@ -1,42 +0,0 @@ -define('x-foo', ['lwc'], function (lwc) { - -const style = undefined; - -function tmpl($api, $cmp, $slotset, $ctx) { - const { - h: api_element - } = $api; - - return [api_element("section", { - key: 1 - }, [])]; -} - -if (style) { - tmpl.hostToken = 'x-foo_foo-host'; - tmpl.shadowToken = 'x-foo_foo'; - - const style$$1 = document.createElement('style'); - style$$1.type = 'text/css'; - style$$1.dataset.token = 'x-foo_foo'; - style$$1.textContent = style('x-foo_foo'); - document.head.appendChild(style$$1); -} - -const Test = 1; -class ClassAndTemplate extends lwc.LightningElement { - constructor() { - super(); - this.t = Test; - this.counter = 0; - } - - render() { - return tmpl; - } - -} - -return ClassAndTemplate; - -}); diff --git a/packages/lwc-compiler/src/__tests__/fixtures/expected-sources-namespaced.js b/packages/lwc-compiler/src/__tests__/fixtures/expected-sources-namespaced.js deleted file mode 100755 index 47eb7204cb..0000000000 --- a/packages/lwc-compiler/src/__tests__/fixtures/expected-sources-namespaced.js +++ /dev/null @@ -1,40 +0,0 @@ -import { LightningElement } from 'lwc'; - -const style = undefined; - -function tmpl($api, $cmp, $slotset, $ctx) { - const { - h: api_element - } = $api; - - return [api_element("section", { - key: 1 - }, [])]; -} - -if (style) { - tmpl.hostToken = 'x-foo_foo-host'; - tmpl.shadowToken = 'x-foo_foo'; - - const style$$1 = document.createElement('style'); - style$$1.type = 'text/css'; - style$$1.dataset.token = 'x-foo_foo'; - style$$1.textContent = style('x-foo_foo'); - document.head.appendChild(style$$1); -} - -const Test = 1; -class ClassAndTemplate extends LightningElement { - constructor() { - super(); - this.t = Test; - this.counter = 0; - } - - render() { - return tmpl; - } - -} - -export default ClassAndTemplate; diff --git a/packages/lwc-compiler/src/__tests__/modes.spec.ts b/packages/lwc-compiler/src/__tests__/modes.spec.ts new file mode 100644 index 0000000000..25621e0bc4 --- /dev/null +++ b/packages/lwc-compiler/src/__tests__/modes.spec.ts @@ -0,0 +1,124 @@ +import { compile } from "../index"; +import { pretify, readFixture } from "./utils"; + +const VALID_CONFIG = { + outputConfig: { + env: {}, + minify: false, + compat: false, + format: "amd" + }, + name: "node_env", + namespace: "x", + files: { + "node_env.js": readFixture( + "./node_env/node_env.js" + ), + "node_env.html": readFixture( + "./node_env/node_env.html" + ) + }, +}; + +describe('test shape of the bundle in different modes and environments', () => { + + test('test bundle with NODE_ENV set to dev', async () => { + const customConfig = { + outputConfig: { + format: 'es', + env: { + NODE_ENV: 'development', + }, + }, + }; + const config = Object.assign({}, VALID_CONFIG, customConfig); + const { result: { code }} = await compile(config); + expect(pretify(code)).toBe(pretify(readFixture('expected-node-env-dev.js'))); + }); + + test('test bundle with NODE_ENV set to production', async () => { + const customConfig = { + outputConfig: { + format: 'es', + env: { + NODE_ENV: 'production', + }, + }, + }; + const config = Object.assign({}, VALID_CONFIG, customConfig); + const { result: { code }} = await compile(config); + expect(pretify(code)).toBe(pretify(readFixture('expected-node-env-prod.js'))); + }); + + test('test bundle for production in compat', async () => { + const customConfig = { + outputConfig: { + compat: true, + minify: true, + } + }; + const config = Object.assign({}, VALID_CONFIG, customConfig); + const { result: { code }} = await compile(config); + expect(pretify(code)).toBe(pretify(readFixture('expected-prod_compat-mode.js'))); + }); + + test('test bundle in dev mode', async () => { + const customConfig = { + name: "class_and_template", + files: { + "class_and_template.js": readFixture( + "./class_and_template/class_and_template.js" + ), + "class_and_template.html": readFixture( + "./class_and_template/class_and_template.html" + ), + }, + }; + const config = Object.assign({}, VALID_CONFIG, customConfig); + const { result: {code} } = await compile(config); + expect(pretify(code)).toBe(pretify(readFixture('expected-dev-mode.js'))); + }); + + test('test bundle in prod mode', async () => { + const customConfig = { + name: "class_and_template", + files: { + "class_and_template.js": readFixture( + "./class_and_template/class_and_template.js" + ), + "class_and_template.html": readFixture( + "./class_and_template/class_and_template.html" + ), + }, + outputConfig: { + minify: true, + } + }; + const config = Object.assign({}, VALID_CONFIG, customConfig); + const { result: {code} } = await compile(config); + expect(pretify(code)).toBe(pretify(readFixture('expected-prod-mode.js'))); + }); + + test('test minified bundles to remove comments', async () => { + const minifiedConfig = { + name: "comments", + files: { + "comments.js": readFixture( + "./comments/comments.js" + ), + "comments.html": readFixture( + "./comments/comments.html" + ), + "comments.css": readFixture( + "./comments/comments.css" + ) + }, + outputConfig: { + minify: true + }, + }; + const config = Object.assign({}, VALID_CONFIG, minifiedConfig); + const { result: {code} } = await compile(config); + expect(pretify(code)).toBe(pretify(readFixture('expected-minify-no-comments.js'))); + }); +}); diff --git a/packages/lwc-compiler/src/rollup-plugins/minify.ts b/packages/lwc-compiler/src/rollup-plugins/minify.ts index db98c0e685..4c429d447c 100755 --- a/packages/lwc-compiler/src/rollup-plugins/minify.ts +++ b/packages/lwc-compiler/src/rollup-plugins/minify.ts @@ -3,7 +3,9 @@ import minify from 'babel-preset-minify'; import { BABEL_CONFIG_BASE } from '../babel-plugins'; -export const MINIFY_CONFIG: any = Object.assign({}, BABEL_CONFIG_BASE, { +export const MINIFY_CONFIG: any = Object.assign({ + comments: false, +}, BABEL_CONFIG_BASE, { presets: [[minify, { guards: false, evaluate: false }]] });