babel-preset-startupjs 0.60.0-canary.0 → 0.60.0-canary.2

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.
Files changed (3) hide show
  1. package/index.js +216 -2
  2. package/package.json +5 -4
  3. package/utils.js +35 -0
package/index.js CHANGED
@@ -1,4 +1,57 @@
1
- module.exports = (api, { platform, env } = {}) => {
1
+ /**
2
+ * Compilation pipeline:
3
+ * 1. Transform pug to jsx
4
+ * 2. Auto-load startupjs plugins
5
+ * 3. Run eliminator to remove code targeting other envs
6
+ * 4. Transform CSS modules
7
+ *
8
+ * Options:
9
+ * platform - Force platform to compile to (e.g. 'ios', 'android', 'web').
10
+ * Default: 'web' (or auto-detected on React Native)
11
+ * On React Native (Metro) this gets automatically detected inside babel plugins.
12
+ * reactType - Force the React type - RN or pure web React (e.g. 'react-native', 'web').
13
+ * Default: undefined (auto-detected)
14
+ * This shouldn't be needed in most cases since it will be automatically detected.
15
+ * cache - Force the CSS caching library instance (e.g. 'teamplay').
16
+ * Default: undefined (auto-detected)
17
+ * This shouldn't be needed in most cases since it will be automatically detected.
18
+ * transformPug - Whether to transform pug to jsx.
19
+ * Default: true
20
+ * transformCss - Whether to transform CSS modules (styl/css files and styl`` css`` in JSX).
21
+ * Default: true
22
+ * useRequireContext - Whether to use require.context for loading startupjs plugins.
23
+ * The underlying environment must support require.context (e.g. Metro, Webpack).
24
+ * Default: true
25
+ * clientOnly - Whether to transform model/*.js files to keep only the client-relevant code.
26
+ * Default: true
27
+ * This option is required when building for the client (React Native or web) so that
28
+ * server-only code is removed from model files and secret information is not leaked to the client.
29
+ * envs - Array of envs to keep during code elimination of the startupjs config and plugins.
30
+ * Default: ['features', 'isomorphic', 'client']
31
+ * On the server, this should usually include 'server' instead of 'client':
32
+ * ['features', 'isomorphic', 'server']
33
+ * isStartupjsFile - A function (filename, code) => boolean that checks whether the given file
34
+ * is part of the startupjs ecosystem (a plugin, startupjs.config.js, loadStartupjsConfig.js or a model file).
35
+ * Default: a function that returns true for all startupjs plugin ecosystem files. And also
36
+ * when clientOnly is true, it also returns true for model/*.js files to keep only client-relevant code there.
37
+ */
38
+ const { createStartupjsFileChecker, CONFIG_FILENAME_REGEX } = require('./utils.js')
39
+ const PLUGIN_KEYS = ['name', 'for', 'order', 'enabled']
40
+ const PROJECT_KEYS = ['plugins', 'modules']
41
+ const ALL_ENVS = ['features', 'isomorphic', 'client', 'server', 'build']
42
+ const MAGIC_IMPORTS = ['startupjs/registry', '@startupjs/registry']
43
+
44
+ module.exports = (api, {
45
+ platform,
46
+ reactType,
47
+ cache,
48
+ transformCss = true,
49
+ transformPug = true,
50
+ useRequireContext = true,
51
+ clientOnly = true,
52
+ envs = ['features', 'isomorphic', 'client'],
53
+ isStartupjsFile = createStartupjsFileChecker({ clientOnly })
54
+ } = {}) => {
2
55
  return {
3
56
  overrides: [{
4
57
  test: isJsxSource,
@@ -22,8 +75,169 @@ module.exports = (api, { platform, env } = {}) => {
22
75
  ]
23
76
  }, {
24
77
  plugins: [
78
+ // transform pug to jsx. This generates a bunch of new AST nodes
79
+ // (it's important to do this first before any dead code elimination runs)
80
+ transformPug && [require('cssxjs/babel/plugin-react-pug'), { classAttribute: 'styleName' }],
81
+
82
+ // inline CSS modules (styl`` in the same JSX file -- similar to how it is in Vue.js)
83
+ transformCss && [require('cssxjs/babel/plugin-rn-stylename-inline'), {
84
+ platform
85
+ }],
86
+ // CSS modules (separate .styl/.css file)
87
+ transformCss && [require('cssxjs/babel/plugin-rn-stylename-to-style'), {
88
+ extensions: ['styl', 'css'],
89
+ useImport: true,
90
+ reactType,
91
+ cache
92
+ }],
93
+
94
+ // auto-load startupjs plugins
95
+ // traverse "exports" of package.json and all dependencies to find all startupjs plugins
96
+ // and automatically import them in the main startupjs.config.js file
97
+ [require('@startupjs/babel-plugin-startupjs-plugins'), { useRequireContext }],
98
+
99
+ // run eliminator to remove code targeting other envs.
100
+ // For example, only keep code related to 'client' and 'isomorphic' envs
101
+ // (in which case any code related to 'server' and 'build' envs will be removed)
102
+ [require('@startupjs/babel-plugin-eliminator'), {
103
+ shouldTransformFileChecker: isStartupjsFile,
104
+ trimObjects: [{
105
+ magicFilenameRegex: CONFIG_FILENAME_REGEX,
106
+ magicExport: 'default',
107
+ targetObjectJsonPath: '$.modules.*',
108
+ ensureOnlyKeys: ALL_ENVS,
109
+ keepKeys: envs
110
+ }, {
111
+ magicFilenameRegex: CONFIG_FILENAME_REGEX,
112
+ magicExport: 'default',
113
+ targetObjectJsonPath: '$.plugins.*',
114
+ ensureOnlyKeys: ALL_ENVS,
115
+ keepKeys: envs
116
+ }, {
117
+ magicFilenameRegex: CONFIG_FILENAME_REGEX,
118
+ magicExport: 'default',
119
+ targetObjectJsonPath: '$',
120
+ // envs on the top level are the alias for '$.modules.startupjs'
121
+ ensureOnlyKeys: [...PROJECT_KEYS, ...ALL_ENVS],
122
+ keepKeys: [...PROJECT_KEYS, ...envs]
123
+ }, {
124
+ functionName: 'createPlugin',
125
+ magicImports: MAGIC_IMPORTS,
126
+ ensureOnlyKeys: [...PLUGIN_KEYS, ...ALL_ENVS],
127
+ keepKeys: [...PLUGIN_KEYS, ...envs]
128
+ }],
129
+ ...(clientOnly
130
+ ? {
131
+ transformFunctionCalls: [{
132
+ // direct named exports of aggregation() within model/*.js files
133
+ // are replaced with aggregationHeader() calls.
134
+ // 'collection' is the filename without extension
135
+ // 'name' is the direct named export const name
136
+ //
137
+ // Example:
138
+ //
139
+ // // in model/games.js
140
+ // export const $$byGameId = aggregation(({ gameId }) => ({ gameId }))
141
+ //
142
+ // will be replaced with:
143
+ //
144
+ // __aggregationHeader({ collection: 'games', name: '$$byGameId' })
145
+ //
146
+ functionName: 'aggregation',
147
+ magicImports: ['startupjs'],
148
+ requirements: {
149
+ argumentsAmount: 1,
150
+ directNamedExportedAsConst: true
151
+ },
152
+ replaceWith: {
153
+ newFunctionNameFromSameImport: '__aggregationHeader',
154
+ newCallArgumentsTemplate: `[
155
+ {
156
+ collection: %%filenameWithoutExtension%%,
157
+ name: %%directNamedExportConstName%%
158
+ }
159
+ ]`
160
+ }
161
+ }, {
162
+ // export default inside of aggregation() within a separate model/*.$$myAggregation.js files
163
+ // are replaced with aggregationHeader() calls.
164
+ // Filepath is stripped of the extensions and split into sections (by dots and slashes)
165
+ // 'name' is the last section.
166
+ // 'collection' is the section before it.
167
+ //
168
+ // Example:
169
+ //
170
+ // // in model/games/$$active.js
171
+ // export default aggregation(({ gameId }) => ({ gameId }))
172
+ //
173
+ // will be replaced with:
174
+ //
175
+ // __aggregationHeader({ collection: 'games', name: '$$active' })
176
+ //
177
+ functionName: 'aggregation',
178
+ magicImports: ['startupjs'],
179
+ requirements: {
180
+ argumentsAmount: 1,
181
+ directDefaultExported: true
182
+ },
183
+ replaceWith: {
184
+ newFunctionNameFromSameImport: '__aggregationHeader',
185
+ newCallArgumentsTemplate: `[
186
+ {
187
+ collection: %%folderAndFilenameWithoutExtension%%.split(/[\\\\/\\.]/).at(-2),
188
+ name: %%folderAndFilenameWithoutExtension%%.split(/[\\\\/\\.]/).at(-1)
189
+ }
190
+ ]`
191
+ }
192
+ }, {
193
+ // TODO: this has to be implemented! It's not actually working yet.
194
+
195
+ // any other calls to aggregation() must explicitly define the collection and name
196
+ // as the second argument. If not, the build will fail.
197
+ //
198
+ // Example:
199
+ //
200
+ // aggregation(
201
+ // ({ gameId }) => ({ gameId }),
202
+ // { collection: 'games', name: 'byGameId' }
203
+ // )
204
+ //
205
+ // will be replaced with:
206
+ //
207
+ // __aggregationHeader({ collection: 'games', name: 'byGameId' })
208
+ //
209
+ functionName: 'aggregation',
210
+ magicImports: ['startupjs'],
211
+ requirements: {
212
+ argumentsAmount: 2
213
+ },
214
+ throwIfRequirementsNotMet: true,
215
+ replaceWith: {
216
+ newFunctionNameFromSameImport: '__aggregationHeader',
217
+ newCallArgumentsTemplate: '[%%argument1%%]' // 0-based index
218
+ }
219
+ }, {
220
+ // remove accessControl() calls (replace with undefined)
221
+ functionName: 'accessControl',
222
+ magicImports: ['startupjs'],
223
+ replaceWith: {
224
+ remove: true // replace the whole function call with undefined
225
+ }
226
+ }, {
227
+ // remove serverOnly() calls (replace with undefined)
228
+ functionName: 'serverOnly',
229
+ magicImports: ['startupjs'],
230
+ replaceWith: {
231
+ remove: true // replace the whole function call with undefined
232
+ }
233
+ }]
234
+ }
235
+ : {}
236
+ )
237
+ }],
238
+
25
239
  // debugging features
26
- env === 'development' && require('@startupjs/babel-plugin-startupjs-debug'),
240
+ require('@startupjs/babel-plugin-startupjs-debug'),
27
241
  require('@startupjs/babel-plugin-i18n-extract')
28
242
  ].filter(Boolean)
29
243
  }]
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "babel-preset-startupjs",
3
- "version": "0.60.0-canary.0",
3
+ "version": "0.60.0-canary.2",
4
4
  "description": "Babel preset for compiling StartupJS app on server, web, native",
5
5
  "main": "index.js",
6
6
  "exports": {
7
- ".": "./index.js"
7
+ ".": "./index.js",
8
+ "./utils": "./utils.js"
8
9
  },
9
10
  "publishConfig": {
10
11
  "access": "public"
@@ -16,7 +17,7 @@
16
17
  "@babel/plugin-syntax-typescript": "^7.23.3",
17
18
  "@startupjs/babel-plugin-i18n-extract": "^0.60.0-canary.0",
18
19
  "@startupjs/babel-plugin-startupjs": "^0.60.0-canary.0",
19
- "@startupjs/babel-plugin-startupjs-debug": "^0.60.0-canary.0"
20
+ "@startupjs/babel-plugin-startupjs-debug": "^0.60.0-canary.2"
20
21
  },
21
- "gitHead": "4c5baa750402d0beb02921a38413620b348bd374"
22
+ "gitHead": "cbea3f3fe0689dfca50dcf5ddf9ef67730a680e2"
22
23
  }
package/utils.js ADDED
@@ -0,0 +1,35 @@
1
+ // NOTE: startupjs.config.cjs is used by the old bundler. This regex does not include it.
2
+ exports.CONFIG_FILENAME_REGEX = /(?:^|[\\/])startupjs\.config\.m?[jt]sx?$/
3
+ exports.PLUGIN_FILENAME_REGEX = /(?:^|[.\\/])plugin\.[mc]?[jt]sx?$/
4
+ exports.LOAD_CONFIG_FILENAME_REGEX = /(?:^|[\\/])loadStartupjsConfig\.m?[jt]sx?$/
5
+ exports.MODEL_FILENAME_REGEX = /(?:^|[.\\/])model[\\/].*\.[mc]?[jt]sx?$/
6
+ const STARTUPJS_FILE_CONTENT_REGEX = /['"]startupjs['"]/
7
+
8
+ exports.isStartupjsPluginEcosystemFile = filename => {
9
+ return (
10
+ exports.PLUGIN_FILENAME_REGEX.test(filename) ||
11
+ exports.CONFIG_FILENAME_REGEX.test(filename) ||
12
+ exports.LOAD_CONFIG_FILENAME_REGEX.test(filename)
13
+ )
14
+ }
15
+
16
+ exports.isModelFile = (filename, code) => {
17
+ return exports.MODEL_FILENAME_REGEX.test(filename) && STARTUPJS_FILE_CONTENT_REGEX.test(code)
18
+ }
19
+
20
+ exports.createStartupjsFileChecker = ({ clientOnly } = {}) => {
21
+ return (filename, code) => {
22
+ if (exports.isStartupjsPluginEcosystemFile(filename)) return true
23
+ if (clientOnly) {
24
+ if (code != null) {
25
+ if (exports.isModelFile(filename, code)) return true
26
+ } else {
27
+ console.warn(
28
+ '[babel-preset-startupjs] File\'s source code must be provided when clientOnly is true. ' +
29
+ 'Assuming false for isModelFile check. Filename:', filename
30
+ )
31
+ }
32
+ }
33
+ return false
34
+ }
35
+ }