Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: patch ember-cli-babel #12

Merged
merged 2 commits into from
Aug 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ module.exports = {
'testem.js',
'blueprints/*/index.js',
'config/**/*.js',
'tests/dummy/config/**/*.js'
'tests/dummy/config/**/*.js',
'lib/**/*.js'
],
excludedFiles: [
'addon/**',
Expand Down
20 changes: 20 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const { resolve } = require('path');

module.exports = {
name: require('./package').name,

Expand All @@ -8,6 +10,7 @@ module.exports = {
this._ensureThisImport();

this.import('vendor/ember-cached-decorator-polyfill/index.js');
this.patchEmberModulesAPIPolyfill();
},

treeForVendor(tree) {
Expand Down Expand Up @@ -38,5 +41,22 @@ module.exports = {
app.import(asset, options);
};
}
},

patchEmberModulesAPIPolyfill() {
const babel = this.parent.findOwnAddonByName
? this.parent.findOwnAddonByName('ember-cli-babel') // parent is an addon
: this.parent.findAddonByName('ember-cli-babel'); // parent is an app

if (babel.__CachedDecoratorPolyfillApplied) return;
babel.__CachedDecoratorPolyfillApplied = true;

const { _getEmberModulesAPIPolyfill } = babel;
babel._getEmberModulesAPIPolyfill = function (...args) {
const plugins = _getEmberModulesAPIPolyfill.apply(this, args);
if (!plugins) return;

return [[resolve(__dirname, './lib/transpile-modules.js')], ...plugins];
};
}
};
182 changes: 182 additions & 0 deletions lib/transpile-modules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
'use strict';

const path = require('path');

/**
* Based on `babel-plugin-ember-modules-api-polyfill`.
* @see https://github.com/ember-cli/babel-plugin-ember-modules-api-polyfill/blob/master/src/index.js
*/
module.exports = function (babel) {
const t = babel.types;

const MODULE = '@glimmer/tracking';
const IMPORT = 'cached';
const GLOBAL = 'Ember._cached';
const MEMBER_EXPRESSION = t.MemberExpression(
t.identifier('Ember'),
t.identifier('_cached')
);

const TSTypesRequiringModification = [
'TSAsExpression',
'TSTypeAssertion',
'TSNonNullExpression'
];
const isTypescriptNode = node =>
node.type.startsWith('TS') &&
!TSTypesRequiringModification.includes(node.type);

return {
name: 'ember-cache-decorator-polyfill',
visitor: {
ImportDeclaration(path) {
let node = path.node;
let declarations = [];
let removals = [];
let specifiers = path.get('specifiers');
let importPath = node.source.value;

// Only walk specifiers if this is a module we have a mapping for
if (importPath === MODULE) {
// Iterate all the specifiers and attempt to locate their mapping
specifiers.forEach(specifierPath => {
let specifier = specifierPath.node;
let importName;

// imported is the name of the module being imported, e.g. import foo from bar
const imported = specifier.imported;

// local is the name of the module in the current scope, this is usually the same
// as the imported value, unless the module is aliased
const local = specifier.local;

// We only care about these 2 specifiers
if (
specifier.type !== 'ImportDefaultSpecifier' &&
specifier.type !== 'ImportSpecifier'
) {
if (specifier.type === 'ImportNamespaceSpecifier') {
throw new Error(
`Using \`import * as ${specifier.local.name} from '${importPath}'\` is not supported.`
);
}
return;
}

// Determine the import name, either default or named
if (specifier.type === 'ImportDefaultSpecifier') {
importName = 'default';
} else {
importName = imported.name;
}

if (importName !== IMPORT) return;

removals.push(specifierPath);

if (
path.scope.bindings[local.name].referencePaths.find(
rp => rp.parent.type === 'ExportSpecifier'
)
) {
// not safe to use path.scope.rename directly
declarations.push(
t.variableDeclaration('var', [
t.variableDeclarator(
t.identifier(local.name),
t.identifier(GLOBAL)
)
])
);
} else {
// Replace the occurences of the imported name with the global name.
let binding = path.scope.getBinding(local.name);

binding.referencePaths.forEach(referencePath => {
if (!isTypescriptNode(referencePath.parentPath)) {
referencePath.replaceWith(MEMBER_EXPRESSION);
}
});
}
});
}

if (removals.length > 0) {
if (removals.length === node.specifiers.length) {
path.replaceWithMultiple(declarations);
} else {
removals.forEach(specifierPath => specifierPath.remove());
path.insertAfter(declarations);
}
}
},

ExportNamedDeclaration(path) {
let node = path.node;
if (!node.source) {
return;
}

let replacements = [];
let removals = [];
let specifiers = path.get('specifiers');
let importPath = node.source.value;

// Only walk specifiers if this is a module we have a mapping for
if (importPath === MODULE) {
// Iterate all the specifiers and attempt to locate their mapping
specifiers.forEach(specifierPath => {
let specifier = specifierPath.node;

// exported is the name of the module being export,
// e.g. `foo` in `export { computed as foo } from '@ember/object';`
const exported = specifier.exported;

// local is the original name of the module, this is usually the same
// as the exported value, unless the module is aliased
const local = specifier.local;

// We only care about the ExportSpecifier
if (specifier.type !== 'ExportSpecifier') {
return;
}

// Determine the import name, either default or named
let importName = local.name;

if (importName !== IMPORT) return;

removals.push(specifierPath);

let declaration;
const globalAsIdentifier = t.identifier(GLOBAL);
if (exported.name === 'default') {
declaration = t.exportDefaultDeclaration(globalAsIdentifier);
} else {
// Replace the node with a new `var name = Ember.something`
declaration = t.exportNamedDeclaration(
t.variableDeclaration('var', [
t.variableDeclarator(exported, globalAsIdentifier)
]),
[],
null
);
}
replacements.push(declaration);
});
}

if (removals.length > 0 && removals.length === node.specifiers.length) {
path.replaceWithMultiple(replacements);
} else if (replacements.length > 0) {
removals.forEach(specifierPath => specifierPath.remove());
path.insertAfter(replacements);
}
}
}
};
};

