@teleporthq/teleport-project-plugin-styled-components 0.21.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/next.ts ADDED
@@ -0,0 +1,96 @@
1
+ import {
2
+ FileType,
3
+ ProjectPluginStructure,
4
+ ReactNativeStyleVariation,
5
+ } from '@teleporthq/teleport-types'
6
+ import { createStyleSheetPlugin } from '@teleporthq/teleport-plugin-react-styled-components'
7
+ import importStatementsPlugin from '@teleporthq/teleport-plugin-import-statements'
8
+ import prettierJS from '@teleporthq/teleport-postprocessor-prettier-js'
9
+ import MagicString from 'magic-string'
10
+ import { STYLED_DEPENDENCIES } from './constant'
11
+
12
+ export const nextBeforeModifier = async (structure: ProjectPluginStructure) => {
13
+ const { strategy } = structure
14
+
15
+ if (strategy.id !== 'teleport-project-next') {
16
+ throw new Error('Plugin can be used only with teleport-project-next')
17
+ }
18
+
19
+ strategy.style = ReactNativeStyleVariation.StyledComponents
20
+ if (strategy?.projectStyleSheet?.generator) {
21
+ strategy.projectStyleSheet.plugins = [createStyleSheetPlugin(), importStatementsPlugin]
22
+ strategy.projectStyleSheet.postprocessors = [prettierJS]
23
+ strategy.framework.config.isGlobalStylesDependent = false
24
+ }
25
+ }
26
+
27
+ export const nextAfterModifier = async (structure: ProjectPluginStructure) => {
28
+ const { files, dependencies } = structure
29
+
30
+ if (!files.get('entry')) {
31
+ throw new Error(`Entry File is missing from the generated project, _document file not found`)
32
+ }
33
+
34
+ const fileContent = files.get('entry').files[0].content
35
+ const magicString = new MagicString(fileContent.replace('/n', '//n'))
36
+
37
+ magicString.appendRight(70, `\nimport { ServerStyleSheet } from 'styled-components' \n`)
38
+ magicString.appendRight(
39
+ 114,
40
+ `\nstatic getInitialProps({ renderPage }) {
41
+ const sheet = new ServerStyleSheet();
42
+ const page = renderPage((App) => (props) =>
43
+ sheet.collectStyles(<App {...props} />),
44
+ );
45
+ const styleTags = sheet.getStyleElement();
46
+
47
+ return { ...page, styleTags };
48
+ }\n\n`
49
+ )
50
+ magicString.appendRight(175, `\n{this.props.styleTags}`)
51
+
52
+ const formattedCode = prettierJS({
53
+ [FileType.JS]: magicString.toString(),
54
+ })
55
+
56
+ const babelRc = `{
57
+ "presets": [
58
+ "next/babel"
59
+ ],
60
+ "plugins": [
61
+ [
62
+ "styled-components",
63
+ {
64
+ "ssr": true,
65
+ "displayName": true,
66
+ "preprocess": false
67
+ }
68
+ ]
69
+ ]
70
+ }`
71
+
72
+ files.set('entry', {
73
+ path: ['pages'],
74
+ files: [
75
+ {
76
+ name: '_document',
77
+ fileType: FileType.JS,
78
+ content: formattedCode[FileType.JS],
79
+ },
80
+ ],
81
+ })
82
+
83
+ files.set('.babelrc', {
84
+ path: [],
85
+ files: [
86
+ {
87
+ name: '.babelrc',
88
+ content: babelRc,
89
+ },
90
+ ],
91
+ })
92
+
93
+ Object.keys(STYLED_DEPENDENCIES).forEach((dep: string) => {
94
+ dependencies[dep] = STYLED_DEPENDENCIES[dep]
95
+ })
96
+ }
package/src/react.ts ADDED
@@ -0,0 +1,31 @@
1
+ import { ProjectPluginStructure, ReactStyleVariation } from '@teleporthq/teleport-types'
2
+ import {
3
+ createStyleSheetPlugin,
4
+ createReactStyledComponentsPlugin,
5
+ } from '@teleporthq/teleport-plugin-react-styled-components'
6
+ import importStatementsPlugin from '@teleporthq/teleport-plugin-import-statements'
7
+ import reactAppRoutingPlugin from '@teleporthq/teleport-plugin-react-app-routing'
8
+ import { STYLED_DEPENDENCIES } from './constant'
9
+
10
+ export const reactBeforeModifier = async (structure: ProjectPluginStructure) => {
11
+ const { strategy } = structure
12
+
13
+ strategy.style = ReactStyleVariation.StyledComponents
14
+ if (strategy?.projectStyleSheet?.generator) {
15
+ strategy.projectStyleSheet.plugins = [createStyleSheetPlugin(), importStatementsPlugin]
16
+ strategy.router = {
17
+ ...strategy.router,
18
+ plugins: [createReactStyledComponentsPlugin(), reactAppRoutingPlugin, importStatementsPlugin],
19
+ }
20
+ if (strategy.framework?.config) {
21
+ strategy.framework.config.isGlobalStylesDependent = false
22
+ }
23
+ }
24
+ }
25
+
26
+ export const reactAfterModifier = async (structure: ProjectPluginStructure) => {
27
+ const { dependencies } = structure
28
+ Object.keys(STYLED_DEPENDENCIES).forEach((dep: string) => {
29
+ dependencies[dep] = STYLED_DEPENDENCIES[dep]
30
+ })
31
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "dist/esm"
5
+ },
6
+ "include": [
7
+ "./src"
8
+ ]
9
+ }