@jk2908/cxx 0.1.7 → 0.1.9

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/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2024 Jerome Kenway
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Jerome Kenway
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
  SOFTWARE.
package/README.md CHANGED
@@ -1,71 +1,58 @@
1
- cxx
2
-
3
- library based on experiments with React 19 suspenseful style hoisting and deduping. `cxx` intakes CSS via a template literal arg and returns a three item tuple; the processed CSS, if configured (on by default) a CSS Modules exports object, and also optionally for usage, an id to pass to the href attribute of a style tag for deduping see [React docs](https://react.dev/reference/react-dom/components/style#noun-labs-1201738-(2)). The function actually just returns placeholders for the above items which will be replaced at build time. No string interpolation is supported within the CSS template tag input at the moment.
4
-
5
- example usage:
6
- In ui templates `cmp.tsx`:
7
- ```
8
- import { cxx } from '@jk2908/cxx'
9
-
10
- const [css, styles, href] = cxx`
11
- .header {
12
- display: flex;
13
- }
14
- `
15
-
16
- function Cmp() {
17
- return (
18
- <header className={styles.header}>
19
- ...
20
-
21
- <style href={href} precedence="medium">
22
- {css}
23
- </style>
24
- </header>
25
- )
26
- }
27
-
28
- // without CSS Modules exports but with href
29
- const [css,, href] = cxx`...`
30
-
31
- // plain CSS
32
- const [css] = cxx`...`
33
- ```
34
-
35
- CSS is transformed using [lightningcss](https://www.npmjs.com/package/lightningcss). There is access to the full lightningcss config.
36
-
37
- vite `vite.config.ts`:
38
- ```
39
- import cxx from '@jk2908/cxx/vite-plugin-cxx'
40
-
41
- export default defineConfig({
42
- plugins: [react(), cxx({ ...optional cxx config })],
43
- })
44
-
45
- // turn off default config
46
- export default defineConfig({
47
- plugins: [cxx({
48
- lightningcss: {
49
- minify: false,
50
- cssModules: false,
51
- ...other transform options
52
- }
53
- })]
54
- })
55
- ```
56
-
57
- next `next.config.ts`:
58
- ```
59
- import { withCxx } from '@jk2908/cxx/next'
60
-
61
- const nextConfig = {}
62
-
63
- export default withCxx(nextConfig, { ...optional cxx config })
64
- ```
65
-
66
- with inspiration from:
67
- - https://github.com/bluskript/vite-plugin-inline-css-modules/
68
-
69
- using:
70
- - https://github.com/bryc/code/blob/master/jshash/experimental/cyrb53.js
71
- - https://www.npmjs.com/package/lightningcss
1
+ cxx
2
+
3
+ library based on experiments with React 19 suspenseful style hoisting and deduping. `cxx` intakes CSS via a template literal arg and returns a three item tuple; the processed CSS, if configured (on by default) a CSS Modules exports object, and also optionally for usage, an id to pass to the href attribute of a style tag for deduping see [React docs](https://react.dev/reference/react-dom/components/style#noun-labs-1201738-(2)). The function actually just returns placeholders for the above items which will be replaced at build time. No string interpolation is supported within the CSS template tag.
4
+
5
+ example usage:
6
+ In ui templates `cmp.tsx`:
7
+ ```
8
+ import { cxx } from '@jk2908/cxx'
9
+
10
+ const [css, styles, href] = cxx`
11
+ .header {
12
+ display: flex;
13
+ }
14
+ `
15
+
16
+ function Cmp() {
17
+ return (
18
+ <header className={styles.header}>
19
+ ...
20
+
21
+ <style href={href} precedence="medium">
22
+ {css}
23
+ </style>
24
+ </header>
25
+ )
26
+ }
27
+ ```
28
+
29
+ CSS is transformed using [lightningcss](https://www.npmjs.com/package/lightningcss). There is access to the full lightningcss config.
30
+
31
+ vite `vite.config.ts`:
32
+ ```
33
+ import cxx from '@jk2908/cxx/vite-plugin-cxx'
34
+
35
+ export default defineConfig({
36
+ plugins: [react(), cxx({ ...optional cxx config })],
37
+ })
38
+
39
+ // turn off default config
40
+ export default defineConfig({
41
+ plugins: [cxx({
42
+ lightningcss: {
43
+ minify: false,
44
+ cssModules: false,
45
+ ...other transform options
46
+ }
47
+ })]
48
+ })
49
+ ```
50
+
51
+ next `next.config.ts`:
52
+ ```
53
+ import { withCxx } from '@jk2908/cxx/next'
54
+
55
+ const nextConfig = {}
56
+
57
+ export default withCxx(nextConfig, { ...optional cxx config })
58
+ ```
@@ -34,7 +34,7 @@ function inject(source, id, config = {}) {
34
34
  ...DEFAULT_CONFIG.lightningcss,
35
35
  ...config.lightningcss,
36
36
  filename: id,
37
- code: Buffer.from(css)
37
+ code: new TextEncoder().encode(css)
38
38
  });
39
39
  const href = cyrb53([
40
40
  ...Object.keys(exports ?? {}),
@@ -32,7 +32,7 @@ function inject(source, id, config = {}) {
32
32
  ...DEFAULT_CONFIG.lightningcss,
33
33
  ...config.lightningcss,
34
34
  filename: id,
35
- code: Buffer.from(css)
35
+ code: new TextEncoder().encode(css)
36
36
  });
37
37
  const href = cyrb53([
38
38
  ...Object.keys(exports ?? {}),
@@ -1,7 +1,7 @@
1
1
  import { inject } from '../inject/index.js';
2
2
 
3
- function loader(source) {
4
- return inject(source, this.resourcePath, this.getOptions())
3
+ function loader(source) {
4
+ return inject(source, this.resourcePath, this.getOptions())
5
5
  }
6
6
 
7
7
  export { loader as default };
@@ -15,7 +15,7 @@ function withCxx(nextConfig = {}, config = {}) {
15
15
  const turboConfig = {
16
16
  ...nextConfig.turbo,
17
17
  rules: {
18
- ...nextConfig.turbo?.rules ?? {},
18
+ ...nextConfig.turbo?.rules,
19
19
  [TURBO_GLOB]: {
20
20
  loaders: [
21
21
  ...nextConfig.turbo?.rules?.[TURBO_GLOB]?.loaders ?? [],
package/package.json CHANGED
@@ -1,89 +1,90 @@
1
- {
2
- "name": "@jk2908/cxx",
3
- "version": "0.1.7",
4
- "type": "module",
5
- "author": "Jerome Kenway",
6
- "repository": {
7
- "type": "git",
8
- "url": "git+https://github.com/jk2908/cxx.git"
9
- },
10
- "main": "./dist/index.js",
11
- "types": "./dist/index.d.ts",
12
- "scripts": {
13
- "build": "bunchee"
14
- },
15
- "devDependencies": {
16
- "@biomejs/biome": "^1.8.3",
17
- "@types/react": "latest",
18
- "@types/react-dom": "latest",
19
- "@types/webpack": "^5.28.5",
20
- "bunchee": "^5.3.2",
21
- "cross-env": "^7.0.3",
22
- "lightningcss": "^1.27.0",
23
- "next": "^15.0.0-canary.128",
24
- "react": "^19.0.0-rc || ^19.0.0-beta || ^19.0.0",
25
- "react-dom": "^19.0.0-rc || ^19.0.0-beta || ^19.0.0",
26
- "typescript": "^5.5.4",
27
- "vite": "^5.4.2"
28
- },
29
- "peerDependencies": {
30
- "lightningcss": "^1.27.0",
31
- "next": "^15.0.0-canary || ^15.0.0",
32
- "react": "^19.0.0-rc || ^19.0.0-beta || ^19.0.0",
33
- "react-dom": "^19.0.0-rc || ^19.0.0-beta || ^19.0.0",
34
- "vite": "^4.0.0"
35
- },
36
- "exports": {
37
- ".": {
38
- "types": "./dist/index.d.ts",
39
- "default": "./dist/index.js"
40
- },
41
- "./inject": {
42
- "types": "./dist/inject/index.d.ts",
43
- "require": "./dist/inject/index.cjs",
44
- "default": "./dist/inject/index.js"
45
- },
46
- "./next": {
47
- "types": "./dist/next/index.d.ts",
48
- "default": "./dist/next/index.js"
49
- },
50
- "./next/cxx-loader": {
51
- "types": "./dist/next/cxx-loader.d.ts",
52
- "require": "./dist/next/cxx-loader.cjs",
53
- "default": "./dist/next/cxx-loader.js"
54
- },
55
- "./vite-plugin-cxx": {
56
- "types": "./dist/vite-plugin-cxx/index.d.ts",
57
- "default": "./dist/vite-plugin-cxx/index.js"
58
- }
59
- },
60
- "bugs": {
61
- "url": "https://github.com/jk2908/cxx/issues"
62
- },
63
- "description": "Utilities for generating styles in React 19",
64
- "files": [
65
- "dist",
66
- "src"
67
- ],
68
- "homepage": "https://github.com/jk2908/cxx#readme",
69
- "keywords": [
70
- "react",
71
- "react19",
72
- "css",
73
- "css-in-js",
74
- "vite-plugin",
75
- "webpack-loader",
76
- "lightningcss",
77
- "next",
78
- "vite"
79
- ],
80
- "license": "MIT",
81
- "peerDependenciesMeta": {
82
- "next": {
83
- "optional": true
84
- },
85
- "vite": {
86
- "optional": true
87
- }
88
- }
89
- }
1
+ {
2
+ "name": "@jk2908/cxx",
3
+ "version": "0.1.9",
4
+ "type": "module",
5
+ "author": "Jerome Kenway",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/jk2908/cxx.git"
9
+ },
10
+ "main": "./dist/index.js",
11
+ "types": "./dist/index.d.ts",
12
+ "scripts": {
13
+ "build": "bunchee",
14
+ "prepack": "npm run build"
15
+ },
16
+ "devDependencies": {
17
+ "@biomejs/biome": "^1.8.3",
18
+ "@types/react": "latest",
19
+ "@types/react-dom": "latest",
20
+ "@types/webpack": "^5.28.5",
21
+ "bunchee": "^5.3.2",
22
+ "cross-env": "^7.0.3",
23
+ "lightningcss": "^1.27.0",
24
+ "next": "^15.0.0-canary.128",
25
+ "react": "^19.0.0-rc || ^19.0.0-beta || ^19.0.0",
26
+ "react-dom": "^19.0.0-rc || ^19.0.0-beta || ^19.0.0",
27
+ "typescript": "^5.5.4",
28
+ "vite": "^5.4.2"
29
+ },
30
+ "peerDependencies": {
31
+ "lightningcss": "^1.27.0",
32
+ "next": "^15.0.0-canary || ^15.0.0",
33
+ "react": "^19.0.0-rc || ^19.0.0-beta || ^19.0.0",
34
+ "react-dom": "^19.0.0-rc || ^19.0.0-beta || ^19.0.0",
35
+ "vite": "^4.0.0"
36
+ },
37
+ "exports": {
38
+ ".": {
39
+ "types": "./dist/index.d.ts",
40
+ "default": "./dist/index.js"
41
+ },
42
+ "./inject": {
43
+ "types": "./dist/inject/index.d.ts",
44
+ "require": "./dist/inject/index.cjs",
45
+ "default": "./dist/inject/index.js"
46
+ },
47
+ "./next": {
48
+ "types": "./dist/next/index.d.ts",
49
+ "default": "./dist/next/index.js"
50
+ },
51
+ "./next/cxx-loader": {
52
+ "types": "./dist/next/cxx-loader.d.ts",
53
+ "require": "./dist/next/cxx-loader.cjs",
54
+ "default": "./dist/next/cxx-loader.js"
55
+ },
56
+ "./vite-plugin-cxx": {
57
+ "types": "./dist/vite-plugin-cxx/index.d.ts",
58
+ "default": "./dist/vite-plugin-cxx/index.js"
59
+ }
60
+ },
61
+ "bugs": {
62
+ "url": "https://github.com/jk2908/cxx/issues"
63
+ },
64
+ "description": "Utilities for generating styles in React 19",
65
+ "files": [
66
+ "dist",
67
+ "src"
68
+ ],
69
+ "homepage": "https://github.com/jk2908/cxx#readme",
70
+ "keywords": [
71
+ "react",
72
+ "react19",
73
+ "css",
74
+ "css-in-js",
75
+ "vite-plugin",
76
+ "webpack-loader",
77
+ "lightningcss",
78
+ "next",
79
+ "vite"
80
+ ],
81
+ "license": "MIT",
82
+ "peerDependenciesMeta": {
83
+ "next": {
84
+ "optional": true
85
+ },
86
+ "vite": {
87
+ "optional": true
88
+ }
89
+ }
90
+ }
package/src/cyrb53.ts CHANGED
@@ -1,20 +1,20 @@
1
- // cyrb53 (c) 2018 bryc (github.com/bryc)
2
- // https://github.com/bryc/code/blob/master/jshash/experimental/cyrb53.js
3
-
4
- export function cyrb53(str: string, seed = 0) {
5
- let h1 = 0xdeadbeef ^ seed
6
- let h2 = 0x41c6ce57 ^ seed
7
-
8
- for (let i = 0, ch: number; i < str.length; i++) {
9
- ch = str.charCodeAt(i)
10
- h1 = Math.imul(h1 ^ ch, 2654435761)
11
- h2 = Math.imul(h2 ^ ch, 1597334677)
12
- }
13
-
14
- h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507)
15
- h1 ^= Math.imul(h2 ^ (h2 >>> 13), 3266489909)
16
- h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507)
17
- h2 ^= Math.imul(h1 ^ (h1 >>> 13), 3266489909)
18
-
19
- return 4294967296 * (2097151 & h2) + (h1 >>> 0)
20
- }
1
+ // cyrb53 (c) 2018 bryc (github.com/bryc)
2
+ // https://github.com/bryc/code/blob/master/jshash/experimental/cyrb53.js
3
+
4
+ export function cyrb53(str: string, seed = 0) {
5
+ let h1 = 0xdeadbeef ^ seed
6
+ let h2 = 0x41c6ce57 ^ seed
7
+
8
+ for (let i = 0, ch: number; i < str.length; i++) {
9
+ ch = str.charCodeAt(i)
10
+ h1 = Math.imul(h1 ^ ch, 2654435761)
11
+ h2 = Math.imul(h2 ^ ch, 1597334677)
12
+ }
13
+
14
+ h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507)
15
+ h1 ^= Math.imul(h2 ^ (h2 >>> 13), 3266489909)
16
+ h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507)
17
+ h2 ^= Math.imul(h1 ^ (h1 >>> 13), 3266489909)
18
+
19
+ return 4294967296 * (2097151 & h2) + (h1 >>> 0)
20
+ }
package/src/index.ts CHANGED
@@ -1,12 +1,12 @@
1
- declare module 'react' {
2
- interface StyleHTMLAttributes<T> extends React.HTMLAttributes<T> {
3
- href?: string
4
- precedence?: 'low' | 'medium' | 'high'
5
- }
6
- }
7
-
8
- export const cxx = (_: TemplateStringsArray): readonly ['', Record<string, string>, ''] => [
9
- '',
10
- {},
11
- '',
1
+ declare module 'react' {
2
+ interface StyleHTMLAttributes<T> extends React.HTMLAttributes<T> {
3
+ href?: string
4
+ precedence?: 'low' | 'medium' | 'high'
5
+ }
6
+ }
7
+
8
+ export const cxx = (_: TemplateStringsArray): readonly ['', Record<string, string>, ''] => [
9
+ '',
10
+ {},
11
+ '',
12
12
  ]
package/src/inject.ts CHANGED
@@ -1,54 +1,54 @@
1
- import { cyrb53 } from './cyrb53'
2
-
3
- import { type TransformOptions, type CustomAtRules, transform } from 'lightningcss'
4
-
5
- const varMatch =
6
- /(?:const|var|let)\s*\[(\w+|)\s*,?\s*(\w+|)\s*,?\s*(\w+|)\s*\]\s*=\s*cxx\s*`([\s\S]*?)`/gm
7
-
8
- export type Config<C extends CustomAtRules = CustomAtRules> = {
9
- lightningcss?: Partial<TransformOptions<C>>
10
- }
11
-
12
- const DEFAULT_CONFIG: Config = {
13
- lightningcss: {
14
- minify: true,
15
- cssModules: true,
16
- },
17
- } as const
18
-
19
- export function inject<C extends CustomAtRules = CustomAtRules>(
20
- source: string,
21
- id: string,
22
- config: Config<C> = {},
23
- ) {
24
- try {
25
- const tmpl = source.replace(varMatch, (_: string, ...args: string[]) => {
26
- const [varOne, varTwo, varThree, css] = args
27
-
28
- const { code, exports } = transform({
29
- ...DEFAULT_CONFIG.lightningcss,
30
- ...config.lightningcss,
31
- filename: id,
32
- code: new TextEncoder().encode(css),
33
- })
34
-
35
- const href = cyrb53([...Object.keys(exports ?? {}), id].join(''))
36
- const classes = exports
37
- ? Object.fromEntries(Object.entries(exports).map(([key, value]) => [key, value.name]))
38
- : {}
39
-
40
- return [
41
- varOne && `const ${varOne} = \`${code}\``,
42
- varTwo && `const ${varTwo} = ${JSON.stringify(classes)}`,
43
- varThree && `const ${varThree} = \`${href}\``,
44
- ]
45
- .filter(Boolean)
46
- .join('\n')
47
- })
48
-
49
- return tmpl
50
- } catch (err) {
51
- console.error('cxx error :(', err)
52
- return source
53
- }
54
- }
1
+ import { cyrb53 } from './cyrb53'
2
+
3
+ import { type TransformOptions, type CustomAtRules, transform } from 'lightningcss'
4
+
5
+ const varMatch =
6
+ /(?:const|var|let)\s*\[(\w+|)\s*,?\s*(\w+|)\s*,?\s*(\w+|)\s*\]\s*=\s*cxx\s*`([\s\S]*?)`/gm
7
+
8
+ export type Config<C extends CustomAtRules = CustomAtRules> = {
9
+ lightningcss?: Partial<TransformOptions<C>>
10
+ }
11
+
12
+ const DEFAULT_CONFIG: Config = {
13
+ lightningcss: {
14
+ minify: true,
15
+ cssModules: true,
16
+ },
17
+ } as const
18
+
19
+ export function inject<C extends CustomAtRules = CustomAtRules>(
20
+ source: string,
21
+ id: string,
22
+ config: Config<C> = {},
23
+ ) {
24
+ try {
25
+ const tmpl = source.replace(varMatch, (_: string, ...args: string[]) => {
26
+ const [varOne, varTwo, varThree, css] = args
27
+
28
+ const { code, exports } = transform({
29
+ ...DEFAULT_CONFIG.lightningcss,
30
+ ...config.lightningcss,
31
+ filename: id,
32
+ code: new TextEncoder().encode(css),
33
+ })
34
+
35
+ const href = cyrb53([...Object.keys(exports ?? {}), id].join(''))
36
+ const classes = exports
37
+ ? Object.fromEntries(Object.entries(exports).map(([key, value]) => [key, value.name]))
38
+ : {}
39
+
40
+ return [
41
+ varOne && `const ${varOne} = \`${code}\``,
42
+ varTwo && `const ${varTwo} = ${JSON.stringify(classes)}`,
43
+ varThree && `const ${varThree} = \`${href}\``,
44
+ ]
45
+ .filter(Boolean)
46
+ .join('\n')
47
+ })
48
+
49
+ return tmpl
50
+ } catch (err) {
51
+ console.error('cxx error :(', err)
52
+ return source
53
+ }
54
+ }
@@ -1,5 +1,5 @@
1
- import { inject } from '../inject'
2
-
3
- export default function loader(source) {
4
- return inject(source, this.resourcePath, this.getOptions())
1
+ import { inject } from '../inject'
2
+
3
+ export default function loader(source) {
4
+ return inject(source, this.resourcePath, this.getOptions())
5
5
  }
package/src/next/index.ts CHANGED
@@ -1,56 +1,56 @@
1
- import path from 'node:path'
2
-
3
- import type { Configuration } from 'webpack'
4
- import type { NextConfig } from 'next'
5
- import type { WebpackConfigContext } from 'next/dist/server/config-shared'
6
-
7
- import type { Config } from '../inject'
8
-
9
- export function withCxx(nextConfig: NextConfig = {}, config: Config = {}) {
10
- const pkg = require('next/package.json')
11
-
12
- const IS_CANARY = pkg.version?.includes('canary') ?? false
13
- const IS_TURBO = process.env.TURBOPACK === '1'
14
- const TURBO_GLOB = '*.{tsx,jsx,ts,js}'
15
-
16
- const loader = path.resolve(__dirname, `cxx-loader.${IS_TURBO ? 'cjs' : 'js'}`)
17
-
18
- const turboConfig = {
19
- ...nextConfig.turbo,
20
- rules: {
21
- ...nextConfig.turbo?.rules,
22
- [TURBO_GLOB]: {
23
- loaders: [
24
- ...(nextConfig.turbo?.rules?.[TURBO_GLOB]?.loaders ?? []),
25
- {
26
- loader,
27
- options: config,
28
- },
29
- ],
30
- },
31
- },
32
- }
33
-
34
- return {
35
- ...nextConfig,
36
- webpack(conf: Configuration, options: WebpackConfigContext) {
37
- const resolvedWebpackConfig = (
38
- typeof nextConfig?.webpack === 'function' ? nextConfig?.webpack(conf, options) : conf
39
- ) satisfies Configuration
40
-
41
- resolvedWebpackConfig?.module?.rules?.push({
42
- test: /\.(js|jsx|ts|tsx)$/,
43
- exclude: /node_modules/,
44
- use: {
45
- loader,
46
- options: config,
47
- },
48
- })
49
-
50
- return resolvedWebpackConfig
51
- },
52
- ...(IS_CANARY
53
- ? { experimental: { ...nextConfig?.experimental, turbo: turboConfig } }
54
- : { turbo: turboConfig }),
55
- }
56
- }
1
+ import path from 'node:path'
2
+
3
+ import type { Configuration } from 'webpack'
4
+ import type { NextConfig } from 'next'
5
+ import type { WebpackConfigContext } from 'next/dist/server/config-shared'
6
+
7
+ import type { Config } from '../inject'
8
+
9
+ export function withCxx(nextConfig: NextConfig = {}, config: Config = {}) {
10
+ const pkg = require('next/package.json')
11
+
12
+ const IS_CANARY = pkg.version?.includes('canary') ?? false
13
+ const IS_TURBO = process.env.TURBOPACK === '1'
14
+ const TURBO_GLOB = '*.{tsx,jsx,ts,js}'
15
+
16
+ const loader = path.resolve(__dirname, `cxx-loader.${IS_TURBO ? 'cjs' : 'js'}`)
17
+
18
+ const turboConfig = {
19
+ ...nextConfig.turbo,
20
+ rules: {
21
+ ...nextConfig.turbo?.rules,
22
+ [TURBO_GLOB]: {
23
+ loaders: [
24
+ ...(nextConfig.turbo?.rules?.[TURBO_GLOB]?.loaders ?? []),
25
+ {
26
+ loader,
27
+ options: config,
28
+ },
29
+ ],
30
+ },
31
+ },
32
+ }
33
+
34
+ return {
35
+ ...nextConfig,
36
+ webpack(conf: Configuration, options: WebpackConfigContext) {
37
+ const resolvedWebpackConfig = (
38
+ typeof nextConfig?.webpack === 'function' ? nextConfig?.webpack(conf, options) : conf
39
+ ) satisfies Configuration
40
+
41
+ resolvedWebpackConfig?.module?.rules?.push({
42
+ test: /\.(js|jsx|ts|tsx)$/,
43
+ exclude: /node_modules/,
44
+ use: {
45
+ loader,
46
+ options: config,
47
+ },
48
+ })
49
+
50
+ return resolvedWebpackConfig
51
+ },
52
+ ...(IS_CANARY
53
+ ? { experimental: { ...nextConfig?.experimental, turbo: turboConfig } }
54
+ : { turbo: turboConfig }),
55
+ }
56
+ }
@@ -1,16 +1,16 @@
1
- import type { Plugin } from 'vite'
2
-
3
- import { inject, type Config } from '../inject'
4
-
5
- export default function vitePluginCxx(config?: Config): Plugin {
6
- return {
7
- name: 'cxx',
8
- enforce: 'pre',
9
- transform(code, id) {
10
- return {
11
- code: inject(code, id, config),
12
- map: null
13
- }
14
- }
15
- }
1
+ import type { Plugin } from 'vite'
2
+
3
+ import { inject, type Config } from '../inject'
4
+
5
+ export default function vitePluginCxx(config?: Config): Plugin {
6
+ return {
7
+ name: 'cxx',
8
+ enforce: 'pre',
9
+ transform(code, id) {
10
+ return {
11
+ code: inject(code, id, config),
12
+ map: null
13
+ }
14
+ }
15
+ }
16
16
  }