@startupjs/bundler 0.56.0-alpha.0 → 0.56.0-alpha.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.
@@ -1,17 +1,20 @@
1
+ // TODO: add support for source maps
1
2
  const babel = require('@babel/core')
3
+ const { isStartupjsPluginEcosystemFile, CONFIG_FILENAME_REGEX } = require('./utils')
2
4
 
3
5
  const PLUGIN_KEYS = ['name', 'for']
6
+ const PROJECT_KEYS = ['plugins', 'modules']
4
7
  const ALL_ENVS = ['client', 'isomorphic', 'server', 'build']
5
8
  const MAGIC_IMPORTS = ['startupjs/registry', '@startupjs/registry']
6
- const MAGIC_IMPORTS_REGEX = /['"]@?startupjs\/registry['"]/
7
9
 
8
10
  module.exports = function eliminatorLoader (source) {
9
- // ensure that this loader is only used when the magic import is present
10
- if (!MAGIC_IMPORTS_REGEX.test(source)) return source
11
+ const filename = this.resourcePath
12
+
13
+ // ensure that this loader is only used on plugins, startupjs.config.js and loadStartupjsConfig.js files
14
+ if (!(isStartupjsPluginEcosystemFile(filename))) return source
11
15
 
12
16
  const envs = this.query.envs
13
17
  if (!envs) throw Error("eliminatorLoader: envs not provided (for example ['client', 'isomorphic'])")
14
- const filename = this.resourcePath
15
18
 
16
19
  let code = source
17
20
 
@@ -50,19 +53,31 @@ module.exports = function eliminatorLoader (source) {
50
53
  // For example, only keep code related to 'client' and 'isomorphic' envs
51
54
  // (in which case any code related to 'server' and 'build' envs will be removed)
52
55
  [require('@startupjs/babel-plugin-eliminator'), {
53
- keepObjectKeysOfFunction: {
54
- createProject: {
55
- magicImports: MAGIC_IMPORTS,
56
- targetObjectJsonPath: '$.plugins.*',
57
- ensureOnlyKeys: ALL_ENVS,
58
- keepKeys: envs
59
- },
60
- createPlugin: {
61
- magicImports: MAGIC_IMPORTS,
62
- ensureOnlyKeys: [...PLUGIN_KEYS, ...ALL_ENVS],
63
- keepKeys: [...PLUGIN_KEYS, ...envs]
64
- }
65
- }
56
+ trimObjects: [{
57
+ magicFilenameRegex: CONFIG_FILENAME_REGEX,
58
+ magicExport: 'default',
59
+ targetObjectJsonPath: '$.modules.*',
60
+ ensureOnlyKeys: ALL_ENVS,
61
+ keepKeys: envs
62
+ }, {
63
+ magicFilenameRegex: CONFIG_FILENAME_REGEX,
64
+ magicExport: 'default',
65
+ targetObjectJsonPath: '$.plugins.*',
66
+ ensureOnlyKeys: ALL_ENVS,
67
+ keepKeys: envs
68
+ }, {
69
+ magicFilenameRegex: CONFIG_FILENAME_REGEX,
70
+ magicExport: 'default',
71
+ targetObjectJsonPath: '$',
72
+ // envs on the top level are the alias for '$.modules.startupjs'
73
+ ensureOnlyKeys: [...PROJECT_KEYS, ...ALL_ENVS],
74
+ keepKeys: [...PROJECT_KEYS, ...envs]
75
+ }, {
76
+ functionName: 'createPlugin',
77
+ magicImports: MAGIC_IMPORTS,
78
+ ensureOnlyKeys: [...PLUGIN_KEYS, ...ALL_ENVS],
79
+ keepKeys: [...PLUGIN_KEYS, ...envs]
80
+ }]
66
81
  }]
67
82
  ]
68
83
  }).code
@@ -3,15 +3,12 @@
3
3
  const platformSingleton = require(
4
4
  '@startupjs/babel-plugin-rn-stylename-inline/platformSingleton'
5
5
  )
6
- const fs = require('fs')
7
- const path = require('path')
6
+ const { existsSync } = require('fs')
7
+ const { join } = require('path')
8
8
  const stylus = require('stylus')
9
- const STYLES_PATH = path.join(process.cwd(), 'styles/index.styl')
10
9
 
10
+ const PROJECT_STYLES_PATH = join(process.cwd(), 'styles/index.styl')
11
11
  let UI_STYLES_PATH
12
- try {
13
- UI_STYLES_PATH = require.resolve('@startupjs/ui/styles/index.styl')
14
- } catch (err) {}
15
12
 
