Skip to content

Commit

Permalink
fix(rollup-plugin-lwc-compiler): Allow rollup in COMPAT mode (#121)
Browse files Browse the repository at this point in the history
* fix(rollup-plugin-lwc-compiler): Allow rollup plugin to compile in COMPAT mode
* fix(rollup-plugin-lwc-compiler): Simplify code path
  • Loading branch information
diervo authored Feb 27, 2018
1 parent 9cd09d4 commit 4afb73a
Show file tree
Hide file tree
Showing 13 changed files with 367 additions and 81 deletions.
2 changes: 1 addition & 1 deletion packages/lwc-compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"test": "DIR=`pwd` && cd ../../ && jest $DIR"
},
"dependencies": {
"babel-preset-compat": "0.17.29",
"babel-preset-compat": "0.17.30",
"@babel/plugin-proposal-class-properties": "7.0.0-beta.40",
"@babel/plugin-proposal-object-rest-spread": "7.0.0-beta.40",
"cssnano": "^3.10.0",
Expand Down
1 change: 1 addition & 0 deletions packages/lwc-compiler/src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function getTransformer(fileName) {

default:
throw new Error(`No available transformer for ${fileName}`);

}
}

Expand Down
6 changes: 3 additions & 3 deletions packages/lwc-integration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
"lwc-compiler": "0.17.17",
"lwc-engine": "0.17.17",
"minimist": "^1.2.0",
"rollup-plugin-compat": "0.17.29",
"compat-polyfills": "0.17.29",
"babel-preset-compat": "0.17.29",
"rollup-plugin-compat": "0.17.30",
"compat-polyfills": "0.17.30",
"babel-preset-compat": "0.17.30",
"rollup-plugin-lwc-compiler": "0.17.17",
"wdio-junit-reporter": "^0.3.1",
"wdio-mocha-framework": "^0.5.10",
Expand Down
2 changes: 1 addition & 1 deletion packages/lwc-wire-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"lwc-jest-transformer": "0.17.15",
"rollup": "~0.56.2",
"rollup-plugin-lwc-compiler": "0.17.17",
"rollup-plugin-compat": "0.17.29",
"rollup-plugin-compat": "0.17.30",
"rollup-plugin-node-resolve": "^3.0.0",
"rollup-plugin-uglify": "^2.0.1",
"uglify-es": "~3.3.6"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
import __ProxyCompat from "proxy-compat";
import { Element, createElement } from "engine";
import "compat-polyfills/polyfills";

function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}

var __getKey = __ProxyCompat.getKey;
var __setKey = __ProxyCompat.setKey;
var __inKey = __ProxyCompat.inKey;

function _defineProperties(target, props) {
for (var i = 0; i < __getKey(props, "length"); i++) {
var descriptor = __getKey(props, i);

__setKey(
descriptor,
"enumerable",
__getKey(descriptor, "enumerable") || false
);

__setKey(descriptor, "configurable", true);

if (__inKey(descriptor, "value")) __setKey(descriptor, "writable", true);
Object.defineProperty(target, __getKey(descriptor, "key"), descriptor);
}
}

function _createClass(Constructor, protoProps, staticProps) {
if (protoProps)
_defineProperties(__getKey(Constructor, "prototype"), protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}

function _possibleConstructorReturn(self, call) {
if (call && (typeof call === "object" || typeof call === "function")) {
return call;
}

if (self === void 0) {
throw new ReferenceError(
"this hasn't been initialised - super() hasn't been called"
);
}

return self;
}

var __setKey$1 = __ProxyCompat.setKey;
var __getKey$1 = __ProxyCompat.getKey;
function _inherits(subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function");
}

__setKey$1(
subClass,
"prototype",
Object.create(superClass && __getKey$1(superClass, "prototype"), {
constructor: {
value: subClass,
enumerable: false,
writable: true,
configurable: true
}
})
);

if (superClass)
Object.setPrototypeOf
? Object.setPrototypeOf(subClass, superClass)
: __setKey$1(subClass, "__proto__", superClass);
}

var style = undefined;

var style$2 = undefined;

var __getKey$2 = __ProxyCompat.getKey;
var __setKey$2 = __ProxyCompat.setKey;
function tmpl($api, $cmp, $slotset, $ctx) {
var api_dynamic = __getKey$2($api, "d"),
api_element = __getKey$2($api, "h");

return [
api_element(
"div",
{
key: 1
},
[api_dynamic(__getKey$2($cmp, "x"))]
)
];
}

if (style$2) {
var tagName = "x-foo";
var token = "x-foo_foo";

__setKey$2(tmpl, "token", token);

__setKey$2(tmpl, "style", style$2(tagName, token));
}

var __callKey = __ProxyCompat.callKey;
var __getKey$3 = __ProxyCompat.getKey;

var Foo =
/*#__PURE__*/
(function(_Element) {
_inherits(Foo, _Element);

function Foo() {
_classCallCheck(this, Foo);

return _possibleConstructorReturn(
this,
__callKey(
__getKey$3(Foo, "__proto__") || Object.getPrototypeOf(Foo),
"apply",
this,
arguments
)
);
}

_createClass(Foo, [
{
key: "render",
value: function render() {
return tmpl;
}
}
]);

return Foo;
})(Element);

var __getKey$4 = __ProxyCompat.getKey;
var __setKey$3 = __ProxyCompat.setKey;
function tmpl$1($api, $cmp, $slotset, $ctx) {
var api_custom_element = __getKey$4($api, "c"),
api_element = __getKey$4($api, "h");

return [
api_element(
"div",
{
classMap: {
container: true
},
key: 2
},
[
api_custom_element("x-foo", Foo, {
props: {
x: "1"
},
key: 1
})
]
)
];
}

