@startupjs/bundler 0.62.0-alpha.6 → 0.63.0-alpha.0
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/lib/serverLoader.js +24 -0
- package/nodeLoader.mjs +3 -3
- package/package.json +6 -6
- package/lib/eliminatorLoader.js +0 -198
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// TODO: add support for source maps
|
|
2
|
+
const babel = require('@babel/core')
|
|
3
|
+
const { isStartupjsPluginEcosystemFile } = require('babel-preset-startupjs/utils')
|
|
4
|
+
|
|
5
|
+
module.exports = function serverLoader (source) {
|
|
6
|
+
const filename = this.resourcePath
|
|
7
|
+
|
|
8
|
+
// ensure that this loader is only used on plugins, startupjs.config.js
|
|
9
|
+
// and loadStartupjsConfig.js files.
|
|
10
|
+
if (!isStartupjsPluginEcosystemFile(filename)) return source
|
|
11
|
+
|
|
12
|
+
const envs = this.query.envs
|
|
13
|
+
if (!envs) throw Error("serverLoader: envs not provided (for example ['features', 'isomorphic', 'server'])")
|
|
14
|
+
|
|
15
|
+
return babel.transformSync(source, {
|
|
16
|
+
filename,
|
|
17
|
+
babelrc: false,
|
|
18
|
+
configFile: false,
|
|
19
|
+
presets: [[require('babel-preset-startupjs/server'), {
|
|
20
|
+
envs,
|
|
21
|
+
useRequireContext: this.query.useRequireContext
|
|
22
|
+
}]]
|
|
23
|
+
}).code
|
|
24
|
+
}
|
package/nodeLoader.mjs
CHANGED
|
@@ -3,7 +3,7 @@ import { fileURLToPath } from 'node:url'
|
|
|
3
3
|
import { isStartupjsPluginEcosystemFile } from 'babel-preset-startupjs/utils'
|
|
4
4
|
import callLoader from './lib/callLoader.js'
|
|
5
5
|
import yamlLoader from './lib/yamlLoader.js'
|
|
6
|
-
import
|
|
6
|
+
import serverLoader from './lib/serverLoader.js'
|
|
7
7
|
|
|
8
8
|
export function resolve (specifier, context, nextResolve) {
|
|
9
9
|
const { parentURL = null } = context
|
|
@@ -33,11 +33,11 @@ export async function load (url, context, nextLoad) {
|
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
// process
|
|
36
|
+
// process server-side StartupJS transforms for *.plugin.js and startupjs.config.js
|
|
37
37
|
if (isStartupjsPluginEcosystemFile(url)) {
|
|
38
38
|
const filePath = fileURLToPath(url)
|
|
39
39
|
let source = await readFile(filePath, 'utf8')
|
|
40
|
-
source = callLoader(
|
|
40
|
+
source = callLoader(serverLoader, source, filePath, { envs: ['features', 'isomorphic', 'server'] })
|
|
41
41
|
return {
|
|
42
42
|
format: 'module',
|
|
43
43
|
shortCircuit: true,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@startupjs/bundler",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.63.0-alpha.0",
|
|
4
4
|
"description": "Opinionated scripts and configs to develop a react-native-web project",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"publishConfig": {
|
|
@@ -18,13 +18,13 @@
|
|
|
18
18
|
"@babel/core": "^7.9.0",
|
|
19
19
|
"@babel/plugin-syntax-jsx": "^7.0.0",
|
|
20
20
|
"@mdx-js/mdx": "^3.0.0",
|
|
21
|
-
"@startupjs/babel-plugin-eliminator": "^0.
|
|
22
|
-
"@startupjs/babel-plugin-startupjs-plugins": "^0.
|
|
23
|
-
"@startupjs/server": "^0.
|
|
21
|
+
"@startupjs/babel-plugin-eliminator": "^0.63.0-alpha.0",
|
|
22
|
+
"@startupjs/babel-plugin-startupjs-plugins": "^0.63.0-alpha.0",
|
|
23
|
+
"@startupjs/server": "^0.63.0-alpha.0",
|
|
24
24
|
"@svgr/core": "^8.1.0",
|
|
25
25
|
"@svgr/plugin-jsx": "^8.1.0",
|
|
26
26
|
"@svgr/plugin-svgo": "^8.1.0",
|
|
27
|
-
"babel-preset-startupjs": "^0.
|
|
27
|
+
"babel-preset-startupjs": "^0.63.0-alpha.0",
|
|
28
28
|
"connect": "^3.7.0",
|
|
29
29
|
"lodash": "^4.17.20",
|
|
30
30
|
"remark-gfm": "4.0.1"
|
|
@@ -33,5 +33,5 @@
|
|
|
33
33
|
"cssxjs": "*",
|
|
34
34
|
"react-native-svg": "*"
|
|
35
35
|
},
|
|
36
|
-
"gitHead": "
|
|
36
|
+
"gitHead": "729ad04a5d917e24ebde3ac78505dd91d224773c"
|
|
37
37
|
}
|
package/lib/eliminatorLoader.js
DELETED
|
@@ -1,198 +0,0 @@
|
|
|
1
|
-
// TODO: add support for source maps
|
|
2
|
-
const babel = require('@babel/core')
|
|
3
|
-
const { isStartupjsPluginEcosystemFile, CONFIG_FILENAME_REGEX } = require('babel-preset-startupjs/utils')
|
|
4
|
-
|
|
5
|
-
const PLUGIN_KEYS = ['name', 'for', 'order', 'enabled']
|
|
6
|
-
const PROJECT_KEYS = ['plugins', 'modules']
|
|
7
|
-
const ALL_ENVS = ['features', 'isomorphic', 'client', 'server', 'build']
|
|
8
|
-
const MAGIC_IMPORTS = ['startupjs/registry', '@startupjs/registry']
|
|
9
|
-
|
|
10
|
-
module.exports = function eliminatorLoader (source) {
|
|
11
|
-
const filename = this.resourcePath
|
|
12
|
-
|
|
13
|
-
// transform server-only code to client code in model/*.js files
|
|
14
|
-
// - replace aggregation() with aggregationHeader()
|
|
15
|
-
// - remove accessControl() calls
|
|
16
|
-
const clientModel = this.query.clientModel
|
|
17
|
-
|
|
18
|
-
// ensure that this loader is only used on plugins, startupjs.config.js and loadStartupjsConfig.js files.
|
|
19
|
-
// Or on the client it should also run in model/*.js files
|
|
20
|
-
if (!(
|
|
21
|
-
isStartupjsPluginEcosystemFile(filename) ||
|
|
22
|
-
(clientModel && isModelFile(filename, source))
|
|
23
|
-
)) return source // source inside Program is state.file.code
|
|
24
|
-
|
|
25
|
-
const envs = this.query.envs
|
|
26
|
-
if (!envs) throw Error("eliminatorLoader: envs not provided (for example ['features', 'isomorphic', 'client'])")
|
|
27
|
-
|
|
28
|
-
const useRequireContext = this.query.useRequireContext
|
|
29
|
-
|
|
30
|
-
let code = source
|
|
31
|
-
|
|
32
|
-
// Remove code related to other envs
|
|
33
|
-
code = babel.transformSync(code, {
|
|
34
|
-
filename,
|
|
35
|
-
babelrc: false,
|
|
36
|
-
configFile: false,
|
|
37
|
-
plugins: [
|
|
38
|
-
// support JSX syntax
|
|
39
|
-
require('@babel/plugin-syntax-jsx'),
|
|
40
|
-
|
|
41
|
-
// transform pug to jsx. This generates a bunch of new AST nodes
|
|
42
|
-
// (it's important to do this first before any dead code elimination runs)
|
|
43
|
-
[require('cssxjs/babel/plugin-react-pug'), { classAttribute: 'styleName' }],
|
|
44
|
-
|
|
45
|
-
// auto-load startupjs plugins
|
|
46
|
-
// traverse "exports" of package.json and all dependencies to find all startupjs plugins
|
|
47
|
-
// and automatically import them in the main startupjs.config.js file
|
|
48
|
-
[require('@startupjs/babel-plugin-startupjs-plugins'), { useRequireContext }],
|
|
49
|
-
|
|
50
|
-
// run eliminator to remove code targeting other envs.
|
|
51
|
-
// For example, only keep code related to 'client' and 'isomorphic' envs
|
|
52
|
-
// (in which case any code related to 'server' and 'build' envs will be removed)
|
|
53
|
-
[require('@startupjs/babel-plugin-eliminator'), {
|
|
54
|
-
trimObjects: [{
|
|
55
|
-
magicFilenameRegex: CONFIG_FILENAME_REGEX,
|
|
56
|
-
magicExport: 'default',
|
|
57
|
-
targetObjectJsonPath: '$.modules.*',
|
|
58
|
-
ensureOnlyKeys: ALL_ENVS,
|
|
59
|
-
keepKeys: envs
|
|
60
|
-
}, {
|
|
61
|
-
magicFilenameRegex: CONFIG_FILENAME_REGEX,
|
|
62
|
-
magicExport: 'default',
|
|
63
|
-
targetObjectJsonPath: '$.plugins.*',
|
|
64
|
-
ensureOnlyKeys: ALL_ENVS,
|
|
65
|
-
keepKeys: envs
|
|
66
|
-
}, {
|
|
67
|
-
magicFilenameRegex: CONFIG_FILENAME_REGEX,
|
|
68
|
-
magicExport: 'default',
|
|
69
|
-
targetObjectJsonPath: '$',
|
|
70
|
-
// envs on the top level are the alias for '$.modules.startupjs'
|
|
71
|
-
ensureOnlyKeys: [...PROJECT_KEYS, ...ALL_ENVS],
|
|
72
|
-
keepKeys: [...PROJECT_KEYS, ...envs]
|
|
73
|
-
}, {
|
|
74
|
-
functionName: 'createPlugin',
|
|
75
|
-
magicImports: MAGIC_IMPORTS,
|
|
76
|
-
ensureOnlyKeys: [...PLUGIN_KEYS, ...ALL_ENVS],
|
|
77
|
-
keepKeys: [...PLUGIN_KEYS, ...envs]
|
|
78
|
-
}],
|
|
79
|
-
...(clientModel
|
|
80
|
-
? {
|
|
81
|
-
transformFunctionCalls: [{
|
|
82
|
-
// direct named exports of aggregation() within model/*.js files
|
|
83
|
-
// are replaced with aggregationHeader() calls.
|
|
84
|
-
// 'collection' is the filename without extension
|
|
85
|
-
// 'name' is the direct named export const name
|
|
86
|
-
//
|
|
87
|
-
// Example:
|
|
88
|
-
//
|
|
89
|
-
// // in model/games.js
|
|
90
|
-
// export const $$byGameId = aggregation(({ gameId }) => ({ gameId }))
|
|
91
|
-
//
|
|
92
|
-
// will be replaced with:
|
|
93
|
-
//
|
|
94
|
-
// __aggregationHeader({ collection: 'games', name: '$$byGameId' })
|
|
95
|
-
//
|
|
96
|
-
functionName: 'aggregation',
|
|
97
|
-
magicImports: ['startupjs'],
|
|
98
|
-
requirements: {
|
|
99
|
-
argumentsAmount: 1,
|
|
100
|
-
directNamedExportedAsConst: true
|
|
101
|
-
},
|
|
102
|
-
replaceWith: {
|
|
103
|
-
newFunctionNameFromSameImport: '__aggregationHeader',
|
|
104
|
-
newCallArgumentsTemplate: `[
|
|
105
|
-
{
|
|
106
|
-
collection: %%filenameWithoutExtension%%,
|
|
107
|
-
name: %%directNamedExportConstName%%
|
|
108
|
-
}
|
|
109
|
-
]`
|
|
110
|
-
}
|
|
111
|
-
}, {
|
|
112
|
-
// export default inside of aggregation() within a separate model/*.$$myAggregation.js files
|
|
113
|
-
// are replaced with aggregationHeader() calls.
|
|
114
|
-
// Filepath is stripped of the extensions and split into sections (by dots and slashes)
|
|
115
|
-
// 'name' is the last section.
|
|
116
|
-
// 'collection' is the section before it.
|
|
117
|
-
//
|
|
118
|
-
// Example:
|
|
119
|
-
//
|
|
120
|
-
// // in model/games/$$active.js
|
|
121
|
-
// export default aggregation(({ gameId }) => ({ gameId }))
|
|
122
|
-
//
|
|
123
|
-
// will be replaced with:
|
|
124
|
-
//
|
|
125
|
-
// __aggregationHeader({ collection: 'games', name: '$$active' })
|
|
126
|
-
//
|
|
127
|
-
functionName: 'aggregation',
|
|
128
|
-
magicImports: ['startupjs'],
|
|
129
|
-
requirements: {
|
|
130
|
-
argumentsAmount: 1,
|
|
131
|
-
directDefaultExported: true
|
|
132
|
-
},
|
|
133
|
-
replaceWith: {
|
|
134
|
-
newFunctionNameFromSameImport: '__aggregationHeader',
|
|
135
|
-
newCallArgumentsTemplate: `[
|
|
136
|
-
{
|
|
137
|
-
collection: %%folderAndFilenameWithoutExtension%%.split(/[\\\\/\\.]/).at(-2),
|
|
138
|
-
name: %%folderAndFilenameWithoutExtension%%.split(/[\\\\/\\.]/).at(-1)
|
|
139
|
-
}
|
|
140
|
-
]`
|
|
141
|
-
}
|
|
142
|
-
}, {
|
|
143
|
-
// TODO: this has to be implemented! It's not actually working yet.
|
|
144
|
-
|
|
145
|
-
// any other calls to aggregation() must explicitly define the collection and name
|
|
146
|
-
// as the second argument. If not, the build will fail.
|
|
147
|
-
//
|
|
148
|
-
// Example:
|
|
149
|
-
//
|
|
150
|
-
// aggregation(
|
|
151
|
-
// ({ gameId }) => ({ gameId }),
|
|
152
|
-
// { collection: 'games', name: 'byGameId' }
|
|
153
|
-
// )
|
|
154
|
-
//
|
|
155
|
-
// will be replaced with:
|
|
156
|
-
//
|
|
157
|
-
// __aggregationHeader({ collection: 'games', name: 'byGameId' })
|
|
158
|
-
//
|
|
159
|
-
functionName: 'aggregation',
|
|
160
|
-
magicImports: ['startupjs'],
|
|
161
|
-
requirements: {
|
|
162
|
-
argumentsAmount: 2
|
|
163
|
-
},
|
|
164
|
-
throwIfRequirementsNotMet: true,
|
|
165
|
-
replaceWith: {
|
|
166
|
-
newFunctionNameFromSameImport: '__aggregationHeader',
|
|
167
|
-
newCallArgumentsTemplate: '[%%argument1%%]' // 0-based index
|
|
168
|
-
}
|
|
169
|
-
}, {
|
|
170
|
-
// remove accessControl() calls (replace with undefined)
|
|
171
|
-
functionName: 'accessControl',
|
|
172
|
-
magicImports: ['startupjs'],
|
|
173
|
-
replaceWith: {
|
|
174
|
-
remove: true // replace the whole function call with undefined
|
|
175
|
-
}
|
|
176
|
-
}, {
|
|
177
|
-
// remove serverOnly() calls (replace with undefined)
|
|
178
|
-
functionName: 'serverOnly',
|
|
179
|
-
magicImports: ['startupjs'],
|
|
180
|
-
replaceWith: {
|
|
181
|
-
remove: true // replace the whole function call with undefined
|
|
182
|
-
}
|
|
183
|
-
}]
|
|
184
|
-
}
|
|
185
|
-
: {}
|
|
186
|
-
)
|
|
187
|
-
}]
|
|
188
|
-
]
|
|
189
|
-
}).code
|
|
190
|
-
|
|
191
|
-
return code
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
const MODEL_FILE_REGEX = /(?:^|[.\\/])model[\\/].*\.[mc]?[jt]sx?$/
|
|
195
|
-
const STARTUPJS_REGEX = /['"]startupjs['"]/
|
|
196
|
-
function isModelFile (filename, code) {
|
|
197
|
-
return MODEL_FILE_REGEX.test(filename) && STARTUPJS_REGEX.test(code)
|
|
198
|
-
}
|