@s-ui/bundler 9.57.0 → 9.59.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.
@@ -57,7 +57,7 @@ if (!module.parent) {
57
57
  const start = async ({config = webpackConfig, packagesToLink = program.opts().linkPackage || []} = {}) => {
58
58
  clearConsole()
59
59
  // Warn and crash if required files are missing
60
- if (!checkRequiredFiles([path.join(config.context, 'index.html'), path.join(config.context, 'app.js')])) {
60
+ if (!checkRequiredFiles([path.join(config.context, 'index.html')])) {
61
61
  log.error(`✖ Required files are missing, create and index.html and app.js inside your src folder.`)
62
62
  process.exit(1)
63
63
  }
@@ -43,7 +43,7 @@ module.exports = ({config, packagesToLink, linkAll}) => {
43
43
  * if neccesary
44
44
  */
45
45
  const linkLoader = {
46
- test: /\.(jsx?|scss)$/,
46
+ test: /\.(jsx?|tsx?|scss)$/,
47
47
  enforce: 'pre', // this will ensure is execute before transformations
48
48
  use: {
49
49
  loader: require.resolve('./LinkLoader'),
package/package.json CHANGED
@@ -1,15 +1,15 @@
1
1
  {
2
2
  "name": "@s-ui/bundler",
3
- "version": "9.57.0",
3
+ "version": "9.59.0",
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
- "keywords": [],
13
13
  "author": "",
14
14
  "license": "MIT",
15
15
  "repository": {
@@ -21,11 +21,14 @@
21
21
  },
22
22
  "homepage": "https://github.com/SUI-Components/sui/tree/master/packages/sui-bundler#readme",
23
23
  "dependencies": {
24
- "@babel/core": "7.18.10",
24
+ "@babel/core": "7.21.8",
25
25
  "@builder.io/react-hydration-overlay": "0.0.8",
26
26
  "@pmmmwh/react-refresh-webpack-plugin": "0.5.10",
27
+ "@s-ui/compiler-config": "1",
27
28
  "@s-ui/helpers": "1",
28
29
  "@s-ui/sass-loader": "1",
30
+ "@swc/core": "1.3.14",
31
+ "@swc/helpers": "0.4.12",
29
32
  "address": "1.2.2",
30
33
  "autoprefixer": "10.4.8",
31
34
  "babel-loader": "8.3.0",
@@ -36,7 +39,7 @@
36
39
  "css-minimizer-webpack-plugin": "4.0.0",
37
40
  "esbuild": "0.15.5",
38
41
  "escape-string-regexp": "4.0.0",
39
- "fast-glob": "3.2.11",
42
+ "fast-glob": "3.2.12",
40
43
  "find-free-ports": "3.0.0",
41
44
  "html-webpack-inject-attributes-plugin": "1.0.6",
42
45
  "html-webpack-plugin": "5.5.0",
@@ -50,8 +53,9 @@
50
53
  "stream-http": "3.2.0",
51
54
  "strip-ansi": "6.0.1",
52
55
  "style-loader": "3.3.1",
56
+ "swc-loader": "0.2.1",
53
57
  "url": "0.11.0",
54
- "webpack-dev-server": "4.10.0",
58
+ "webpack-dev-server": "4.15.2",
55
59
  "webpack-manifest-plugin": "5.0.0",
56
60
  "webpack-node-externals": "3.0.0",
57
61
  "webpack": "5.82.1"
@@ -0,0 +1,48 @@
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('🔍 [sui-bundler postinstall] Checking if tsconfig.json is up to date...')
40
+ if (!shouldGenerateTSConfig()) {
41
+ console.log('✅ [sui-bundler postinstall] tsconfig.json is up to date')
42
+ process.exit(0)
43
+ }
44
+ await writeFile(TS_CONFIG_PATH, tsConfigTemplate)
45
+ console.log('❌ [sui-bundler postinstall] tsconfig.json was not up to date, so we updated it')
46
+ }
47
+
48
+ main()
package/shared/define.js CHANGED
@@ -6,10 +6,20 @@ if (process.platform === 'win32') {
6
6
  process.env.PWD = process.cwd()
7
7
  }
8
8
 
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
+
9
18
  module.exports = (vars = {}) =>
10
19
  new webpack.DefinePlugin({
11
- __MOCKS_API_PATH__: JSON.stringify(process.env.MOCKS_API_PATH || process.env.PWD + '/mocks/routes'),
12
20
  __DEV__: false,
13
21
  __BASE_DIR__: JSON.stringify(process.env.PWD),
14
- ...vars
22
+ __MOCKS_API_PATH__: JSON.stringify(process.env.MOCKS_API_PATH || process.env.PWD + '/mocks/routes'),
23
+ ...vars,
24
+ ...Object.fromEntries(Object.entries(magic).map(([key, value]) => [key, JSON.stringify(value)]))
15
25
  })
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,69 @@
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 = ({isServer = false, isDevelopment = false, supportLegacyBrowsers = true} = {}) => {
28
+ const tsConfig = getTSConfig()
29
+ // If TS config exists in root dir, set TypeScript as enabled.
30
+ const isTypeScriptEnabled = Boolean(tsConfig)
31
+
32
+ return isTypeScriptEnabled
33
+ ? {
34
+ test: /\.(js|ts)x?$/,
35
+ exclude: EXCLUDED_FOLDERS_REGEXP,
36
+ use: [
37
+ {
38
+ loader: require.resolve('swc-loader'),
39
+ options: getSWCConfig({isModern: false, isTypeScript: true})
40
+ }
41
+ ]
42
+ }
43
+ : {
44
+ test: /\.jsx?$/,
45
+ exclude: EXCLUDED_FOLDERS_REGEXP,
46
+ use: [
47
+ {
48
+ loader: require.resolve('babel-loader'),
49
+ options: {
50
+ cacheDirectory: true,
51
+ cacheCompression: false,
52
+ babelrc: false,
53
+ compact: true,
54
+ plugins: [isDevelopment && require.resolve('react-refresh/babel')].filter(Boolean),
55
+ presets: [
56
+ [
57
+ require.resolve('babel-preset-sui'),
58
+ {
59
+ isServer,
60
+ isModern: !supportLegacyBrowsers,
61
+ targets: config.targets
62
+ }
63
+ ]
64
+ ]
65
+ }
66
+ }
67
+ ]
68
+ }
69
+ }
@@ -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
 
@@ -9,16 +10,40 @@ const {PWD} = process.env
9
10
  * So you should use the exact same imported file from node_modules, and the linked package
10
11
  * would try to use another different from its own node_modules. This will prevent that.
11
12
  */
12
- const defaultPackagesToAlias = ['react', 'react-router-dom', '@s-ui/pde', '@s-ui/react-context', '@s-ui/react-router']
13
+ const defaultPackagesToAlias = [
14
+ 'react',
15
+ 'react-dom',
16
+ 'react-router-dom',
17
+ 'react/jsx-dev-runtime',
18
+ 'react/jsx-runtime',
19
+ '@s-ui/pde',
20
+ '@s-ui/react-context',
21
+ '@s-ui/react-router'
22
+ ]
13
23
 
14
- const createAliasPath = pkgName => path.resolve(path.join(PWD, `./node_modules/${pkgName}`))
24
+ const createAliasPath = pkgName => {
25
+ const nodeModulesPath = path.join(PWD, './node_modules')
26
+ const parentNodeModulesPath = path.join(PWD, '../node_modules')
27
+ const pkgPath = nodeModulesPath && path.join(nodeModulesPath, pkgName)
15
28
 
16
- const mustPackagesToAlias = {
17
- 'react/jsx-dev-runtime': 'react/jsx-dev-runtime.js',
18
- 'react/jsx-runtime': 'react/jsx-runtime.js'
29
+ if (pkgPath && fs.existsSync(pkgPath)) {
30
+ return path.resolve(pkgPath)
31
+ } else if (fs.existsSync(parentNodeModulesPath)) {
32
+ return path.resolve(path.join(parentNodeModulesPath, pkgName))
33
+ }
34
+
35
+ try {
36
+ return require.resolve(pkgName).replace(/\/index\.js$/, '')
37
+ } catch (e) {
38
+ return ''
39
+ }
19
40
  }
20
41
 
21
- exports.defaultAlias = Object.fromEntries(defaultPackagesToAlias.map(pkgName => [pkgName, createAliasPath(pkgName)]))
42
+ const mustPackagesToAlias = {}
43
+
44
+ exports.defaultAlias = Object.fromEntries(
45
+ defaultPackagesToAlias.map(pkgName => [pkgName, createAliasPath(pkgName)]).filter(([, path]) => path)
46
+ )
22
47
 
23
48
  const aliasFromConfig = config.alias
24
49
  ? Object.entries(config.alias).reduce(
package/tsconfig.json ADDED
@@ -0,0 +1,3 @@
1
+ {
2
+ "extends": "@s-ui/compiler-config/tsconfig.json"
3
+ }
@@ -12,7 +12,7 @@ const {aliasFromConfig, defaultAlias} = require('./shared/resolve-alias.js')
12
12
  const {supportLegacyBrowsers, cacheDirectory} = require('./shared/config.js')
13
13
 
14
14
  const {resolveLoader} = require('./shared/resolve-loader.js')
15
- const createBabelRules = require('./shared/module-rules-babel.js')
15
+ const createCompilerRules = require('./shared/module-rules-compiler.js')
16
16
 
17
17
  const outputPath = path.join(process.cwd(), 'dist')
18
18
 
@@ -42,7 +42,7 @@ const webpackConfig = {
42
42
  timers: false
43
43
  },
44
44
  modules: ['node_modules', path.resolve(process.cwd())],
45
- extensions: ['.js', '.json']
45
+ extensions: ['.js', '.tsx', '.ts', '.json']
46
46
  },
47
47
  stats: 'errors-only',
48
48
  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([
@@ -4,7 +4,7 @@ const {cleanList, envVars, MAIN_ENTRY_POINT, config} = require('./shared/index.j
4
4
  const path = require('path')
5
5
  const minifyJs = require('./shared/minify-js.js')
6
6
  const definePlugin = require('./shared/define.js')
7
- const createBabelRules = require('./shared/module-rules-babel.js')
7
+ const createCompilerRules = require('./shared/module-rules-compiler.js')
8
8
  const sassRules = require('./shared/module-rules-sass.js')
9
9
  const {extractComments, sourceMap, supportLegacyBrowsers} = require('./shared/config.js')
10
10
  const {aliasFromConfig} = require('./shared/resolve-alias.js')
@@ -45,7 +45,7 @@ module.exports = {
45
45
  },
46
46
  plugins: cleanList([
47
47
  new webpack.ProvidePlugin({
48
- process: 'process/browser'
48
+ process: 'process/browser.js'
49
49
  }),
50
50
  new MiniCssExtractPlugin({
51
51
  filename: cssFileName,
@@ -58,6 +58,6 @@ module.exports = {
58
58
  definePlugin()
59
59
  ]),
60
60
  module: {
61
- rules: [createBabelRules({supportLegacyBrowsers}), sassRules]
61
+ rules: [createCompilerRules({supportLegacyBrowsers}), sassRules]
62
62
  }
63
63
  }
@@ -13,7 +13,7 @@ const {when, cleanList, envVars, MAIN_ENTRY_POINT, config} = require('./shared/i
13
13
  const {aliasFromConfig} = require('./shared/resolve-alias.js')
14
14
  const {extractComments, sourceMap, supportLegacyBrowsers, cacheDirectory} = require('./shared/config.js')
15
15
  const {resolveLoader} = require('./shared/resolve-loader.js')
16
- const createBabelRules = require('./shared/module-rules-babel.js')
16
+ const createCompilerRules = require('./shared/module-rules-compiler.js')
17
17
  const sassRules = require('./shared/module-rules-sass.js')
18
18
  const definePlugin = require('./shared/define.js')
19
19
  const manifestLoaderRules = require('./shared/module-rules-manifest-loader.js')
@@ -41,7 +41,7 @@ const webpackConfig = {
41
41
  context: path.resolve(CWD, 'src'),
42
42
  resolve: {
43
43
  alias: {...aliasFromConfig},
44
- extensions: ['.js', '.json'],
44
+ extensions: ['.js', '.json', '.ts', '.tsx'],
45
45
  modules: ['node_modules', path.resolve(CWD)],
46
46
  fallback: {
47
47
  assert: false,
@@ -74,7 +74,7 @@ const webpackConfig = {
74
74
  },
75
75
  plugins: cleanList([
76
76
  new webpack.ProvidePlugin({
77
- process: 'process/browser'
77
+ process: 'process/browser.js'
78
78
  }),
79
79
  new webpack.ids.HashedModuleIdsPlugin(),
80
80
  new webpack.EnvironmentPlugin(envVars(config.env)),
@@ -104,7 +104,7 @@ const webpackConfig = {
104
104
  ]),
105
105
  module: {
106
106
  rules: cleanList([
107
- createBabelRules({supportLegacyBrowsers}),
107
+ createCompilerRules({supportLegacyBrowsers}),
108
108
  sassRules,
109
109
  when(config['externals-manifest'], () => manifestLoaderRules(config['externals-manifest']))
110
110
  ])
@@ -4,7 +4,7 @@ const path = require('path')
4
4
 
5
5
  const {config, when, cleanList} = require('./shared/index.js')
6
6
  const {cacheDirectory} = require('./shared/config.js')
7
- const createBabelRules = require('./shared/module-rules-babel.js')
7
+ const createCompilerRules = require('./shared/module-rules-compiler.js')
8
8
  const manifestLoaderRules = require('./shared/module-rules-manifest-loader.js')
9
9
  const {aliasFromConfig} = require('./shared/resolve-alias.js')
10
10
  const {resolveLoader} = require('./shared/resolve-loader.js')
@@ -22,7 +22,7 @@ const webpackConfig = {
22
22
  mode: isProduction ? 'production' : 'development',
23
23
  resolve: {
24
24
  alias: {...aliasFromConfig},
25
- extensions: ['.js', '.json'],
25
+ extensions: ['.js', '.json', '.ts', '.tsx'],
26
26
  modules: ['node_modules', path.resolve(process.cwd())]
27
27
  },
28
28
  entry: './server.js',
@@ -48,7 +48,7 @@ const webpackConfig = {
48
48
  resolveLoader,
49
49
  module: {
50
50
  rules: cleanList([
51
- createBabelRules({isServer: true}),
51
+ createCompilerRules({isServer: true}),
52
52
  {
53
53
  // ignore css/scss/svg require/imports files in the server
54
54
  test: /(\.svg|\.s?css)$/,
@@ -1,33 +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 = ({isServer = false, isDevelopment = false, supportLegacyBrowsers = true} = {}) => ({
9
- test: /\.jsx?$/,
10
- exclude: EXCLUDED_FOLDERS_REGEXP,
11
- use: [
12
- {
13
- loader: require.resolve('babel-loader'),
14
- options: {
15
- cacheDirectory: true,
16
- cacheCompression: false,
17
- babelrc: false,
18
- compact: true,
19
- plugins: [isDevelopment && require.resolve('react-refresh/babel')].filter(Boolean),
20
- presets: [
21
- [
22
- require.resolve('babel-preset-sui'),
23
- {
24
- isServer,
25
- isModern: !supportLegacyBrowsers,
26
- targets: config.targets
27
- }
28
- ]
29
- ]
30
- }
31
- }
32
- ]
33
- })