-
Notifications
You must be signed in to change notification settings - Fork 159
Changes required for Embroider build to support Fastboot apps #753
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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,61 @@ | ||
| 'use strict'; | ||
|
|
||
| const fs = require('fs'); | ||
| const findScriptSrcs = require('find-scripts-srcs-in-document'); | ||
| const getScriptFileName = require('../utilities/get-script-file-name'); | ||
|
|
||
| module.exports = function updateManifestFromHtml(root) { | ||
| const pkg = JSON.parse(fs.readFileSync(root + '/package.json', 'UTF8')); | ||
| const htmlFileName = pkg.fastboot.manifest.htmlFile || 'index.html'; | ||
|
|
||
| // otherwise, we must parse the index.html file to figure out what's next | ||
| const indexHtml = fs.readFileSync(root + '/' + htmlFileName, 'UTF8') | ||
| // TODO: fix simple-html-tokenizer: https://github.com/tildeio/simple-html-tokenizer/pull/71 | ||
| .replace('<!DOCTYPE html>',''); | ||
|
|
||
| const assetsDir = `assets/`; | ||
| const vendorFiles = pkg.fastboot.manifest.vendorFiles || []; | ||
|
|
||
| // Find script src which are not data-fastboot-ignore | ||
| // data-fastboot-ignore in the script tag is added by the developer | ||
SparshithNR marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // These are those script files which shouldn't be loaded into fastboot sanbox. | ||
| const indexScriptFilePaths = findScriptSrcs(indexHtml, findScriptSrcs.ignoreWithAttribute('data-fastboot-ignore')) | ||
| .filter( src => { | ||
| // skipping files if they are part of vendorFiles list | ||
| // this would cover any vendor files (vendor or vendor-static) | ||
|
|
||
| // The App Files list from html in shouldn't include | ||
| // vendor files as they are already included in manifest. | ||
| // Checking if the script file from index.html | ||
| // is in the the list of vendor files in package.json and skip them. | ||
| const fileName = getScriptFileName(src); | ||
| return !vendorFiles.find(filePath => fileName === getScriptFileName(filePath)); | ||
| }); | ||
|
|
||
| /** | ||
| * Reading app files by parsing index.html so that FastBoot can load assets that | ||
| * are same as what is generated as part of the build flow and updated in | ||
| * index.html. | ||
| * | ||
| * Example: Embroider build flow upon WebPack as stage 3 processing in the build | ||
| * updates app.js to multiple chunks and updates index.html. Parsing from index.html | ||
| * allows loading same set of app files in FastBoot sandbox. | ||
| * | ||
| */ | ||
| const appFiles = new Set(); | ||
| indexScriptFilePaths.forEach(src => { | ||
| // Extract the file name from the src file paths and | ||
| // checks the file exists in the assets location on the disk. | ||
| const fileName = getScriptFileName(src); | ||
| const assetPath = `${assetsDir}${fileName}`; | ||
| const filePath = `${root}/${assetPath}`; | ||
| if(fileName && fs.existsSync(filePath)) { | ||
| appFiles.add(assetPath); | ||
| } | ||
| }); | ||
|
|
||
| const fastbootFile = pkg.fastboot.manifest.appFiles.find(x => x.endsWith('-fastboot.js')); | ||
| appFiles.add(fastbootFile); | ||
| pkg.fastboot.manifest.appFiles = Array.from(appFiles); | ||
| fs.writeFileSync(`${root}/package.json`, JSON.stringify(pkg, null, 2)); | ||
| }; | ||
This file contains hidden or 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 hidden or 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,14 @@ | ||
| 'use strict'; | ||
| // Helper to extract the script file name from a given path. | ||
SparshithNR marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Used for getting the script filename from the html file, where | ||
| // src is set to have custom context or wrapped with a placeholder. | ||
| // Examples file paths: | ||
| // "%PLACEHOLDER[c.js]%" | ||
| // "/some_context_path/e.js" | ||
| // "chunk-12.js" | ||
| // Expected output for file names: c.js, e.js, chunk-12.js | ||
| module.exports = function getScriptFileName(filePath) { | ||
| const scriptFileNameRegEx = /([a-zA-Z0-9_\.\-\(\):])+(\.js)/ig; | ||
| const match = filePath.match(scriptFileNameRegEx); | ||
| return match ? match[0] : ''; | ||
| } | ||
This file contains hidden or 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 hidden or 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,25 @@ | ||
| 'use strict'; | ||
| const { expect } = require('chai'); | ||
| const getScriptFileName = require('../lib/utilities/get-script-file-name'); | ||
|
|
||
| describe('utilities/get-script-file-name', function() { | ||
| let filePaths = [ | ||
| 'a.js', | ||
| '"%PLACEHOLDER[c.js]%"', | ||
| '/some_context_path/e.js', | ||
| 'chunk-12.js"', | ||
| 'assets/vendor.js', | ||
| '/some_context_path/vendor-static.js' | ||
| ] | ||
| it('can parse different script filePaths and return script filenames', () => { | ||
| let fileNames = filePaths.map((filePath) => getScriptFileName(filePath)); | ||
| expect(fileNames).to.deep.equal([ | ||
| 'a.js', | ||
| 'c.js', | ||
| 'e.js', | ||
| 'chunk-12.js', | ||
| 'vendor.js', | ||
| 'vendor-static.js' | ||
| ]); | ||
| }); | ||
| }); |
This file contains hidden or 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,134 @@ | ||
| 'use strict'; | ||
| const { expect } = require('chai'); | ||
| const Project = require('fixturify-project'); | ||
| const fixturify = require('fixturify'); | ||
| const fs = require('fs'); | ||
| const updateManifestFromHtml = require('../lib/embroider/update-manifest-from-html') | ||
|
|
||
| describe('embroider/update-manifest-from-html', function() { | ||
| const PROJECT = new Project('my-app'); | ||
|
|
||
| PROJECT.files['index.html'] = ` | ||
| <script src="a.js"></script> | ||
| <script src="b.js"></script> | ||
| <script src="%PLACEHOLDER[c.js]%"></script> | ||
| <script src="/some_context_path/e.js"></script> | ||
| <script src="chunk-12.js"></script> | ||
| <script src="chunk-12.test.js"></script> | ||
| <script src="fastboot-ignore.js" data-fastboot-ignore></script> | ||
| <script src="ember-cli-live-reload.js"></script> | ||
| <script src="testem.js"></script> | ||
| <script src="assets/vendor.js"></script> | ||
| <script src="assets/vendor-static.js"></script> | ||
| `; | ||
|
|
||
| PROJECT.pkg.fastboot = { | ||
| manifest: { | ||
| htmlFile: 'index.html', | ||
| appFiles: ['assets/app.js', 'assets/my-app-fastboot.js'], | ||
| vendorFiles:['assets/vendor.js', 'assets/vendor-static.js'] | ||
| } | ||
| }; | ||
| const project = PROJECT.clone(); | ||
| project.writeSync(); | ||
| const files = {}; | ||
| files['a.js'] = ''; | ||
| files['b.js'] = ''; | ||
| files['c.js'] = ''; | ||
| files['d.js'] = ''; | ||
| files['e.js'] = ''; | ||
| files['chunk-12.js'] = ''; | ||
| files['chunk-12.test.js'] = ''; | ||
|
|
||
SparshithNR marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| it(`works correctly as files are added/removed`, function() { | ||
SparshithNR marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| fixturify.writeSync(project.root + '/my-app/assets', files); | ||
| updateManifestFromHtml(project.root + '/my-app'); | ||
| project.readSync(); | ||
|
|
||
| expect(JSON.parse(project.toJSON('package.json')).fastboot.manifest.appFiles).to.eql([ | ||
| 'assets/a.js', | ||
| 'assets/b.js', | ||
| 'assets/c.js', | ||
| 'assets/e.js', | ||
| 'assets/chunk-12.js', | ||
| 'assets/chunk-12.test.js', | ||
| 'assets/my-app-fastboot.js' | ||
| ]); | ||
| }); | ||
|
|
||
| it(`Rebuild doesn't change anything if files didn't change`, function() { | ||
| // let's try a rebuild | ||
| fixturify.writeSync(project.root + '/my-app/assets', files); | ||
| updateManifestFromHtml(project.root + '/my-app') | ||
| project.readSync(); | ||
|
|
||
| expect(JSON.parse(project.toJSON('package.json')).fastboot.manifest.appFiles).to.eql([ | ||
| 'assets/a.js', | ||
| 'assets/b.js', | ||
| 'assets/c.js', | ||
| 'assets/e.js', | ||
| 'assets/chunk-12.js', | ||
| 'assets/chunk-12.test.js', | ||
| 'assets/my-app-fastboot.js' | ||
| ]); | ||
| }); | ||
|
|
||
| it(`updates when remove a file, but leave it in the HTML`, function() { | ||
| // now lets remove a file, but leave it in the HTML | ||
| delete files['a.js']; | ||
| fs.unlinkSync(project.root + '/my-app/assets/a.js'); | ||
| fixturify.writeSync(project.root + '/my-app/assets', files); | ||
|
|
||
| updateManifestFromHtml(project.root + '/my-app') | ||
| project.readSync(); | ||
|
|
||
| expect(JSON.parse(project.toJSON('package.json')).fastboot.manifest.appFiles).to.eql([ | ||
| 'assets/b.js', | ||
| 'assets/c.js', | ||
| 'assets/e.js', | ||
| 'assets/chunk-12.js', | ||
| 'assets/chunk-12.test.js', | ||
| 'assets/my-app-fastboot.js' | ||
| ]); | ||
| }); | ||
|
|
||
| it(`updates when a file is added file deleted previously`, function() { | ||
| // lets add a file back | ||
| files['a.js'] = ''; | ||
| fixturify.writeSync(project.root + '/my-app/assets', files); | ||
|
|
||
| updateManifestFromHtml(project.root + '/my-app') | ||
| project.readSync(); | ||
|
|
||
| expect(JSON.parse(project.toJSON('package.json')).fastboot.manifest.appFiles).to.eql([ | ||
| 'assets/a.js', | ||
| 'assets/b.js', | ||
| 'assets/c.js', | ||
| 'assets/e.js', | ||
| 'assets/chunk-12.js', | ||
| 'assets/chunk-12.test.js', | ||
| 'assets/my-app-fastboot.js' | ||
| ]); | ||
| }); | ||
|
|
||
| it(`updates when a new src added to html which is already on disk`, function() { | ||
| // lets add a new src to html which is already on disk | ||
|
|
||
| project.files['index.html'] += '<script src="d.js"></script>'; | ||
|
|
||
| project.writeSync(); | ||
| updateManifestFromHtml(project.root + '/my-app') | ||
| project.readSync(); | ||
|
|
||
| expect(JSON.parse(project.toJSON('package.json')).fastboot.manifest.appFiles).to.eql([ | ||
| 'assets/a.js', | ||
| 'assets/b.js', | ||
| 'assets/c.js', | ||
| 'assets/e.js', | ||
| 'assets/chunk-12.js', | ||
| 'assets/chunk-12.test.js', | ||
| 'assets/d.js', | ||
| 'assets/my-app-fastboot.js' | ||
| ]); | ||
| }); | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.