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: begin adding asset path functionality #4

Open
wants to merge 44 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
7b39690
feat(gatsby): add an asset path registry that plugins can hook into
DSchau Dec 6, 2018
feed176
chore: add another test
DSchau Dec 6, 2018
915e22f
chore: add assets to onPostBuild
DSchau Dec 6, 2018
3a64086
refactor: use an array for assets
DSchau Dec 6, 2018
23101c7
chore: begin integrating into CLI
DSchau Dec 6, 2018
131d62f
Merge remote-tracking branch 'upstream/master' into gatsby/asset-prefix
DSchau Dec 28, 2018
621c74a
chore: small tweaks
DSchau Dec 28, 2018
b91b84c
chore: get closer to getting it working
DSchau Jan 2, 2019
ac65879
chore: keep iterating
DSchau Jan 2, 2019
11b800b
chore: add another
DSchau Jan 4, 2019
43fb60d
Merge branch 'gatsby/asset-prefix' of github.com:DSchau/gatsby into g…
DSchau Jan 4, 2019
5eeacf7
fix: use string joining (this is not for fs paths)
DSchau Jan 8, 2019
617fefb
feat: expose normalize path util for parsing url parts
DSchau Jan 8, 2019
5bc128b
fix: fix trailing slash
DSchau Jan 8, 2019
bef2b66
Merge remote-tracking branch 'upstream/master' into gatsby/asset-prefix
DSchau Jan 8, 2019
ee33c89
chore: remove prefix-assets option (think it makes sense to use
DSchau Jan 8, 2019
c5052c1
test: fix pathPrefix test (webpack requires trailing slash)
DSchau Jan 9, 2019
925ffaf
test: fix failing node tests
DSchau Jan 9, 2019
bb3e19a
chore: update snapshots
DSchau Jan 9, 2019
ea1f93d
chore: add public folder (was gitignored!)
DSchau Jan 9, 2019
b03330f
chore: validate assetPrefix as a URI
DSchau Jan 9, 2019
77a0f5d
Merge remote-tracking branch 'upstream/master' into gatsby/asset-prefix
DSchau Jan 10, 2019
2b2eba2
fix: correctly make links (and probably other things) work
DSchau Jan 10, 2019
3f89dc1
test: get tests passing again
DSchau Jan 10, 2019
4258a34
refactor(gatsby-transformer-sharp): pass the Gatsby API args
DSchau Jan 11, 2019
1547347
chore: use withAssetPrefix
DSchau Jan 11, 2019
def1cbb
chore: more tweaks
DSchau Jan 11, 2019
e35b507
chore: use withPathPrefix and fallback if not available
DSchau Jan 11, 2019
8a00137
chore: make the necessary changes in packages to get fully working
DSchau Jan 11, 2019
0e20b1b
Merge remote-tracking branch 'upstream/master' into gatsby/asset-prefix
DSchau Feb 5, 2019
14ae1ca
chore: intentionally reset plugin changes
DSchau Feb 5, 2019
9f54525
test: hopefully fix tests (fingers crossed emoji)
DSchau Feb 5, 2019
f97f91d
chore: remove test file
DSchau Feb 5, 2019
97d727d
Merge remote-tracking branch 'upstream/master' into gatsby/asset-prefix
DSchau Feb 6, 2019
5c75200
refactor: tweak path normalization for asset registry
DSchau Feb 6, 2019
2348991
chore: remove todo comment
DSchau Feb 6, 2019
b8f35da
Merge remote-tracking branch 'upstream/master' into gatsby/asset-prefix
DSchau Feb 12, 2019
31a2632
Merge remote-tracking branch 'upstream/master' into gatsby/asset-prefix
DSchau Feb 12, 2019
847805f
Merge remote-tracking branch 'upstream/master' into gatsby/asset-prefix
DSchau Feb 15, 2019
6336920
fix: (hopefully) fix tests and normalize path from getAssets
DSchau Feb 15, 2019
2b39dac
chore: use file name (basename) to avoid snapshot failurse
DSchau Feb 15, 2019
f193377
test: fix unit tests
DSchau Mar 28, 2019
5ba3760
Merge remote-tracking branch 'upstream/master' into gatsby/asset-prefix
DSchau Apr 30, 2019
e3f8daa
chore: remove eslint comment
DSchau Apr 30, 2019
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.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"globals": {
"before": true,
"spyOn": true,
"__PATH_PREFIX__": true
"__PATH_PREFIX__": true,
"__ASSET_PREFIX__": true
},
"rules": {
"arrow-body-style": [
Expand Down
1 change: 0 additions & 1 deletion packages/gatsby-link/src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/*global __PATH_PREFIX__ */
import PropTypes from "prop-types"
import React from "react"
import { Link } from "@reach/router"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { navigate } from "gatsby"
import * as catchLinks from "../catch-links"

beforeAll(() => {
global.__PATH_PREFIX__ = ``
global.__BASE_PATH__ = ``
// Set the base URL we will be testing against to http://localhost:8000/blog
window.history.pushState({}, `APP Url`, `${pathPrefix}`)
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Adds <Link> for feed to head creates Link href with path prefix when __PATH_PREFIX__ sets 1`] = `
exports[`Adds <Link> for feed to head creates Link href with asset prefix when __BASE_PATH__ is set 1`] = `
[MockFunction] {
"calls": Array [
Array [
Expand Down
10 changes: 5 additions & 5 deletions packages/gatsby-plugin-feed/src/__tests__/gatsby-ssr.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const { onRenderBody } = require(`../gatsby-ssr`)

const defaultPathPrefix = global.__PATH_PREFIX__
const defaultPathPrefix = global.__BASE_PATH__

describe(`Adds <Link> for feed to head`, () => {
beforeEach(() => {
global.__PATH_PREFIX__ = ``
global.__BASE_PATH__ = ``
})

afterEach(() => {
global.__PATH_PREFIX__ = defaultPathPrefix
global.__BASE_PATH__ = defaultPathPrefix
})

it(`creates Link if feeds does exist`, async () => {
Expand Down Expand Up @@ -54,8 +54,8 @@ describe(`Adds <Link> for feed to head`, () => {
expect(setHeadComponents).toMatchSnapshot()
expect(setHeadComponents).toHaveBeenCalledTimes(1)
})
it(`creates Link href with path prefix when __PATH_PREFIX__ sets`, async () => {
global.__PATH_PREFIX__ = `/hogwarts`
it(`creates Link href with asset prefix when __BASE_PATH__ is set`, async () => {
global.__BASE_PATH__ = `/hogwarts`

const pluginOptions = {
feeds: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const ssrArgs = {

describe(`gatsby-plugin-manifest`, () => {
beforeEach(() => {
global.__BASE_PATH__ = ``
global.__PATH_PREFIX__ = ``
headComponents = []
})
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby/cache-dir/__tests__/static-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ describe(`static-entry sanity checks`, () => {
describe(`static-entry`, () => {
beforeEach(() => {
global.__PATH_PREFIX__ = ``
global.__ASSET_PREFIX__ = ``
})

test(`onPreRenderHTML can be used to replace headComponents`, done => {
Expand Down
5 changes: 3 additions & 2 deletions packages/gatsby/cache-dir/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ const fetchPageResourceMap = () => {
return fetchingPageResourceMapPromise
}

const createJsonURL = jsonName => `${__PATH_PREFIX__}/static/d/${jsonName}.json`
const createJsonURL = jsonName =>
`${__ASSET_PREFIX__}/static/d/${jsonName}.json`
const createComponentUrls = componentChunkName =>
window.___chunkMapping[componentChunkName].map(
chunk => __PATH_PREFIX__ + chunk
chunk => __ASSET_PREFIX__ + chunk
)

const fetchResource = resourceName => {
Expand Down
12 changes: 7 additions & 5 deletions packages/gatsby/cache-dir/static-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ export default (pagePath, callback) => {
scripts,
styles,
pathPrefix: __PATH_PREFIX__,
assetPrefix: __ASSET_PREFIX__,
})

scripts
Expand All @@ -282,13 +283,13 @@ export default (pagePath, callback) => {
as="script"
rel={script.rel}
key={script.name}
href={`${__PATH_PREFIX__}/${script.name}`}
href={`${__ASSET_PREFIX__}/${script.name}`}
/>
)
})

if (page.jsonName in dataPaths) {
const dataPath = `${__PATH_PREFIX__}/static/d/${
const dataPath = `${__ASSET_PREFIX__}/static/d/${
dataPaths[page.jsonName]
}.json`
headComponents.push(
Expand All @@ -315,13 +316,13 @@ export default (pagePath, callback) => {
as="style"
rel={style.rel}
key={style.name}
href={`${__PATH_PREFIX__}/${style.name}`}
href={`${__ASSET_PREFIX__}/${style.name}`}
/>
)
} else {
headComponents.unshift(
<style
data-href={`${__PATH_PREFIX__}/${style.name}`}
data-href={`${__ASSET_PREFIX__}/${style.name}`}
dangerouslySetInnerHTML={{
__html: fs.readFileSync(
join(process.cwd(), `public`, style.name),
Expand Down Expand Up @@ -370,7 +371,7 @@ export default (pagePath, callback) => {
const bodyScripts = scripts
.filter(s => s.rel !== `prefetch`)
.map(s => {
const scriptPath = `${__PATH_PREFIX__}/${JSON.stringify(s.name).slice(
const scriptPath = `${__ASSET_PREFIX__}/${JSON.stringify(s.name).slice(
1,
-1
)}`
Expand All @@ -388,6 +389,7 @@ export default (pagePath, callback) => {
replacePostBodyComponents,
pathname: pagePath,
pathPrefix: __PATH_PREFIX__,
assetPrefix: __ASSET_PREFIX__,
})

const html = `<!DOCTYPE html>${renderToStaticMarkup(
Expand Down
4 changes: 4 additions & 0 deletions packages/gatsby/src/commands/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const bootstrap = require(`../bootstrap`)
const apiRunnerNode = require(`../utils/api-runner-node`)
const { copyStaticDirs } = require(`../utils/get-static-dir`)
const { initTracer, stopTracer } = require(`../utils/tracer`)
const { getAssets } = require(`../utils/asset-path-registry`)
const db = require(`../db`)
const chalk = require(`chalk`)
const tracer = require(`opentracing`).globalTracer()
Expand Down Expand Up @@ -92,9 +93,12 @@ module.exports = async function build(program: BuildArgs) {
}
activity.end()

const assets = await getAssets(program.directory)

await apiRunnerNode(`onPostBuild`, {
graphql: graphqlRunner,
parentSpan: buildSpan,
assets: Array.from(assets),
})

report.info(`Done building in ${process.uptime()} sec`)
Expand Down
11 changes: 7 additions & 4 deletions packages/gatsby/src/joi-schemas/joi.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ const stripTrailingSlash = chain => chain.replace(/(\w)\/+$/, `$1`)
export const gatsbyConfigSchema = Joi.object().keys({
__experimentalThemes: Joi.array(),
polyfill: Joi.boolean(),
assetPrefix: stripTrailingSlash(Joi.string().uri()),
siteMetadata: Joi.object({
siteUrl: stripTrailingSlash(Joi.string()).uri(),
}).unknown(),
pathPrefix: Joi.string().uri({
allowRelative: true,
relativeOnly: true,
}),
pathPrefix: stripTrailingSlash(
Joi.string().uri({
allowRelative: true,
relativeOnly: true,
})
),
mapping: Joi.object(),
plugins: Joi.array(),
proxy: Joi.object().keys({
Expand Down
13 changes: 5 additions & 8 deletions packages/gatsby/src/query/query-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const { store } = require(`../redux`)
const withResolverContext = require(`../schema/context`)
const { generatePathChunkName } = require(`../utils/js-chunk-names`)
const { formatErrorDetails } = require(`./utils`)
const { register } = require(`../utils/asset-path-registry`)
const mod = require(`hash-mod`)(999)
const { boundActionCreators } = require(`../redux/actions`)

Expand Down Expand Up @@ -114,14 +115,10 @@ ${formatErrorDetails(errorDetails)}`)
}

// Always write file to public/static/d/ folder.
const resultPath = path.join(
program.directory,
`public`,
`static`,
`d`,
modInt,
`${dataPath}.json`
)
const filePath = path.join(`static`, `d`, modInt, `${dataPath}.json`)
const resultPath = path.join(program.directory, `public`, filePath)

register(filePath)

if (queryJob.isPage) {
dataPath = `${modInt}/${dataPath}`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ exports[`config reducer Validates configs with unsupported options 1`] = `"The s

exports[`config reducer handles empty configs 1`] = `
Object {
"assetPrefix": "",
"pathPrefix": "",
"polyfill": true,
}
`;

exports[`config reducer let's you add a config 1`] = `
Object {
"assetPrefix": "",
"pathPrefix": "",
"polyfill": true,
"siteMetadata": Object {
Expand Down
6 changes: 6 additions & 0 deletions packages/gatsby/src/redux/reducers/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ module.exports = (state = {}, action) => {
action = _.set(action, [`payload`, `pathPrefix`], ``)
}

// If pathPrefix isn't set, set it to an empty string
// to avoid it showing up as undefined elsewhere.
if (!_.has(action, [`payload`, `assetPrefix`])) {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure we need this.

action = _.set(action, [`payload`, `assetPrefix`], ``)
}

// Default polyfill to true.
if (!_.has(action, [`payload`, `polyfill`])) {
action = _.set(action, [`payload`, `polyfill`], true)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`getAssets returns webpack assets 1`] = `
Array [
"webpack-runtime-f08922a061a7e09baa26.js",
"webpack-runtime-f08922a061a7e09baa26.js.map",
"app-4b433d29b46598f0ecd3.js",
"app-4b433d29b46598f0ecd3.js.map",
"component---node-modules-gatsby-plugin-offline-app-shell-js-b1a75793f38b5d3df59f.js",
"component---node-modules-gatsby-plugin-offline-app-shell-js-b1a75793f38b5d3df59f.js.map",
"0.2b5955a752ed0afe77ce.css",
"0-fecec88f7d9d61b9013a.js",
"0-fecec88f7d9d61b9013a.js.map",
"1-b047c021df576f0acceb.js",
"1-b047c021df576f0acceb.js.map",
"2.fd4497ec960200ce8103.css",
"2-5ce6e769c43c7185e581.js",
"2-5ce6e769c43c7185e581.js.map",
"3-acdb3c000e2ba39d41ef.js",
"3-acdb3c000e2ba39d41ef.js.map",
"component---src-templates-template-blog-list-js-e53084eb5bee3f6f7f3e.js",
"component---src-templates-template-blog-list-js-e53084eb5bee3f6f7f3e.js.map",
"40-cfb9271892b3c6f32cb7.js",
"40-cfb9271892b3c6f32cb7.js.map",
"component---src-templates-template-blog-post-js-7b76cbf47a3d2298e372.js",
"component---src-templates-template-blog-post-js-7b76cbf47a3d2298e372.js.map",
"component---src-templates-tags-js-596abb26f29b385da04f.js",
"component---src-templates-tags-js-596abb26f29b385da04f.js.map",
"component---src-templates-template-starter-page-js-3ffff961577834cf4023.js",
"component---src-templates-template-starter-page-js-3ffff961577834cf4023.js.map",
"component---src-templates-template-contributor-page-js-f9404cf60eca8d73fa02.js",
"component---src-templates-template-contributor-page-js-f9404cf60eca8d73fa02.js.map",
"component---src-templates-template-creator-details-js-88ef970e91e0b78c2f36.js",
"component---src-templates-template-creator-details-js-88ef970e91e0b78c2f36.js.map",
"43-53d7dbdaf8c4c4002036.js",
"43-53d7dbdaf8c4c4002036.js.map",
"component---src-templates-template-showcase-details-js-f5e8e7b262e87f30a837.js",
"component---src-templates-template-showcase-details-js-f5e8e7b262e87f30a837.js.map",
"component---src-templates-template-docs-markdown-js-beed50a1f7dd4d27b797.js",
"component---src-templates-template-docs-markdown-js-beed50a1f7dd4d27b797.js.map",
"4-a765cc45c3be4b896d6f.js",
"4-a765cc45c3be4b896d6f.js.map",
"6-877482b381b70a3e1930.js",
"6-877482b381b70a3e1930.js.map",
"component---src-templates-template-docs-local-packages-js.fd4497ec960200ce8103.css",
"component---src-templates-template-docs-local-packages-js-c53e4baf78ad21580235.js",
"component---src-templates-template-docs-local-packages-js-c53e4baf78ad21580235.js.map",
"component---src-templates-template-docs-remote-packages-js.fd4497ec960200ce8103.css",
"component---src-templates-template-docs-remote-packages-js-68b7685d85bd50607d49.js",
"component---src-templates-template-docs-remote-packages-js-68b7685d85bd50607d49.js.map",
"component---src-pages-404-js-c31dd87a2028cf26b810.js",
"component---src-pages-404-js-c31dd87a2028cf26b810.js.map",
"component---src-pages-blog-tags-js-46843cd191f6d9a81483.js",
"component---src-pages-blog-tags-js-46843cd191f6d9a81483.js.map",
"5-5f0403c3cf66cc31c4fb.js",
"5-5f0403c3cf66cc31c4fb.js.map",
"component---src-pages-community-agencies-js-442a248e84f48ec18e11.js",
"component---src-pages-community-agencies-js-442a248e84f48ec18e11.js.map",
"component---src-pages-community-companies-js-4a9bb59d522d16bc0cd5.js",
"component---src-pages-community-companies-js-4a9bb59d522d16bc0cd5.js.map",
"component---src-pages-community-index-js-3c9cc70d04f424db3ffa.js",
"component---src-pages-community-index-js-3c9cc70d04f424db3ffa.js.map",
"component---src-pages-community-people-js-26220872f1564524ef20.js",
"component---src-pages-community-people-js-26220872f1564524ef20.js.map",
"component---src-pages-docs-actions-js-af5b12753a00718765fd.js",
"component---src-pages-docs-actions-js-af5b12753a00718765fd.js.map",
"component---src-pages-docs-browser-apis-js-f9529c1875808d214471.js",
"component---src-pages-docs-browser-apis-js-f9529c1875808d214471.js.map",
"component---src-pages-docs-index-js-c7323417d5798f1bcd94.js",
"component---src-pages-docs-index-js-c7323417d5798f1bcd94.js.map",
"component---src-pages-docs-node-apis-js-18a7ea5c808fce0d9402.js",
"component---src-pages-docs-node-apis-js-18a7ea5c808fce0d9402.js.map",
"component---src-pages-docs-ssr-apis-js-c4b332ac75f8093bdf42.js",
"component---src-pages-docs-ssr-apis-js-c4b332ac75f8093bdf42.js.map",
"component---src-pages-features-js-4a4a3f05f2ed8a3afa03.js",
"component---src-pages-features-js-4a4a3f05f2ed8a3afa03.js.map",
"component---src-pages-newsletter-js-b823f6af0f63d49f5bef.js",
"component---src-pages-newsletter-js-b823f6af0f63d49f5bef.js.map",
"component---src-pages-plugins-js.fd4497ec960200ce8103.css",
"component---src-pages-plugins-js-259ea07a4bf263d315c7.js",
"component---src-pages-plugins-js-259ea07a4bf263d315c7.js.map",
"42-d5c0588bf92953044654.js",
"42-d5c0588bf92953044654.js.map",
"component---src-pages-showcase-js-710ac9cbe8f04ccc5629.js",
"component---src-pages-showcase-js-710ac9cbe8f04ccc5629.js.map",
"39-72be356c49b30af6ec24.js",
"39-72be356c49b30af6ec24.js.map",
"component---src-pages-starters-js-5cea20767c8766bdcda4.js",
"component---src-pages-starters-js-5cea20767c8766bdcda4.js.map",
"component---src-pages-ecosystem-js-633f4f4caa8ef50afcbe.js",
"component---src-pages-ecosystem-js-633f4f4caa8ef50afcbe.js.map",
"component---src-pages-index-js-0a2a452fc816778a6329.js",
"component---src-pages-index-js-0a2a452fc816778a6329.js.map",
]
`;
Loading