Skip to content

Commit

Permalink
Wrap tree passed to preprocessors with moduleName
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Edwards committed May 19, 2022
1 parent f843868 commit b370bfe
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 26 deletions.
5 changes: 3 additions & 2 deletions packages/compat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"bind-decorator": "^1.0.11",
"broccoli": "^3.5.2",
"broccoli-concat": "^4.2.5",
"broccoli-debug": "^0.6.5",
"broccoli-file-creator": "^2.1.1",
"broccoli-funnel": "^3.0.7",
"broccoli-merge-trees": "^4.2.0",
Expand All @@ -64,6 +65,7 @@
"devDependencies": {
"@embroider/sample-transforms": "0.0.0",
"@embroider/test-support": "0.36.0",
"@glimmer/syntax": "0.80.0",
"@types/babel__core": "^7.1.14",
"@types/babel__generator": "^7.6.2",
"@types/babel__template": "^7.4.0",
Expand All @@ -77,9 +79,8 @@
"@types/resolve": "^1.20.0",
"@types/semver": "^7.3.6",
"@types/strip-bom": "^4.0.1",
"ember-cli-htmlbars-inline-precompile": "^2.1.0",
"ember-cli-htmlbars-3": "npm:ember-cli-htmlbars@3",
"@glimmer/syntax": "0.80.0",
"ember-cli-htmlbars-inline-precompile": "^2.1.0",
"ember-engines": "^0.8.19",
"typescript": "*"
},
Expand Down
26 changes: 25 additions & 1 deletion packages/compat/src/v1-addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ import { getEmberExports } from '@embroider/core/src/load-ember-template-compile
import prepHtmlbarsAstPluginsForUnwrap from './prepare-htmlbars-ast-plugins';
import getRealAddon from './get-real-addon';

// eslint-disable-next-line @typescript-eslint/no-require-imports
const BroccoliDebug = require('broccoli-debug');
let debugTree = BroccoliDebug.buildDebugCallback('my-debug');

