@s-ui/bundler 9.43.0-beta.0 → 9.43.0-typescript.1

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.
@@ -67,12 +67,7 @@ const start = async ({
67
67
  } = {}) => {
68
68
  clearConsole()
69
69
  // Warn and crash if required files are missing
70
- if (
71
- !checkRequiredFiles([
72
- path.join(config.context, 'index.html'),
73
- path.join(config.context, 'app.js')
74
- ])
75
- ) {
70
+ if (!checkRequiredFiles([path.join(config.context, 'index.html')])) {
76
71
  log.error(
77
72
  `✖ Required files are missing, create and index.html and app.js inside your src folder.`
78
73
  )
@@ -49,7 +49,7 @@ module.exports = ({config, packagesToLink, linkAll}) => {
49
49
  * if neccesary
50
50
  */
51
51
  const linkLoader = {
52
- test: /\.(jsx?|scss)$/,
52
+ test: /\.(jsx?|tsx?|scss)$/,
53
53
  enforce: 'pre', // this will ensure is execute before transformations
54
54
  use: {
55
55
  loader: require.resolve('./LinkLoader'),
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@s-ui/bundler",
3
- "version": "9.43.0-beta.0",
3
+ "version": "9.43.0-typescript.1",
4
4
  "description": "Config-free bundler for ES6 React apps.",
5
5
  "bin": {
6
6
  "sui-bundler": "./bin/sui-bundler.js"
7
7
  },
8
8
  "main": "./bin/sui-bundler.js",
9
9
  "scripts": {
10
+ "postinstall": "node ./scripts/postinstall.js",
10
11
  "test": "echo \"Error: no test specified\" && exit 1"
11
12
  },
12
13
  "keywords": [],
@@ -21,9 +22,12 @@
21
22
  },
22
23
  "homepage": "https://github.com/SUI-Components/sui/tree/master/packages/sui-bundler#readme",
23
24
  "dependencies": {
24
- "@babel/core": "7.18.10",
25
+ "@babel/core": "7.21.8",
26
+ "@s-ui/compiler-config": "1",
25
27
  "@s-ui/helpers": "1",
26
28
  "@s-ui/sass-loader": "1",
29
+ "@swc/core": "1.3.14",
30
+ "@swc/helpers": "0.4.12",
27
31
  "address": "1.2.0",
28
32
  "autoprefixer": "10.4.8",
29
33
  "babel-loader": "8.2.5",
@@ -34,18 +38,19 @@
34
38
  "css-minimizer-webpack-plugin": "4.0.0",
35
39
  "esbuild": "0.15.5",
36
40
  "escape-string-regexp": "4.0.0",
37
- "fast-glob": "3.2.11",
41
+ "fast-glob": "3.2.12",
38
42
  "find-free-ports": "3.0.0",
39
43
  "html-webpack-plugin": "5.5.0",
40
44
  "https-browserify": "1.0.0",
41
45
  "mini-css-extract-plugin": "2.6.1",
42
- "postcss": "8.4.16",
46
+ "postcss": "8.4.31",
43
47
  "postcss-loader": "7.0.1",
44
48
  "process": "0.11.10",
45
49
  "sass": "1.54.5",
46
50
  "stream-http": "3.2.0",
47
51
  "strip-ansi": "6.0.1",
48
52
  "style-loader": "3.3.1",
53
+ "swc-loader": "0.2.1",
49
54
  "url": "0.11.0",
50
55
  "webpack": "5.82.1",
51
56
  "webpack-dev-server": "4.10.0",
@@ -0,0 +1,52 @@
1
+ #!/usr/bin/env node
2
+ /* eslint-disable no-console */
3
+
4
+ const crypto = require('crypto')
5
+ const fs = require('fs-extra')
6
+ const path = require('path')
7
+ const {writeFile} = require('@s-ui/helpers/file.js')
8
+
9
+ const {INIT_CWD} = process.env
10
+ const tsConfigTemplate = `\
11
+ {
12
+ "extends": "@s-ui/bundler/tsconfig.json",
13
+ "compilerOptions": {
14
+ "rootDir": "./"
15
+ },
16
+ "include": ["src", "domain", "components"]
17
+ }`
18
+
19
+ const md5 = str => crypto.createHash('md5').update(str).digest('hex')
20
+ const TS_CONFIG_PATH = path.join(INIT_CWD, 'tsconfig.json')
21
+ const PACKAGE_JSON_CONFIG_PATH = path.join(INIT_CWD, 'package.json')
22
+
23
+ const config = require(PACKAGE_JSON_CONFIG_PATH)?.config?.['sui-bundler'] || {}
24
+
25
+ const shouldGenerateTSConfig = () => {
26
+ try {
27
+ if (!config?.type || config?.type !== 'typescript') return false
28
+
29
+ if (!fs.existsSync(TS_CONFIG_PATH)) return true
30
+
31
+ const tsConfigLocal = fs.readFileSync(TS_CONFIG_PATH, {encoding: 'utf8'})
32
+ return md5(tsConfigLocal) !== md5(tsConfigTemplate)
33
+ } catch (err) {
34
+ return true
35
+ }
36
+ }
37
+
38
+ async function main() {
39
+ console.log(
40
+ '🔍 [sui-bundler postinstall] Checking if tsconfig.json is up to date...'
41
+ )
42
+ if (!shouldGenerateTSConfig()) {
43
+ console.log('✅ [sui-bundler postinstall] tsconfig.json is up to date')
44
+ process.exit(0)
45
+ }
46
+ await writeFile(TS_CONFIG_PATH, tsConfigTemplate)
47
+ console.log(
48
+ '❌ [sui-bundler postinstall] tsconfig.json was not up to date, so we updated it'
49
+ )
50
+ }
51
+
52
+ main()
package/shared/define.js CHANGED
@@ -6,9 +6,27 @@ if (process.platform === 'win32') {
6
6
  process.env.PWD = process.cwd()
7
7
  }
8
8
 
9
- module.exports = (vars = {}) =>
10
- new webpack.DefinePlugin({
9
+ const {MAGIC_STRINGS = '{}'} = process.env
10
+
11
+ let magic
12
+ try {
13
+ magic = JSON.parse(MAGIC_STRINGS)
14
+ } catch (err) {
15
+ magic = {}
16
+ }
17
+
18
+ module.exports = (vars = {}) => {
19
+ const definitions = {
11
20
  __DEV__: false,
12
21
  __BASE_DIR__: JSON.stringify(process.env.PWD),
13
- ...vars
14
- })
22
+ __MOCKS_API_PATH__: JSON.stringify(
23
+ process.env.MOCKS_API_PATH || process.env.PWD + '/mocks/routes'
24
+ ),
25
+ ...vars,
26
+ ...Object.fromEntries(
27
+ Object.entries(magic).map(([key, value]) => [key, JSON.stringify(value)])
28
+ )
29
+ }
30
+
31
+ return new webpack.DefinePlugin(definitions)
32
+ }
package/shared/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  const {config} = require('./config.js')
2
2
 
3
- exports.MAIN_ENTRY_POINT = './app.js'
3
+ exports.MAIN_ENTRY_POINT = './app'
4
4
  exports.config = config
5
5
 
6
6
  exports.cleanList = list => list.filter(Boolean)
@@ -0,0 +1,75 @@
1
+ /* eslint-disable no-console */
2
+ const fs = require('fs-extra')
3
+ const path = require('path')
4
+ const {config} = require('./index.js')
5
+ const {getSWCConfig} = require('@s-ui/compiler-config')
6
+
7
+ const EXCLUDED_FOLDERS_REGEXP = new RegExp(
8
+ `node_modules(?!${path.sep}@s-ui(${path.sep}studio)(${path.sep}workbench)?${path.sep}src)`
9
+ )
10
+
11
+ const getTSConfig = () => {
12
+ // Get TS config from the package dir.
13
+ const tsConfigPath = path.join(process.cwd(), 'tsconfig.json')
14
+ let tsConfig
15
+
16
+ try {
17
+ if (fs.existsSync(tsConfigPath)) {
18
+ tsConfig = JSON.parse(fs.readFileSync(tsConfigPath, {encoding: 'utf8'}))
19
+ }
20
+ } catch (err) {
21
+ console.error(err)
22
+ }
23
+
24
+ return tsConfig
25
+ }
26
+
27
+ module.exports = ({
28
+ isServer = false,
29
+ isDevelopment = false,
30
+ supportLegacyBrowsers = true
31
+ } = {}) => {
32
+ const tsConfig = getTSConfig()
33
+ // If TS config exists in root dir, set TypeScript as enabled.
34
+ const isTypeScriptEnabled = Boolean(tsConfig)
35
+
36
+ return isTypeScriptEnabled
37
+ ? {
38
+ test: /\.(js|ts)x?$/,
39
+ exclude: EXCLUDED_FOLDERS_REGEXP,
40
+ use: [
41
+ {
42
+ loader: require.resolve('swc-loader'),
43
+ options: getSWCConfig({isModern: false, isTypeScript: true})
44
+ }
45
+ ]
46
+ }
47
+ : {
48
+ test: /\.jsx?$/,
49
+ exclude: EXCLUDED_FOLDERS_REGEXP,
50
+ use: [
51
+ {
52
+ loader: require.resolve('babel-loader'),
53
+ options: {
54
+ cacheDirectory: true,
55
+ cacheCompression: false,
56
+ babelrc: false,
57
+ compact: true,
58
+ plugins: [
59
+ isDevelopment && require.resolve('react-refresh/babel')
60
+ ].filter(Boolean),
61
+ presets: [
62
+ [
63
+ require.resolve('babel-preset-sui'),
64
+ {
65
+ isServer,
66
+ isModern: !supportLegacyBrowsers,
67
+ targets: config.targets
68
+ }
69
+ ]
70
+ ]
71
+ }
72
+ }
73
+ ]
74
+ }
75
+ }
@@ -1,5 +1,6 @@
1
1
  const path = require('path')
2
2
  const {config} = require('./config.js')
3
+ const fs = require('fs')
3
4
 
4
5
  const {PWD} = process.env
5
6
 
@@ -11,22 +12,33 @@ const {PWD} = process.env
11
12
  */
12
13
  const defaultPackagesToAlias = [
13
14
  'react',
15
+ 'react-dom',
14
16
  'react-router-dom',
17
+ 'react/jsx-dev-runtime',
18
+ 'react/jsx-runtime',
15
19
  '@s-ui/pde',
16
20
  '@s-ui/react-context',
17
21
  '@s-ui/react-router'
18
22
  ]
19
23
 
20
- const createAliasPath = pkgName =>
21
- path.resolve(path.join(PWD, `./node_modules/${pkgName}`))
24
+ const createAliasPath = pkgName => {
25
+ const PWDNodeModules = path.join(PWD, './node_modules')
26
+ if (fs.existsSync(PWDNodeModules))
27
+ return path.resolve(path.join(PWDNodeModules, pkgName))
22
28
 
23
- const mustPackagesToAlias = {
24
- 'react/jsx-dev-runtime': 'react/jsx-dev-runtime.js',
25
- 'react/jsx-runtime': 'react/jsx-runtime.js'
29
+ try {
30
+ return require.resolve(pkgName).replace(/\/index\.js$/, '')
31
+ } catch (e) {
32
+ return ''
33
+ }
26
34
  }
27
35
 
36
+ const mustPackagesToAlias = {}
37
+
28
38
  exports.defaultAlias = Object.fromEntries(
29
- defaultPackagesToAlias.map(pkgName => [pkgName, createAliasPath(pkgName)])
39
+ defaultPackagesToAlias
40
+ .map(pkgName => [pkgName, createAliasPath(pkgName)])
41
+ .filter(([, path]) => path)
30
42
  )
31
43
 
32
44
  const aliasFromConfig = config.alias
package/tsconfig.json ADDED
@@ -0,0 +1,3 @@
1
+ {
2
+ "extends": "@s-ui/compiler-config/tsconfig.json"
3
+ }
@@ -18,7 +18,7 @@ const {aliasFromConfig, defaultAlias} = require('./shared/resolve-alias.js')
18
18
  const {supportLegacyBrowsers} = require('./shared/config.js')
19
19
 
20
20
  const {resolveLoader} = require('./shared/resolve-loader.js')
21
- const createBabelRules = require('./shared/module-rules-babel.js')
21
+ const createCompilerRules = require('./shared/module-rules-compiler.js')
22
22
 
23
23
  const outputPath = path.join(process.cwd(), 'dist')
24
24
 
@@ -47,7 +47,7 @@ const webpackConfig = {
47
47
  timers: false
48
48
  },
49
49
  modules: ['node_modules', path.resolve(process.cwd())],
50
- extensions: ['.js', '.json']
50
+ extensions: ['.js', '.tsx', '.ts', '.json']
51
51
  },
52
52
  stats: 'errors-only',
53
53
  entry: {
@@ -87,7 +87,7 @@ const webpackConfig = {
87
87
  resolveLoader,
88
88
  module: {
89
89
  rules: cleanList([
90
- createBabelRules({supportLegacyBrowsers, isDevelopment: true}),
90
+ createCompilerRules({supportLegacyBrowsers, isDevelopment: true}),
91
91
  {
92
92
  test: /(\.css|\.scss)$/,
93
93
  use: cleanList([
@@ -9,7 +9,7 @@ const {
9
9
  const path = require('path')
10
10
  const minifyJs = require('./shared/minify-js.js')
11
11
  const definePlugin = require('./shared/define.js')
12
- const createBabelRules = require('./shared/module-rules-babel.js')
12
+ const createCompilerRules = require('./shared/module-rules-compiler.js')
13
13
  const sassRules = require('./shared/module-rules-sass.js')
14
14
  const {
15
15
  extractComments,
@@ -67,6 +67,6 @@ module.exports = {
67
67
  definePlugin()
68
68
  ]),
69
69
  module: {
70
- rules: [createBabelRules({supportLegacyBrowsers}), sassRules]
70
+ rules: [createCompilerRules({supportLegacyBrowsers}), sassRules]
71
71
  }
72
72
  }
@@ -23,7 +23,7 @@ const {
23
23
  supportLegacyBrowsers
24
24
  } = require('./shared/config.js')
25
25
  const {resolveLoader} = require('./shared/resolve-loader.js')
26
- const createBabelRules = require('./shared/module-rules-babel.js')
26
+ const createCompilerRules = require('./shared/module-rules-compiler.js')
27
27
  const sassRules = require('./shared/module-rules-sass.js')
28
28
  const definePlugin = require('./shared/define.js')
29
29
  const manifestLoaderRules = require('./shared/module-rules-manifest-loader.js')
@@ -54,7 +54,7 @@ const webpackConfig = {
54
54
  context: path.resolve(CWD, 'src'),
55
55
  resolve: {
56
56
  alias: {...aliasFromConfig},
57
- extensions: ['.js', '.json'],
57
+ extensions: ['.js', '.json', '.ts', '.tsx'],
58
58
  modules: ['node_modules', path.resolve(CWD)],
59
59
  fallback: {
60
60
  assert: false,
@@ -114,7 +114,7 @@ const webpackConfig = {
114
114
  ]),
115
115
  module: {
116
116
  rules: cleanList([
117
- createBabelRules({supportLegacyBrowsers}),
117
+ createCompilerRules({supportLegacyBrowsers}),
118
118
  sassRules,
119
119
  when(config['externals-manifest'], () =>
120
120
  manifestLoaderRules(config['externals-manifest'])
@@ -3,7 +3,7 @@ const webpackNodeExternals = require('webpack-node-externals')
3
3
  const path = require('path')
4
4
 
5
5
  const {config, when, cleanList} = require('./shared/index.js')
6
- const createBabelRules = require('./shared/module-rules-babel.js')
6
+ const createCompilerRules = require('./shared/module-rules-compiler.js')
7
7
  const manifestLoaderRules = require('./shared/module-rules-manifest-loader.js')
8
8
  const {aliasFromConfig} = require('./shared/resolve-alias.js')
9
9
  const {resolveLoader} = require('./shared/resolve-loader.js')
@@ -18,7 +18,7 @@ const webpackConfig = {
18
18
  mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
19
19
  resolve: {
20
20
  alias: {...aliasFromConfig},
21
- extensions: ['.js', '.json'],
21
+ extensions: ['.js', '.json', '.ts', '.tsx'],
22
22
  modules: ['node_modules', path.resolve(process.cwd())]
23
23
  },
24
24
  entry: './server.js',
@@ -39,7 +39,7 @@ const webpackConfig = {
39
39
  resolveLoader,
40
40
  module: {
41
41
  rules: cleanList([
42
- createBabelRules({isServer: true}),
42
+ createCompilerRules({isServer: true}),
43
43
  {
44
44
  // ignore css/scss/svg require/imports files in the server
45
45
  test: /(\.svg|\.s?css)$/,
@@ -1,39 +0,0 @@
1
- const path = require('path')
2
- const {config} = require('./index.js')
3
-
4
- const EXCLUDED_FOLDERS_REGEXP = new RegExp(
5
- `node_modules(?!${path.sep}@s-ui(${path.sep}studio)(${path.sep}workbench)?${path.sep}src)`
6
- )
7
-
8
- module.exports = ({
9
- isServer = false,
10
- isDevelopment = false,
11
- supportLegacyBrowsers = true
12
- } = {}) => ({
13
- test: /\.jsx?$/,
14
- exclude: EXCLUDED_FOLDERS_REGEXP,
15
- use: [
16
- {
17
- loader: require.resolve('babel-loader'),
18
- options: {
19
- cacheDirectory: true,
20
- cacheCompression: false,
21
- babelrc: false,
22
- compact: true,
23
- plugins: [
24
- isDevelopment && require.resolve('react-refresh/babel')
25
- ].filter(Boolean),
26
- presets: [
27
- [
28
- require.resolve('babel-preset-sui'),
29
- {
30
- isServer,
31
- isModern: !supportLegacyBrowsers,
32
- targets: config.targets
33
- }
34
- ]
35
- ]
36
- }
37
- }
38
- ]
39
- })