This repository was archived by the owner on Nov 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 647
feat: precompile assets on build #2091
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
cb8d5bd
Use precompiled assets in production mode
BinaryMuse fe52290
Make browserify options reusable
BinaryMuse 5871815
Add precomilation task
BinaryMuse 1bd8f60
Precompile assets as part of app deployment
BinaryMuse 50f39d8
Compile assets as build instead of release phase
BinaryMuse 4a4f487
:shirt: fix lint
BinaryMuse ae78fd9
Minify precompiled JS
BinaryMuse d0b2d95
Precompile assets before integration tests
BinaryMuse 733493b
Simplify browserify middleware
BinaryMuse 15719da
Provide the correct NODE_ENV to asset precompilation
BinaryMuse a2e2eb0
Merge remote-tracking branch 'origin/master' into mkt/precompile-assets
BinaryMuse 07378e4
Convert precompilation script to JS
BinaryMuse 5be9eea
Use specific lodash imports
BinaryMuse 96da0b1
Merge branch 'master' into mkt/precompile-assets
eb7a766
Remove old precompile assets script
BinaryMuse 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,3 +9,4 @@ css/**/*.css | |
| yarn.lock | ||
| cypress/videos/* | ||
| /public/styles/octicons/octicons.html | ||
| /precompiled/ | ||
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 @@ | ||
| const nodeModulesToAvoidBabelifying = [ | ||
| 'lodash', | ||
| 'lunr', | ||
| 'prettydate' | ||
| ] | ||
|
|
||
| const excludeRegex = new RegExp(`/node_modules/(${nodeModulesToAvoidBabelifying.join('|')})`) | ||
|
|
||
| module.exports = function doBrowserify (browserify) { | ||
| return function (entry) { | ||
| return browserify(entry, { | ||
| transform: [ | ||
| ['babelify', { | ||
| global: true, | ||
| exclude: excludeRegex, | ||
| presets: [ | ||
| ['@babel/preset-env', { targets: '> 0.25%, not dead' }] | ||
| ] | ||
| }], | ||
| 'brfs' | ||
| ] | ||
| }) | ||
| } | ||
| } | ||
|
|
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 |
|---|---|---|
| @@ -1,26 +1,4 @@ | ||
| const browserify = require('browserify-middleware') | ||
| const browserifyOptions = require('./browserify-opts') | ||
|
|
||
| const nodeModulesToAvoidBabelifying = [ | ||
| 'lodash', | ||
| 'lunr', | ||
| 'prettydate' | ||
| ] | ||
|
|
||
| const excludeRegex = new RegExp(`/node_modules/(${nodeModulesToAvoidBabelifying.join('|')})`) | ||
|
|
||
| function babelifyMiddleware (entry) { | ||
| return browserify(entry, { | ||
| transform: [ | ||
| ['babelify', { | ||
| global: true, | ||
| exclude: excludeRegex, | ||
| presets: [ | ||
| ['@babel/preset-env', { targets: '> 0.25%, not dead' }] | ||
| ] | ||
| }], | ||
| 'brfs' | ||
| ] | ||
| }) | ||
| } | ||
|
|
||
| module.exports = babelifyMiddleware | ||
| module.exports = browserifyOptions(browserify) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| npm run precompile-assets | ||
| npm start & | ||
| wait-on http://localhost:5000 | ||
| cypress run --record --key a0cba5c6-0650-4abe-8d41-d990bb7a0a66 |
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,90 @@ | ||
| const path = require('path') | ||
| const stream = require('stream') | ||
| const fs = require('fs-extra') | ||
| const browserify = require('browserify') | ||
| const browserifyOptions = require('../middleware/browserify-opts') | ||
| const sass = require('node-sass') | ||
| const uglify = require('uglify-js') | ||
|
|
||
| function dir (...parts) { | ||
| return path.join(__dirname, '..', ...parts) | ||
| } | ||
|
|
||
| function uglifyStream () { | ||
| const buffers = [] | ||
| return new stream.Transform({ | ||
| transform (chunk, _encoding, callback) { | ||
| buffers.push(chunk) | ||
| callback() | ||
| }, | ||
|
|
||
| flush (callback) { | ||
| const code = Buffer.concat(buffers).toString() | ||
| const compiled = uglify.minify(code) | ||
| this.push(compiled.code) | ||
| callback() | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| const PATHS = { | ||
| precompiled: dir('precompiled'), | ||
| scripts: dir('precompiled', 'scripts'), | ||
| styles: dir('precompiled', 'styles'), | ||
| nodeModules: dir('node_modules'), | ||
|
|
||
| jsEntry: dir('scripts', 'index.js'), | ||
| jsDestination: dir('precompiled', 'scripts', 'index.js'), | ||
|
|
||
| cssEntry: dir('public', 'styles', 'index.scss'), | ||
| cssDestination: dir('precompiled', 'styles', 'index.css') | ||
| } | ||
|
|
||
| async function precompileAssets () { | ||
| try { | ||
| console.log('Creating directories...') | ||
| await fs.remove(PATHS.precompiled) | ||
| await fs.ensureDir(PATHS.scripts) | ||
| await fs.ensureDir(PATHS.styles) | ||
| console.log('Precompiling JS...') | ||
| await precompileJavaScript() | ||
| console.log('Precompiling CSS...') | ||
| await precompileCss() | ||
| } catch (err) { | ||
| console.error(err) | ||
| process.exit(1) | ||
| } | ||
| } | ||
|
|
||
| function precompileJavaScript () { | ||
| return new Promise((resolve, reject) => { | ||
| const b = browserifyOptions(browserify)(PATHS.jsEntry) | ||
| const pipe = b.bundle() | ||
| .pipe(uglifyStream()) | ||
| .pipe(fs.createWriteStream(PATHS.jsDestination)) | ||
| pipe.on('error', reject) | ||
| pipe.on('finish', resolve) | ||
| }) | ||
| } | ||
|
|
||
| function precompileCss () { | ||
| return new Promise((resolve, reject) => { | ||
| sass.render({ | ||
| file: PATHS.cssEntry, | ||
| includePaths: [ | ||
| PATHS.nodeModules | ||
| ] | ||
| }, async function onSassCompiled (err, result) { | ||
| if (err) { | ||
| return reject(err) | ||
| } | ||
|
|
||
| await fs.writeFile(PATHS.cssDestination, result.css) | ||
| resolve() | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| if (require.main === module) { | ||
| precompileAssets() | ||
| } |
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
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
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.