if (style) {
var tagName$1 = "x-app";
var token$1 = "x-app_app";

__setKey$3(tmpl$1, "token", token$1);

__setKey$3(tmpl$1, "style", style(tagName$1, token$1));
}

var __callKey$1 = __ProxyCompat.callKey;
var __getKey$5 = __ProxyCompat.getKey;
var __setKey$4 = __ProxyCompat.setKey;

var App =
/*#__PURE__*/
(function(_Element) {
_inherits(App, _Element);

function App() {
var _this;

_classCallCheck(this, App);

_this = _possibleConstructorReturn(
this,
__callKey$1(
__getKey$5(App, "__proto__") || Object.getPrototypeOf(App),
"call",
this
)
);

__setKey$4(_this, "list", []);

return _this;
}

_createClass(App, [
{
key: "render",
value: function render() {
return tmpl$1;
}
}
]);

return App;
})(Element);

var __callKey$2 = __ProxyCompat.callKey;

var container = __callKey$2(document, "getElementById", "main");

var element = createElement("x-app", {
is: App
});

__callKey$2(container, "appendChild", element);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Waiting to fix minify plugin
74 changes: 44 additions & 30 deletions packages/rollup-plugin-lwc-compiler/__tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,65 @@ function pretty(str) {
return prettier.format(str);
}

const skipTests = [
'.babelrc',
'.DS_Store',
'bundle',
'simple_app',
];
function fsExpected(fileName) {
return fs.readFileSync(path.join(fixturesDir, `${fileName}.js`), 'utf-8');
}

const fixturesDir = path.join(__dirname, 'fixtures');
const simpleAppDir = path.join(fixturesDir, 'simple_app/src');

describe('emit asserts for: ', () => {
fs.readdirSync(fixturesDir).map((caseName) => {
if (skipTests.indexOf(caseName) >= 0) return;
const fixtureCaseDir = path.join(fixturesDir, caseName);
describe('default configuration', () => {
const rollupOptions = { allowUnnamespaced: true };

it(`output match: ${caseName}`, () => {
const entry = path.join(fixtureCaseDir, caseName + '.js');
return doRollup(entry).then(res => {
const { code: actual } = res;
it(`simple app`, () => {
const entry = path.join(simpleAppDir, 'main.js');
return doRollup(entry, rollupOptions).then(({ code: actual }) => {
const expected = fsExpected('expected_default_config_simple_app');
expect(pretty(actual)).toBe(pretty(expected));
})
});

const expected = fs.readFileSync(path.join(fixtureCaseDir, 'expected.js'), 'utf-8');
expect(pretty(actual)).toBe(pretty(expected));
})
});
it(`simple component`, () => {
const entry = path.join(simpleAppDir, 'main.js');
return doRollup(entry, rollupOptions).then(({ code: actual }) => {
const expected = fsExpected('expected_default_config_simple_app');
expect(pretty(actual)).toBe(pretty(expected));
})
});
});

describe('rollup in compat mode', () => {
const rollupOptions = { allowUnnamespaced: true, mode: 'compat' };

describe('emit asserts for simple_app: ', () => {
const fixtureCaseDir = path.join(fixturesDir, 'simple_app/src');

it(`output match:`, () => {
const entry = path.join(fixtureCaseDir, 'main.js');
return doRollup(entry, { allowUnnamespaced: true } ).then(res => {
const { code: actual } = res;

const expected = fs.readFileSync(path.join(fixturesDir, 'simple_app/expected.js'), 'utf-8');
it(`simple app`, () => {
const entry = path.join(simpleAppDir, 'main.js');
return doRollup(entry, rollupOptions).then(({ code: actual }) => {
const expected = fsExpected('expected_compat_config_simple_app');
expect(pretty(actual)).toBe(pretty(expected));
})
});
});

function doRollup(entry, options = {}) {
// describe('rollup in prod_compat mode', () => {
// const rollupOptions = { allowUnnamespaced: true, mode: 'prod_compat' };
// it(`simple app`, () => {
// const entry = path.join(simpleAppDir, 'main.js');
// return doRollup(entry, rollupOptions).then(({ code: actual }) => {
// const expected = fsExpected('expected_prod_compat_config_simple_app');
// expect(pretty(actual)).toBe(pretty(expected));
// })
// });
// });

function doRollup(input, options = {}) {
return rollup.rollup({
input: entry,
external: ['engine'],
input,
external: [
'engine',
'compat-polyfills/polyfills',
'compat-polyfills/downgrade',
'proxy-compat',
],
plugins: [ rollupCompile(options) ],
onwarn(warn) {
if (warn && warn.code !== 'UNRESOLVED_IMPORT') {
Expand Down
6 changes: 5 additions & 1 deletion packages/rollup-plugin-lwc-compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
"description": "Rollup plugin to compile LWC",
"main": "src/index.js",
"license": "MIT",
"scripts": {
"test": "DIR=`pwd` && cd ../../ && jest $DIR"
},
"devDependencies": {
"lwc-compiler": "0.17.17",
"lwc-engine": "0.17.17"
},
"dependencies": {
"glob": "^7.1.2",
"lwc-module-resolver": "0.17.17",
"rollup-pluginutils": "^2.0.1"
"rollup-pluginutils": "^2.0.1",
"rollup-plugin-compat": "0.17.30"
},
"peerDependencies": {
"lwc-compiler": "~0.17.17",
Expand Down
Loading

0 comments on commit 4afb73a

Please sign in to comment.