// Provide the path to the package's base directory for caching with broccoli
// Ref: https://github.com/babel/broccoli-babel-transpiler#caching
module.exports.baseDir = () => path.resolve(__dirname, '..');
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"test": "tests"
},
"scripts": {
"postinstall": "patch-package",
"build": "ember build --environment=production",
"lint": "npm-run-all --aggregate-output --continue-on-error --parallel lint:*",
"lint:hbs": "ember-template-lint .",
Expand All @@ -41,8 +40,7 @@
},
"dependencies": {
"ember-cache-primitive-polyfill": "^1.0.0",
"ember-cli-babel": "^7.21.0",
"patch-package": "^6.2.2"
"ember-cli-babel": "^7.21.0"
},
"devDependencies": {
"@ember/optional-features": "^1.3.0",
Expand Down Expand Up @@ -97,6 +95,7 @@
"edition": "octane"
},
"ember-addon": {
"before": "ember-cli-babel",
"configPath": "tests/dummy/config"
},
"release-it": {
Expand Down
17 changes: 0 additions & 17 deletions patches/ember-rfc176-data+0.3.15.patch

This file was deleted.

35 changes: 0 additions & 35 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1749,11 +1749,6 @@
resolved "https://registry.yarnpkg.com/@xtuc/long/-/long-4.2.1.tgz#5c85d662f76fa1d34575766c5dcd6615abcd30d8"
integrity sha512-FZdkNBDqBRHKQ2MEbSC17xnPFOhZxeJ2YGSfr2BKf3sujG49Qe3bB+rGCwQfIaA7WHnGeGkSijX4FuBCdrzW/g==

"@yarnpkg/lockfile@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@yarnpkg/lockfile/-/lockfile-1.1.0.tgz#e77a97fbd345b76d83245edcd17d393b1b41fb31"
integrity sha512-GpSwvyXOcOOlV70vbnzjj4fW5xW/FdUF6nQEt1ENy7m4ZCczi1+/buVUPAqmGfqznsORNFzUMjctTIp8a9tuCQ==

abab@^2.0.0:
version "2.0.4"
resolved "https://registry.yarnpkg.com/abab/-/abab-2.0.4.tgz#6dfa57b417ca06d21b2478f0e638302f99c2405c"
Expand Down Expand Up @@ -8077,13 +8072,6 @@ kind-of@^6.0.0, kind-of@^6.0.2:
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-6.0.3.tgz#07c05034a6c349fa06e24fa35aa76db4580ce4dd"
integrity sha512-dcS1ul+9tmeD95T+x28/ehLgd9mENa3LsvDTtzm3vyBEO7RPptvAD+t44WVXaUjTBRcrpFeFlC8WCruUR456hw==

klaw-sync@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/klaw-sync/-/klaw-sync-6.0.0.tgz#1fd2cfd56ebb6250181114f0a581167099c2b28c"
integrity sha512-nIeuVSzdCCs6TDPTqI8w1Yre34sSq7AkZ4B3sfOBbI2CgVSB4Du4aLQijFU2+lhAFCwt9+42Hel6lQNIv6AntQ==
dependencies:
graceful-fs "^4.1.11"

latest-version@^5.0.0:
version "5.1.0"
resolved "https://registry.yarnpkg.com/latest-version/-/latest-version-5.1.0.tgz#119dfe908fe38d15dfa43ecd13fa12ec8832face"
Expand Down Expand Up @@ -9593,24 +9581,6 @@ pascalcase@^0.1.1:
resolved "https://registry.yarnpkg.com/pascalcase/-/pascalcase-0.1.1.tgz#b363e55e8006ca6fe21784d2db22bd15d7917f14"
integrity sha1-s2PlXoAGym/iF4TS2yK9FdeRfxQ=

patch-package@^6.2.2:
version "6.2.2"
resolved "https://registry.yarnpkg.com/patch-package/-/patch-package-6.2.2.tgz#71d170d650c65c26556f0d0fbbb48d92b6cc5f39"
integrity sha512-YqScVYkVcClUY0v8fF0kWOjDYopzIM8e3bj/RU1DPeEF14+dCGm6UeOYm4jvCyxqIEQ5/eJzmbWfDWnUleFNMg==
dependencies:
"@yarnpkg/lockfile" "^1.1.0"
chalk "^2.4.2"
cross-spawn "^6.0.5"
find-yarn-workspace-root "^1.2.1"
fs-extra "^7.0.1"
is-ci "^2.0.0"
klaw-sync "^6.0.0"
minimist "^1.2.0"
rimraf "^2.6.3"
semver "^5.6.0"
slash "^2.0.0"
tmp "^0.0.33"

[email protected]:
version "0.0.1"
resolved "https://registry.yarnpkg.com/path-browserify/-/path-browserify-0.0.1.tgz#e6c4ddd7ed3aa27c68a20cc4e50e1a4ee83bbc4a"
Expand Down Expand Up @@ -10855,11 +10825,6 @@ slash@^1.0.0:
resolved "https://registry.yarnpkg.com/slash/-/slash-1.0.0.tgz#c41f2f6c39fc16d1cd17ad4b5d896114ae470d55"
integrity sha1-xB8vbDn8FtHNF61LXYlhFK5HDVU=

slash@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/slash/-/slash-2.0.0.tgz#de552851a1759df3a8f206535442f5ec4ddeab44"
integrity sha512-ZYKh3Wh2z1PpEXWr0MpSBZ0V6mZHAQfYevttO11c51CaWjGTaadiKZ+wVt1PbMlDV5qhMFslpZCemhwOK7C89A==

slash@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/slash/-/slash-3.0.0.tgz#6539be870c165adbd5240220dbe361f1bc4d4634"
Expand Down