const stockTreeNames: AddonTreePath[] = Object.freeze([
'addon',
'addon-styles',
Expand Down Expand Up @@ -488,15 +492,35 @@ export default class V1Addon {

// applies preprocessors to JS and HBS
private transpile(tree: Node) {
tree = debugTree(tree, { label: `${this.moduleName}-1-before`, force: true });

let hasAdjustedPath = false;

if (!this.moduleName.startsWith('@')) {
hasAdjustedPath = true;
tree = buildFunnel(tree, { destDir: this.moduleName });
}

if (this.name === 'my-addon' || this.name === 'some-addon') {
console.log(this.name, this.addonInstance?.registry?.registeredForType('js'));
}
tree = this.addonInstance.preprocessJs(tree, '/', this.moduleName, {
registry: this.addonInstance.registry,
});

if (this.addonInstance.shouldCompileTemplates() && this.addonInstance.registry.load('template')?.length > 0) {
tree = this.app.preprocessRegistry.preprocessTemplates(tree, {
registry: this.addonInstance.registry,
});
}
return tree;

if (hasAdjustedPath) {
tree = buildFunnel(tree, {
srcDir: this.moduleName,
});
}

return debugTree(tree, { label: `${this.moduleName}-2-after`, force: true });
}

@Memoize()
Expand Down
133 changes: 133 additions & 0 deletions packages/compat/tests/preprocessors.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { Project, BuildResult, expectFilesAt, ExpectFile } from '@embroider/test-support';
import { throwOnWarnings } from '@embroider/core';
import merge from 'lodash/merge';

describe('preprocessors tests', function () {
jest.setTimeout(120000);
let build: BuildResult;
let app: Project;
let expectFile: ExpectFile;

throwOnWarnings();

beforeAll(async function () {
app = Project.emberNew('my-app');

const PKG_NAME_ADDON_WITH_PREPROCESS = 'addon-with-preprocess';

merge(app.files, {
config: {
'targets.js': `module.exports = { browsers: ['last 1 Chrome versions'] }`,
},
app: {
components: {
'from-the-app.js': `
import Component from '@glimmer/component';
export default class extends Component {}
`,
'from-the-app.hbs': `<div>{{this.title}}</div><Greeting/>`,
},
},
});

let addonWithPreprocess = app.addAddon(PKG_NAME_ADDON_WITH_PREPROCESS);

const INDEX_JS_WITH_PREPROCESSOR = `const { map } = require('broccoli-stew');
module.exports = {
name: require('./package').name,
setupPreprocessorRegistry(type, registry) {
if (type !== 'parent') {
return;
}
registry.add('js', {
name: 'special-path-processor',
toTree(tree, inputPath) {
if (inputPath !== '/') {
return tree;
}
let augmented = map(
tree,
'**/*.{js,css}',
function (content, relativePath) {
console.log('$$$$' + relativePath);
return \`/*path@\${relativePath}*/\n\${content}\`;
}
);
return augmented;
},
});
}
};
`;

addonWithPreprocess.linkDevPackage('broccoli-stew');
addonWithPreprocess.files['index.js'] = INDEX_JS_WITH_PREPROCESSOR;

let addon = app.addAddon('my-addon');

merge(addon.files, {
app: {
components: {
'greeting.js': `export { default } from 'my-addon/components/greeting';`,
},
},
addon: {
components: {
'greeting.js': `
import Component from '@glimmer/component';
export default class extends Component {}
`,
'greeting.hbs': `Hello World`,
},
},
});

addon.pkg['ember-addon'] = {
version: 1,
type: 'addon',
};

addon.addDependency(PKG_NAME_ADDON_WITH_PREPROCESS);

build = await BuildResult.build(app, {
stage: 2,
type: 'app',
emberAppOptions: {
tests: false,
},
});
expectFile = expectFilesAt(build.outputPath);
});

afterAll(async () => {
await build.cleanup();
});

test('dependencies are setup for this test suite correctly', () => {
expectFile('package.json').exists();
expectFile('package.json').matches(/addon-with-preprocess/, 'has the preprocessor dependency');
expectFile('node_modules/my-addon/package.json').exists();
expectFile('node_modules/my-addon/package.json').matches(
/addon-with-preprocess/,
'has the preprocessor dependency'
);
expectFile('node_modules/addon-with-preprocess/package.json').exists();
});

test.skip('preprocessor has path for app code scoped with moduleName', () => {
const assertFile = expectFile('components/from-the-app.js');
assertFile.exists();
// This is the expected output during an classic build.
assertFile.matches(/path:my-app\/components\/from-the-app\.js/, 'has a path comment in app components');
});

test.only('preprocessor has path for addon code scoped with moduleName', () => {
expectFile('node_modules/addon-with-preprocess/package.json').exists();
const assertFile = expectFile('node_modules/my-addon/components/greeting.js');
assertFile.matches(/\/\/path:my-addon\/components\/from-the-app\.js/, 'has a path comment in app components');
});
});
51 changes: 28 additions & 23 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5650,14 +5650,14 @@ browserify-zlib@^0.2.0:
pako "~1.0.5"

browserslist@^3.2.6, browserslist@^4.0.0, browserslist@^4.14.0, browserslist@^4.14.5, browserslist@^4.17.5, browserslist@^4.19.1:
version "4.19.1"
resolved "https://registry.yarnpkg.com/browserslist/-/browserslist-4.19.1.tgz#4ac0435b35ab655896c31d53018b6dd5e9e4c9a3"
integrity sha512-u2tbbG5PdKRTUoctO3NBD8FQ5HdPh1ZXPHzp1rwaa5jTc+RV9/+RlWiAIKmjRPQF+xbGM9Kklj5bZQFa2s/38A==
version "4.20.3"
resolved "https://registry.yarnpkg.com/browserslist/-/browserslist-4.20.3.tgz#eb7572f49ec430e054f56d52ff0ebe9be915f8bf"
integrity sha512-NBhymBQl1zM0Y5dQT/O+xiLP9/rzOIQdKM/eMJBAq7yBgaB6krIYLGejrwVYnSHZdqjscB1SPuAjHwxjvN6Wdg==
dependencies:
caniuse-lite "^1.0.30001286"
electron-to-chromium "^1.4.17"
caniuse-lite "^1.0.30001332"
electron-to-chromium "^1.4.118"
escalade "^3.1.1"
node-releases "^2.0.1"
node-releases "^2.0.3"
picocolors "^1.0.0"

bser@2.1.1:
Expand Down Expand Up @@ -5889,11 +5889,16 @@ caniuse-api@^3.0.0:
lodash.memoize "^4.1.2"
lodash.uniq "^4.5.0"

caniuse-lite@^1.0.0, caniuse-lite@^1.0.30001286:
caniuse-lite@^1.0.0:
version "1.0.30001309"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001309.tgz#e0ee78b9bec0704f67304b00ff3c5c0c768a9f62"
integrity sha512-Pl8vfigmBXXq+/yUz1jUwULeq9xhMJznzdc/xwl4WclDAuebcTHVefpz8lE/bMI+UN7TOkSSe7B7RnZd6+dzjA==

caniuse-lite@^1.0.30001332:
version "1.0.30001340"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001340.tgz#029a2f8bfc025d4820fafbfaa6259fd7778340c7"
integrity sha512-jUNz+a9blQTQVu4uFcn17uAD8IDizPzQkIKh3LCJfg9BkyIqExYYdyc/ZSlWUSKb8iYiXxKsxbv4zYSvkqjrxw==

capture-exit@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/capture-exit/-/capture-exit-2.0.0.tgz#fb953bfaebeb781f62898239dabb426d08a509a4"
Expand Down Expand Up @@ -7125,10 +7130,10 @@ ee-first@1.1.1:
resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d"
integrity sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=

electron-to-chromium@^1.4.17:
version "1.4.66"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.66.tgz#d7453d363dcd7b06ed1757adcde34d724e27b367"
integrity sha512-f1RXFMsvwufWLwYUxTiP7HmjprKXrqEWHiQkjAYa9DJeVIlZk5v8gBGcaV+FhtXLly6C1OTVzQY+2UQrACiLlg==
electron-to-chromium@^1.4.118:
version "1.4.137"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.137.tgz#186180a45617283f1c012284458510cd99d6787f"
integrity sha512-0Rcpald12O11BUogJagX3HsCN3FE83DSqWjgXoHo5a72KUKMSfI39XBgJpgNNxS9fuGzytaFjE06kZkiVFy2qA==

elliptic@^6.5.3:
version "6.5.4"
Expand Down Expand Up @@ -13848,15 +13853,15 @@ node-notifier@^9.0.1:
uuid "^8.3.0"
which "^2.0.2"

node-releases@^2.0.1:
version "2.0.2"
resolved "https://registry.yarnpkg.com/node-releases/-/node-releases-2.0.2.tgz#7139fe71e2f4f11b47d4d2986aaf8c48699e0c01"
integrity sha512-XxYDdcQ6eKqp/YjI+tb2C5WM2LgjnZrfYg4vgQt49EK268b6gYCHsBLrK2qvJo4FmCtqmKezb0WZFK4fkrZNsg==
node-releases@^2.0.3:
version "2.0.4"
resolved "https://registry.yarnpkg.com/node-releases/-/node-releases-2.0.4.tgz#f38252370c43854dc48aa431c766c6c398f40476"
integrity sha512-gbMzqQtTtDz/00jQzZ21PQzdI9PyLYqUSvD0p3naOhX4odFji0ZxYdnVwPTxmSwkmxhcFImpozceidSG+AgoPQ==

node-watch@0.7.2:
version "0.7.2"
resolved "https://registry.yarnpkg.com/node-watch/-/node-watch-0.7.2.tgz#545f057da8500487eb8287adcb4cb5a7338d7e21"
integrity sha512-g53VjSARRv1JdST0LZRIg8RiuLr1TaBbVPsVvxh0/0Ymvi0xYUjDuoqQQAWtHJQUXhiShowPT/aXKNeHBcyQsw==
node-watch@0.7.3:
version "0.7.3"
resolved "https://registry.yarnpkg.com/node-watch/-/node-watch-0.7.3.tgz#6d4db88e39c8d09d3ea61d6568d80e5975abc7ab"
integrity sha512-3l4E8uMPY1HdMMryPRUAl+oIHtXtyiTlIiESNSVSNxcPfzAFzeTbXFQkZfAwBbo0B1qMSG8nUABx+Gd+YrbKrQ==

nopt@^3.0.6:
version "3.0.6"
Expand Down Expand Up @@ -14996,12 +15001,12 @@ qunit-dom@^1.6.0:
ember-cli-version-checker "^5.1.1"

qunit@^2.14.1, qunit@^2.16.0:
version "2.17.2"
resolved "https://registry.yarnpkg.com/qunit/-/qunit-2.17.2.tgz#5cb278e131d931f25c109a0fdb0518be7754c25a"
integrity sha512-17isVvuOmALzsPjiV7wFg/6O5vJYXBrQZPwocfQSSh0I/rXvfX7bKMFJ4GMVW3U4P8r2mBeUy8EAngti4QD2Vw==
version "2.19.1"
resolved "https://registry.yarnpkg.com/qunit/-/qunit-2.19.1.tgz#eb1afd188da9e47f07c13aa70461a1d9c4505490"
integrity sha512-gSGuw0vErE/rNjnlBW/JmE7NNubBlGrDPQvsug32ejYhcVFuZec9yoU0+C30+UgeCGwq6Ap89K65dMGo+kDGZQ==
dependencies:
commander "7.2.0"
node-watch "0.7.2"
node-watch "0.7.3"
tiny-glob "0.2.9"

randombytes@^2.0.0, randombytes@^2.0.1, randombytes@^2.0.5, randombytes@^2.1.0:
Expand Down

0 comments on commit b370bfe

Please sign in to comment.