-
-
Notifications
You must be signed in to change notification settings - Fork 67
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
Initial implementation of co-located templates RFC. #249
Merged
Merged
Changes from 11 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
fbda2e0
Initial spike of co-located templates RFC.
chancancode 764615e
Use globals for now
chancancode a640658
Fix addon compilation
chancancode 4c6017b
Better error for missing default export
chancancode 942c301
v3.2.0-alpha.0
chancancode 66dff2f
Fix some Node 8 issues
chancancode b9295c5
Fix lint
chancancode 6b5a4d6
Extract ColocatedTemplateProcessor to dedicated file.
rwjblue 10e5f31
Merge remote-tracking branch 'origin/master' into colocation
rwjblue 3aabb50
Update linting config to allow async functions.
rwjblue d7a579b
Add broccoli-debug logging.
rwjblue f7f3f47
Start fleshing out basic testing infrastructure.
rwjblue 1dbfb87
Merge remote-tracking branch 'origin/master' into colocation
rwjblue 6ba80f4
Merge remote-tracking branch 'origin/master' into colocation
rwjblue 24e8ad6
yarn lint:js --fix
rwjblue fb2808a
Extract helper method to build merged component JS contents.
rwjblue f5a0b01
Add TODO comments for additional tasks.
rwjblue 6de52e5
Flesh out more tests.
rwjblue 5709c0f
Avoid adding the import for setComponentTemplate.
rwjblue b44e14b
yarn lint:js --fix
rwjblue bb4ad58
Fix colocated-broccoli-plugin tests
rwjblue 9362a4c
Add babel plugin to add setComponentTemplate usage.
rwjblue 952287b
Simplify implementation / reduce duplication.
rwjblue cfa93e3
Make requires more lazy.
rwjblue eba43a7
Add proper guard for using colocation.
rwjblue 99ac97d
Refactor registry toTree setup.
rwjblue 60b8ae9
Tweak comments in colocated-broccoli-plugin.
rwjblue 2d58f11
Ensure debugTree's are not "merged" for different invocations.
rwjblue 9972294
Add tests for scoped addons.
rwjblue db7e5f2
Only process colocated files from <app-or-addon-name>/components/
rwjblue 1ef7d42
Merge pull request #280 from rwjblue/colocation-refactor
rwjblue 791bde4
Fix setComponentTemplate argument ordering for native classes.
rwjblue 253503f
Only opt-in to colocation when using octane edition.
rwjblue 35b66bf
Merge remote-tracking branch 'origin/master' into colocation
rwjblue File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const mkdirp = require('mkdirp'); | ||
const copyFileSync = require('fs-copy-file-sync'); | ||
const path = require('path'); | ||
const walkSync = require('walk-sync'); | ||
const Plugin = require('broccoli-plugin'); | ||
|
||
module.exports = class ColocatedTemplateProcessor extends Plugin { | ||
constructor(tree, options) { | ||
super([tree], options); | ||
|
||
this.options = options; | ||
} | ||
|
||
build() { | ||
// TODO: do we need to pass through all files, or only template files? | ||
let files = walkSync(this.inputPaths[0], { directories: false }); | ||
|
||
let filesToCopy = []; | ||
files.forEach(filePath => { | ||
let filePathParts = path.parse(filePath); | ||
let inputPath = path.join(this.inputPaths[0], filePath); | ||
|
||
// TODO: why are these different? | ||
// Apps: my-app/components/foo.hbs, my-app/templates/components/foo.hbs | ||
// Addons: components/foo.js, templates/components/foo.hbs | ||
|
||
// TODO: make this more robust | ||
let isInsideComponentsFolder = | ||
(filePath.includes('/components/') || filePath.startsWith('components/')) && | ||
!filePath.includes('/templates/components/') && | ||
!filePath.startsWith('templates/components'); | ||
|
||
// copy forward non-hbs files | ||
// TODO: don't copy .js files that will ultimately be overridden | ||
if (!isInsideComponentsFolder || filePathParts.ext !== '.hbs') { | ||
filesToCopy.push(filePath); | ||
return; | ||
} | ||
|
||
// TODO: deal with alternate extensions (e.g. ts) | ||
let possibleJSPath = path.join(filePathParts.dir, filePathParts.name + '.js'); | ||
let hasJSFile = fs.existsSync(path.join(this.inputPaths[0], possibleJSPath)); | ||
|
||
if (filePathParts.name === 'template') { | ||
// TODO: maybe warn? | ||
return; | ||
} | ||
|
||
let templateContents = fs.readFileSync(inputPath, { encoding: 'utf8' }); | ||
let jsContents = null; | ||
|
||
// TODO: deal with hygiene | ||
if (hasJSFile) { | ||
// add the template, call setComponentTemplate | ||
|
||
jsContents = fs.readFileSync(path.join(this.inputPaths[0], possibleJSPath), { encoding: 'utf8' }); | ||
|
||
if (jsContents.includes('export default')) { | ||
jsContents = jsContents.replace('export default', 'const CLASS ='); | ||
} else { | ||
let message = `${filePath}\` does not contain a \`default export\`. Did you forget to export the component class?`; | ||
jsContents = `${jsContents}\nthrow new Error(${JSON.stringify(message)})`; | ||
} | ||
|
||
jsContents = `${jsContents} | ||
const setComponentTemplate = Ember._setComponentTemplate; | ||
const TEMPLATE = ${this.options.precompile(templateContents)}; | ||
export default setComponentTemplate(TEMPLATE, CLASS);`; | ||
} else { | ||
// create JS file, use null component pattern | ||
jsContents = `const templateOnlyComponent = Ember._templateOnlyComponent; | ||
const setComponentTemplate = Ember._setComponentTemplate; | ||
const TEMPLATE = ${this.options.precompile(templateContents)}; | ||
const CLASS = templateOnlyComponent(); | ||
export default setComponentTemplate(TEMPLATE, CLASS);`; | ||
} | ||
|
||
|
||
let outputPath = path.join(this.outputPath, possibleJSPath); | ||
|
||
// TODO: don't speculatively mkdirSync (likely do in a try/catch with ENOENT) | ||
mkdirp.sync(path.dirname(outputPath)); | ||
fs.writeFileSync(outputPath, jsContents, { encoding: 'utf8' }); | ||
}); | ||
|
||
filesToCopy.forEach(filePath => { | ||
let inputPath = path.join(this.inputPaths[0], filePath); | ||
let outputPath = path.join(this.outputPath, filePath); | ||
|
||
// avoid copying file over top of a previously written one | ||
if (fs.existsSync(outputPath)) { | ||
return; | ||
} | ||
|
||
// TODO: don't speculatively mkdirSync (likely do in a try/catch with ENOENT) | ||
mkdirp.sync(path.dirname(outputPath)); | ||
copyFileSync(inputPath, outputPath); | ||
}) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello from bar! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import Component from '@ember/component'; | ||
|
||
export default Component.extend({ | ||
attributeBindings: ['lolol'], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Stuff!! |
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll need to tweak this, as there are breaking changes now on master.