16
13
  function renderToCSS (src, filename) {
17
14
  let compiled
@@ -24,13 +21,13 @@ function renderToCSS (src, filename) {
24
21
  compiler.define(`__${platform.toUpperCase()}__`, true)
25
22
  }
26
23
 
27
- if (fs.existsSync(UI_STYLES_PATH)) {
24
+ if (checkUiStylesExist()) {
28
25
  compiler.import(UI_STYLES_PATH)
29
26
  }
30
27
 
31
28
  // TODO: Make this a setting
32
- if (fs.existsSync(STYLES_PATH)) {
33
- compiler.import(STYLES_PATH)
29
+ if (checkProjectStylesExist()) {
30
+ compiler.import(PROJECT_STYLES_PATH)
34
31
  }
35
32
 
36
33
  compiler.render(function (err, res) {
@@ -46,3 +43,28 @@ function renderToCSS (src, filename) {
46
43
  module.exports = function stylusToReactNative (source) {
47
44
  return renderToCSS(source, this.resourcePath)
48
45
  }
46
+
47
+ // check if @startupjs/ui is being used to load styles file from it, cache result for 5 seconds
48
+ let uiStylesExist
49
+ let uiStylesLastChecked = 0
50
+ function checkUiStylesExist () {
51
+ if (uiStylesLastChecked + 5000 > Date.now()) return uiStylesExist
52
+ uiStylesLastChecked = Date.now()
53
+ try {
54
+ UI_STYLES_PATH = join(require.resolve('@startupjs/ui'), '../styles/index.styl')
55
+ uiStylesExist = existsSync(UI_STYLES_PATH)
56
+ } catch {
57
+ uiStylesExist = false
58
+ }
59
+ return uiStylesExist
60
+ }
61
+
62
+ // check if project styles file exist, cache result for 5 seconds
63
+ let projectStylesExist
64
+ let projectStylesLastChecked = 0
65
+ function checkProjectStylesExist () {
66
+ if (projectStylesLastChecked + 5000 > Date.now()) return projectStylesExist
67
+ projectStylesLastChecked = Date.now()
68
+ projectStylesExist = existsSync(PROJECT_STYLES_PATH)
69
+ return projectStylesExist
70
+ }
package/lib/utils.js ADDED
@@ -0,0 +1,12 @@
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
+
6
+ exports.isStartupjsPluginEcosystemFile = filename => {
7
+ return (
8
+ exports.PLUGIN_FILENAME_REGEX.test(filename) ||
9
+ exports.CONFIG_FILENAME_REGEX.test(filename) ||
10
+ exports.LOAD_CONFIG_FILENAME_REGEX.test(filename)
11
+ )
12
+ }
@@ -31,7 +31,7 @@ module.exports.transform = async function startupjsMetroBabelTransform ({
31
31
  }
32
32
 
33
33
  // js transformations
34
- if (/(?:[./]plugin\.[mc]?[jt]sx?|startupjs\.config\.[mc]?[jt]sx?)$/.test(filename)) {
34
+ if (/\.[mc]?[jt]sx?$/.test(filename)) {
35
35
  src = callLoader(eliminatorLoader, src, filename, { envs: ['client', 'isomorphic'] })
36
36
  }
37
37
  if ((/\.mdx?$/.test(filename) || /\.[mc]?[jt]sx?$/.test(filename)) && /['"](?:startupjs|@env)['"]/.test(src)) {
package/metro-config.js CHANGED
@@ -1,3 +1,5 @@
1
+ const connect = require('connect')
2
+
1
3
  // To pass existing config for modification, pass it as 'upstreamConfig' in options:
2
4
  // config = getDefaultConfig(__dirname, { upstreamConfig })
3
5
  exports.getDefaultConfig = function getDefaultConfig (projectRoot, { upstreamConfig } = {}) {
@@ -16,7 +18,20 @@ exports.getDefaultConfig = function getDefaultConfig (projectRoot, { upstreamCon
16
18
  ...(isExpo ? ['expo.ts', 'expo.tsx', 'expo.js', 'expo.jsx', 'expo.mjs', 'expo.cjs'] : []),
17
19
  ...(upstreamConfig.resolver.sourceExts || []),
18
20
  ...['mjs', 'cjs', 'md', 'mdx', 'css', 'styl', 'svg']
19
- ]))
21
+ ])),
22
+ unstable_enablePackageExports: true
23
+ },
24
+ server: {
25
+ ...upstreamConfig.server,
26
+ // TODO: implement a simple parsing of startupjs.config.js
27
+ // to figure out whether we need to load 'server' and 'isomorphic'
28
+ // envs or only the 'build' one.
29
+ // If $.isomorphic.server is turned off then we don't need the enhanceMiddleware at all
30
+ enhanceMiddleware: metroMiddleware => {
31
+ return connect()
32
+ .use(metroMiddleware)
33
+ .use(getStartupjsMiddleware())
34
+ }
20
35
  }
21
36
  }
22
37
  }
@@ -50,3 +65,24 @@ function checkIfExpo (upstreamConfig) {
50
65
  return false
51
66
  }
52
67
  }
68
+
69
+ function getStartupjsMiddleware () {
70
+ const middlewarePromise = (async () => {
71
+ await import('./nodeRegister.mjs')
72
+ await import('@startupjs/registry/loadStartupjsConfig.auto')
73
+ const { ROOT_MODULE: MODULE } = await import('@startupjs/registry')
74
+ if (!MODULE.options.server) return
75
+ const { createMiddleware } = await import('@startupjs/server')
76
+ return (await createMiddleware()).middleware
77
+ })()
78
+ return function startupjsMiddleware (req, res, next) {
79
+ (async () => {
80
+ try {
81
+ const middleware = await middlewarePromise
82
+ return middleware ? middleware.apply(this, arguments) : next()
83
+ } catch (err) {
84
+ return next(err)
85
+ }
86
+ })()
87
+ }
88
+ }
package/nodeLoader.mjs CHANGED
@@ -3,6 +3,7 @@ import { fileURLToPath } from 'node:url'
3
3
  import callLoader from './lib/callLoader.js'
4
4
  import yamlLoader from './lib/yamlLoader.js'
5
5
  import eliminatorLoader from './lib/eliminatorLoader.js'
6
+ import { isStartupjsPluginEcosystemFile } from './lib/utils.js'
6
7
 
7
8
  export function resolve (specifier, context, nextResolve) {
8
9
  const { parentURL = null } = context
@@ -33,7 +34,7 @@ export async function load (url, context, nextLoad) {
33
34
  }
34
35
 
35
36
  // process code elimination of other envs for *.plugin.js and startupjs.config.js
36
- if (/(?:[./]plugin\.[mc]?[jt]sx?|startupjs\.config\.js)$/.test(url)) {
37
+ if (isStartupjsPluginEcosystemFile(url)) {
37
38
  const filePath = fileURLToPath(url)
38
39
  let source = await readFile(filePath, 'utf8')
39
40
  source = callLoader(eliminatorLoader, source, filePath, { envs: ['server', 'isomorphic'] })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@startupjs/bundler",
3
- "version": "0.56.0-alpha.0",
3
+ "version": "0.56.0-alpha.2",
4
4
  "description": "Opinionated scripts and configs to develop a react-native-web project",
5
5
  "main": "index.js",
6
6
  "publishConfig": {
@@ -21,12 +21,14 @@
21
21
  "@mdx-js/loader": "^2.3.0",
22
22
  "@mdx-js/mdx": "^2.3.0",
23
23
  "@pmmmwh/react-refresh-webpack-plugin": "^0.5.1",
24
- "@startupjs/babel-plugin-eliminator": "^0.56.0-alpha.0",
24
+ "@startupjs/babel-plugin-eliminator": "^0.56.0-alpha.1",
25
25
  "@startupjs/babel-plugin-react-css-modules": "^6.5.4-1",
26
26
  "@startupjs/babel-plugin-react-pug-classnames": "^0.56.0-alpha.0",
27
- "@startupjs/babel-plugin-startupjs-plugins": "^0.56.0-alpha.0",
27
+ "@startupjs/babel-plugin-startupjs-plugins": "^0.56.0-alpha.1",
28
28
  "@startupjs/css-to-react-native-transform": "^1.9.0-2",
29
29
  "@startupjs/plugin": "^0.56.0-alpha.0",
30
+ "@startupjs/registry": "^0.56.0-alpha.2",
31
+ "@startupjs/server": "^0.56.0-alpha.2",
30
32
  "@svgr/core": "^8.1.0",
31
33
  "@svgr/plugin-jsx": "^8.1.0",
32
34
  "@svgr/plugin-svgo": "^8.1.0",
@@ -35,7 +37,8 @@
35
37
  "autoprefixer": "^10.4.0",
36
38
  "babel-loader": "^8.2.3",
37
39
  "babel-plugin-transform-react-pug": "^7.0.1",
38
- "babel-preset-startupjs": "^0.56.0-alpha.0",
40
+ "babel-preset-startupjs": "^0.56.0-alpha.2",
41
+ "connect": "^3.7.0",
39
42
  "css-loader": "^6.5.0",
40
43
  "css-minimizer-webpack-plugin": "^5.0.0",
41
44
  "file-loader": "^6.2.0",
@@ -58,5 +61,5 @@
58
61
  "peerDependencies": {
59
62
  "react-native-svg": "*"
60
63
  },
61
- "gitHead": "076772b216a09281ed38b32866b576024562f099"
64
+ "gitHead": "97f1ce92de18e3300d02321986936890df822dc2"
62
65
  }