-
Notifications
You must be signed in to change notification settings - Fork 27
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
refactor(app-shell): drop Rollup, pre-compile css module and use a macro to load the styles #2018
Changes from all commits
51a5a9d
0048ea6
b0fc5d2
0bcf5f1
f1d1870
ec4f0f1
8e59d11
fb02977
cb4fa76
5cb00f2
45d365d
f19ff1d
1cf5369
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@commercetools-frontend/mc-scripts': patch | ||
--- | ||
|
||
Include a `postcss.config.js`, which is used by the `postcss-loader` Webpack plugin. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@commercetools-frontend/application-shell': minor | ||
--- | ||
|
||
Build package using `preconstruct`. This is now possible as we don't directly load the `.css` file anymore. Instead, we use `postcss` to compile it and load the styles using a macro. This allows the code to be bundled using Babel. |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,20 +15,15 @@ | |
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"main": "./dist/application-shell-index.cjs.js", | ||
"module": "./dist/application-shell-index.es.js", | ||
"typings": "./dist/typings/index.d.ts", | ||
"types": "./dist/typings/index.d.ts", | ||
"main": "dist/commercetools-frontend-application-shell.cjs.js", | ||
"module": "dist/commercetools-frontend-application-shell.esm.js", | ||
"preconstruct": { | ||
"entrypoints": ["./index.ts", "./test-utils/index.ts"] | ||
}, | ||
"files": ["dist", "test-utils", "package.json", "LICENSE", "README.md"], | ||
"scripts": { | ||
"prepare": "./../../scripts/version.js replace", | ||
"prebuild": "rimraf dist/** test-utils/**", | ||
"build": "yarn build:bundles && yarn build:test-utils && yarn build:typings", | ||
"build:bundles": "cross-env NODE_ENV=production rollup -c ../../rollup.config.js -i ./src/index.ts -d dist", | ||
"build:bundles:watch": "yarn build:bundles -w", | ||
"build:test-utils": "cross-env NODE_ENV=development rollup -c ../../rollup.config.js -i ./src/test-utils/index.ts", | ||
"build:typings": "cross-env tsc -p tsconfig.declarations.json --emitDeclarationOnly --declarationDir dist/typings", | ||
"postbuild:typings": "echo \"export * from '../dist/typings/test-utils';\" > test-utils/index.d.ts" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💯 |
||
"compile-css-modules": "./scripts/compile-styles.js" | ||
}, | ||
"dependencies": { | ||
"@babel/runtime": "7.12.5", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#!/usr/bin/env node | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
const postcss = require('postcss'); | ||
const loadPostCssConfig = require('postcss-load-config'); | ||
|
||
const filesToCompile = ['src/components/navbar/navbar.mod.css']; | ||
|
||
function getCompiledPaths(cssFilePath) { | ||
const fileName = path.basename(cssFilePath); | ||
const [folderPath] = cssFilePath.split(fileName); | ||
const compiledDir = path.join(folderPath, 'compiled'); | ||
const compiledFileName = fileName.replace('.mod', ''); | ||
|
||
if (!fs.existsSync(compiledDir)) { | ||
fs.mkdirSync(compiledDir); | ||
} | ||
|
||
return { | ||
dir: compiledDir, | ||
fileName: compiledFileName, | ||
}; | ||
} | ||
|
||
async function compileCss() { | ||
const { plugins, options } = await loadPostCssConfig(); | ||
|
||
const processor = postcss([ | ||
...plugins, | ||
require('postcss-modules')({ | ||
generateScopedName: '[name]__[local]___[hash:base64:5]', | ||
getJSON: function (cssFilePath, json) { | ||
const compiledPaths = getCompiledPaths(cssFilePath); | ||
|
||
// This file contains the mapping between the class names referenced | ||
// in the components and the compiled CSS selectors. | ||
fs.writeFileSync( | ||
`${path.join(compiledPaths.dir, compiledPaths.fileName)}.json`, | ||
JSON.stringify(json), | ||
{ encoding: 'utf8' } | ||
); | ||
|
||
// This file provides the type declaration for the JSON file created above. | ||
fs.writeFileSync( | ||
`${path.join(compiledPaths.dir, compiledPaths.fileName)}.json.d.ts`, | ||
`/* eslint-disable prettier/prettier */ | ||
declare const styles: ${JSON.stringify(json)}; | ||
export default styles;`, | ||
{ encoding: 'utf8' } | ||
); | ||
}, | ||
}), | ||
]); | ||
|
||
for (const cssPath of filesToCompile) { | ||
const cssFilePath = path.join(__dirname, '..', cssPath); | ||
const compiledPaths = getCompiledPaths(cssFilePath); | ||
const css = fs.readFileSync(cssFilePath, { encoding: 'utf8' }); | ||
const compiled = await processor.process(css, { | ||
...options, | ||
from: cssFilePath, | ||
}); | ||
fs.writeFileSync( | ||
path.join(compiledPaths.dir, compiledPaths.fileName), | ||
compiled.css, | ||
{ encoding: 'utf8' } | ||
); | ||
Comment on lines
+64
to
+68
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will still be fingerprinted etc as it's loaded through webpack, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No really,this script is what we execute independently. |
||
} | ||
} | ||
|
||
compileCss().catch((error) => { | ||
console.error(error); | ||
process.exit(1); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ import type { TApplicationContext } from '@commercetools-frontend/application-sh | |
import type { TAsyncLocaleDataProps } from '@commercetools-frontend/i18n'; | ||
import type { TrackingList } from '../../utils/gtm'; | ||
|
||
import './global-style-imports'; | ||
import '../../track-performance'; | ||
import React from 'react'; | ||
import { Router } from 'react-router-dom'; | ||
|
@@ -21,6 +20,7 @@ import Authenticated from '../authenticated'; | |
import GtmBooter from '../gtm-booter'; | ||
import ApplicationLoader from '../application-loader'; | ||
import ErrorBoundary from '../error-boundary'; | ||
import GlobalStyles from './global-styles'; | ||
import { getBrowserLocale } from './utils'; | ||
import useCoercedEnvironmentValues from './use-coerced-environment-values'; | ||
|
||
|
@@ -47,46 +47,49 @@ const ApplicationShellProvider = <AdditionalEnvironmentProperties extends {}>( | |
); | ||
const browserLocale = getBrowserLocale(window); | ||
return ( | ||
<ErrorBoundary> | ||
<ApplicationContextProvider<AdditionalEnvironmentProperties> | ||
environment={coercedEnvironmentValues} | ||
> | ||
<ReduxProvider store={internalReduxStore}> | ||
<ApolloProvider client={apolloClient}> | ||
<React.Suspense fallback={<ApplicationLoader />}> | ||
<Router history={ApplicationShellProvider.history}> | ||
<GtmBooter trackingEventList={props.trackingEventList || {}}> | ||
<Authenticated | ||
locale={browserLocale} | ||
applicationMessages={props.applicationMessages} | ||
render={({ isAuthenticated }) => { | ||
if (isAuthenticated) | ||
return props.children({ isAuthenticated }); | ||
<> | ||
<GlobalStyles /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Injected using Emotion |
||
<ErrorBoundary> | ||
<ApplicationContextProvider<AdditionalEnvironmentProperties> | ||
environment={coercedEnvironmentValues} | ||
> | ||
<ReduxProvider store={internalReduxStore}> | ||
<ApolloProvider client={apolloClient}> | ||
<React.Suspense fallback={<ApplicationLoader />}> | ||
<Router history={ApplicationShellProvider.history}> | ||
<GtmBooter trackingEventList={props.trackingEventList || {}}> | ||
<Authenticated | ||
locale={browserLocale} | ||
applicationMessages={props.applicationMessages} | ||
render={({ isAuthenticated }) => { | ||
if (isAuthenticated) | ||
return props.children({ isAuthenticated }); | ||
|
||
return ( | ||
<AsyncLocaleData | ||
locale={browserLocale} | ||
applicationMessages={props.applicationMessages} | ||
> | ||
{({ locale, messages }) => ( | ||
<ConfigureIntlProvider | ||
locale={locale} | ||
messages={messages} | ||
> | ||
{props.children({ isAuthenticated })} | ||
</ConfigureIntlProvider> | ||
)} | ||
</AsyncLocaleData> | ||
); | ||
}} | ||
/> | ||
</GtmBooter> | ||
</Router> | ||
</React.Suspense> | ||
</ApolloProvider> | ||
</ReduxProvider> | ||
</ApplicationContextProvider> | ||
</ErrorBoundary> | ||
return ( | ||
<AsyncLocaleData | ||
locale={browserLocale} | ||
applicationMessages={props.applicationMessages} | ||
> | ||
{({ locale, messages }) => ( | ||
<ConfigureIntlProvider | ||
locale={locale} | ||
messages={messages} | ||
> | ||
{props.children({ isAuthenticated })} | ||
</ConfigureIntlProvider> | ||
)} | ||
</AsyncLocaleData> | ||
); | ||
}} | ||
/> | ||
</GtmBooter> | ||
</Router> | ||
</React.Suspense> | ||
</ApolloProvider> | ||
</ReduxProvider> | ||
</ApplicationContextProvider> | ||
</ErrorBoundary> | ||
</> | ||
); | ||
}; | ||
|
||
|
This file was deleted.
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.
The
test-utils
is now a preconstruct entrypoint.