@wyw-in-js/bun 1.0.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/README.md +53 -0
- package/esm/index.mjs +192 -0
- package/esm/index.mjs.map +1 -0
- package/lib/index.js +200 -0
- package/lib/index.js.map +1 -0
- package/package.json +43 -0
- package/types/index.d.ts +16 -0
- package/types/index.js +171 -0
package/README.md
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# @wyw-in-js/bun
|
|
2
|
+
|
|
3
|
+
The package contains WyW-in-JS plugin for [Bun](https://bun.sh/) bundler.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```shell
|
|
8
|
+
# npm
|
|
9
|
+
npm i -D @wyw-in-js/bun
|
|
10
|
+
# yarn
|
|
11
|
+
yarn add --dev @wyw-in-js/bun
|
|
12
|
+
# pnpm
|
|
13
|
+
pnpm add -D @wyw-in-js/bun
|
|
14
|
+
# bun
|
|
15
|
+
bun add -d @wyw-in-js/bun
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
```js
|
|
21
|
+
import wyw from '@wyw-in-js/bun';
|
|
22
|
+
|
|
23
|
+
await Bun.build({
|
|
24
|
+
entrypoints: ['src/index.ts'],
|
|
25
|
+
outdir: 'dist',
|
|
26
|
+
plugins: [wyw()],
|
|
27
|
+
});
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Transforming libraries in `node_modules`
|
|
31
|
+
|
|
32
|
+
By default, the Bun plugin skips transforming files from `node_modules` for performance.
|
|
33
|
+
|
|
34
|
+
To transform a specific library, enable `transformLibraries` and narrow `include`/`exclude`:
|
|
35
|
+
|
|
36
|
+
```js
|
|
37
|
+
wyw({
|
|
38
|
+
transformLibraries: true,
|
|
39
|
+
include: [/node_modules\\/(?:@fluentui)\\//],
|
|
40
|
+
});
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Disabling vendor prefixing
|
|
44
|
+
|
|
45
|
+
Stylis adds vendor-prefixed CSS by default. To disable it (and reduce CSS size), pass `prefixer: false`:
|
|
46
|
+
|
|
47
|
+
```js
|
|
48
|
+
wyw({
|
|
49
|
+
prefixer: false,
|
|
50
|
+
});
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
To get details about supported options by the plugin, please check [documentation](https://wyw-in-js.dev/bundlers/bun).
|
package/esm/index.mjs
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { readFileSync } from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { createFilter } from '@rollup/pluginutils';
|
|
4
|
+
import { resolveSync, Transpiler } from 'bun';
|
|
5
|
+
import { asyncResolveFallback } from '@wyw-in-js/shared';
|
|
6
|
+
import { createFileReporter, slugify, transform, TransformCacheCollection } from '@wyw-in-js/transform';
|
|
7
|
+
const nodeModulesRegex = /^(?:.*[\\/])?node_modules(?:[\\/].*)?$/;
|
|
8
|
+
function getLoader(filename) {
|
|
9
|
+
const ext = path.extname(filename);
|
|
10
|
+
switch (ext) {
|
|
11
|
+
case '.jsx':
|
|
12
|
+
return 'jsx';
|
|
13
|
+
case '.ts':
|
|
14
|
+
case '.mts':
|
|
15
|
+
case '.cts':
|
|
16
|
+
return 'ts';
|
|
17
|
+
case '.tsx':
|
|
18
|
+
return 'tsx';
|
|
19
|
+
default:
|
|
20
|
+
return 'js';
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function splitQueryAndHash(request) {
|
|
24
|
+
const queryIdx = request.indexOf('?');
|
|
25
|
+
const hashIdx = request.indexOf('#');
|
|
26
|
+
if (queryIdx === -1 && hashIdx === -1) {
|
|
27
|
+
return {
|
|
28
|
+
specifier: request,
|
|
29
|
+
suffix: ''
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
let startIdx;
|
|
33
|
+
if (queryIdx === -1) {
|
|
34
|
+
startIdx = hashIdx;
|
|
35
|
+
} else if (hashIdx === -1) {
|
|
36
|
+
startIdx = queryIdx;
|
|
37
|
+
} else {
|
|
38
|
+
startIdx = Math.min(queryIdx, hashIdx);
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
specifier: request.slice(0, startIdx),
|
|
42
|
+
suffix: request.slice(startIdx)
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
export default function wywInJS({
|
|
46
|
+
debug,
|
|
47
|
+
include,
|
|
48
|
+
exclude,
|
|
49
|
+
filter = /\.[cm]?[jt]sx?$/,
|
|
50
|
+
nodeModules = false,
|
|
51
|
+
transformLibraries,
|
|
52
|
+
sourceMap,
|
|
53
|
+
keepComments,
|
|
54
|
+
prefixer,
|
|
55
|
+
preprocessor,
|
|
56
|
+
...rest
|
|
57
|
+
} = {}) {
|
|
58
|
+
const cache = new TransformCacheCollection();
|
|
59
|
+
const filterFn = createFilter(include, exclude);
|
|
60
|
+
const allowNodeModules = transformLibraries ?? nodeModules;
|
|
61
|
+
return {
|
|
62
|
+
name: 'wyw-in-js',
|
|
63
|
+
setup(build) {
|
|
64
|
+
const cssLookup = new Map();
|
|
65
|
+
const emittedWarnings = new Set();
|
|
66
|
+
const {
|
|
67
|
+
emitter,
|
|
68
|
+
onDone
|
|
69
|
+
} = createFileReporter(debug ?? false);
|
|
70
|
+
const transpilers = new Map();
|
|
71
|
+
const getTranspiler = loader => {
|
|
72
|
+
const cached = transpilers.get(loader);
|
|
73
|
+
if (cached) {
|
|
74
|
+
return cached;
|
|
75
|
+
}
|
|
76
|
+
const created = new Transpiler({
|
|
77
|
+
loader,
|
|
78
|
+
autoImportJSX: true
|
|
79
|
+
});
|
|
80
|
+
transpilers.set(loader, created);
|
|
81
|
+
return created;
|
|
82
|
+
};
|
|
83
|
+
const asyncResolve = async (what, importer, stack) => {
|
|
84
|
+
const {
|
|
85
|
+
specifier,
|
|
86
|
+
suffix
|
|
87
|
+
} = splitQueryAndHash(what);
|
|
88
|
+
try {
|
|
89
|
+
return (await asyncResolveFallback(specifier, importer, stack)).replace(/\\/g, path.posix.sep).concat(suffix);
|
|
90
|
+
} catch {
|
|
91
|
+
return resolveSync(specifier, importer).replace(/\\/g, path.posix.sep).concat(suffix);
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
const emitWarning = message => {
|
|
95
|
+
const match = message.match(/\nconfig key: (.+)\n/);
|
|
96
|
+
const key = match?.[1] ?? message;
|
|
97
|
+
if (emittedWarnings.has(key)) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
emittedWarnings.add(key);
|
|
101
|
+
// eslint-disable-next-line no-console
|
|
102
|
+
console.warn(message);
|
|
103
|
+
};
|
|
104
|
+
build.onEnd(() => {
|
|
105
|
+
onDone(process.cwd());
|
|
106
|
+
});
|
|
107
|
+
build.onResolve({
|
|
108
|
+
filter: /\.wyw\.css$/
|
|
109
|
+
}, args => ({
|
|
110
|
+
namespace: 'wyw-in-js',
|
|
111
|
+
path: args.path
|
|
112
|
+
}));
|
|
113
|
+
build.onLoad({
|
|
114
|
+
filter: /.*/,
|
|
115
|
+
namespace: 'wyw-in-js'
|
|
116
|
+
}, async args => {
|
|
117
|
+
const contents = cssLookup.get(args.path);
|
|
118
|
+
if (typeof contents === 'undefined') {
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
return {
|
|
122
|
+
contents,
|
|
123
|
+
loader: 'css'
|
|
124
|
+
};
|
|
125
|
+
});
|
|
126
|
+
const filterRegexp = typeof filter === 'string' ? new RegExp(filter) : filter;
|
|
127
|
+
build.onLoad({
|
|
128
|
+
filter: filterRegexp
|
|
129
|
+
}, async args => {
|
|
130
|
+
if (!filterFn(args.path)) {
|
|
131
|
+
return undefined;
|
|
132
|
+
}
|
|
133
|
+
if (!allowNodeModules && nodeModulesRegex.test(args.path)) {
|
|
134
|
+
return undefined;
|
|
135
|
+
}
|
|
136
|
+
const rawCode = readFileSync(args.path, 'utf8');
|
|
137
|
+
const loader = getLoader(args.path);
|
|
138
|
+
const transpiler = getTranspiler(loader);
|
|
139
|
+
const code = transpiler.transformSync(rawCode);
|
|
140
|
+
const transformServices = {
|
|
141
|
+
options: {
|
|
142
|
+
filename: args.path,
|
|
143
|
+
pluginOptions: rest,
|
|
144
|
+
prefixer,
|
|
145
|
+
keepComments,
|
|
146
|
+
preprocessor,
|
|
147
|
+
root: process.cwd()
|
|
148
|
+
},
|
|
149
|
+
cache,
|
|
150
|
+
emitWarning,
|
|
151
|
+
eventEmitter: emitter
|
|
152
|
+
};
|
|
153
|
+
const result = await transform(transformServices, code, asyncResolve);
|
|
154
|
+
const {
|
|
155
|
+
cssText
|
|
156
|
+
} = result;
|
|
157
|
+
if (typeof cssText === 'undefined') {
|
|
158
|
+
return undefined;
|
|
159
|
+
}
|
|
160
|
+
if (cssText === '') {
|
|
161
|
+
let contents = result.code;
|
|
162
|
+
if (sourceMap && result.sourceMap) {
|
|
163
|
+
const map = Buffer.from(JSON.stringify(result.sourceMap)).toString('base64');
|
|
164
|
+
contents += `\n//# sourceMappingURL=data:application/json;base64,${map}`;
|
|
165
|
+
}
|
|
166
|
+
return {
|
|
167
|
+
contents,
|
|
168
|
+
loader
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
const slug = slugify(cssText);
|
|
172
|
+
const cssFilename = `${args.path.replace(/\.[cm]?[jt]sx?$/, '')}_${slug}.wyw.css`.replace(/\\/g, path.posix.sep);
|
|
173
|
+
let nextCssText = cssText;
|
|
174
|
+
let contents = `${result.code}\nimport ${JSON.stringify(cssFilename)};\n`;
|
|
175
|
+
if (sourceMap && result.cssSourceMapText) {
|
|
176
|
+
const map = Buffer.from(result.cssSourceMapText).toString('base64');
|
|
177
|
+
nextCssText += `/*# sourceMappingURL=data:application/json;base64,${map}*/`;
|
|
178
|
+
}
|
|
179
|
+
if (sourceMap && result.sourceMap) {
|
|
180
|
+
const map = Buffer.from(JSON.stringify(result.sourceMap)).toString('base64');
|
|
181
|
+
contents += `//# sourceMappingURL=data:application/json;base64,${map}\n`;
|
|
182
|
+
}
|
|
183
|
+
cssLookup.set(cssFilename, nextCssText);
|
|
184
|
+
return {
|
|
185
|
+
contents,
|
|
186
|
+
loader
|
|
187
|
+
};
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["readFileSync","path","createFilter","resolveSync","Transpiler","asyncResolveFallback","createFileReporter","slugify","transform","TransformCacheCollection","nodeModulesRegex","getLoader","filename","ext","extname","splitQueryAndHash","request","queryIdx","indexOf","hashIdx","specifier","suffix","startIdx","Math","min","slice","wywInJS","debug","include","exclude","filter","nodeModules","transformLibraries","sourceMap","keepComments","prefixer","preprocessor","rest","cache","filterFn","allowNodeModules","name","setup","build","cssLookup","Map","emittedWarnings","Set","emitter","onDone","transpilers","getTranspiler","loader","cached","get","created","autoImportJSX","set","asyncResolve","what","importer","stack","replace","posix","sep","concat","emitWarning","message","match","key","has","add","console","warn","onEnd","process","cwd","onResolve","args","namespace","onLoad","contents","undefined","filterRegexp","RegExp","test","rawCode","transpiler","code","transformSync","transformServices","options","pluginOptions","root","eventEmitter","result","cssText","map","Buffer","from","JSON","stringify","toString","slug","cssFilename","nextCssText","cssSourceMapText"],"sources":["../src/index.ts"],"sourcesContent":["import { readFileSync } from 'fs';\nimport path from 'path';\n\nimport { createFilter } from '@rollup/pluginutils';\nimport type { FilterPattern } from '@rollup/pluginutils';\nimport type { BunPlugin, JavaScriptLoader, PluginBuilder } from 'bun';\nimport { resolveSync, Transpiler } from 'bun';\n\nimport { asyncResolveFallback } from '@wyw-in-js/shared';\nimport type {\n IFileReporterOptions,\n PluginOptions,\n Preprocessor,\n} from '@wyw-in-js/transform';\nimport {\n createFileReporter,\n slugify,\n transform,\n TransformCacheCollection,\n} from '@wyw-in-js/transform';\n\nexport type BunPluginOptions = {\n debug?: IFileReporterOptions | false | null | undefined;\n exclude?: FilterPattern;\n filter?: RegExp | string;\n include?: FilterPattern;\n keepComments?: boolean | RegExp;\n nodeModules?: boolean;\n prefixer?: boolean;\n preprocessor?: Preprocessor;\n sourceMap?: boolean;\n transformLibraries?: boolean;\n} & Partial<PluginOptions>;\n\nconst nodeModulesRegex = /^(?:.*[\\\\/])?node_modules(?:[\\\\/].*)?$/;\n\nfunction getLoader(filename: string): JavaScriptLoader {\n const ext = path.extname(filename);\n switch (ext) {\n case '.jsx':\n return 'jsx';\n case '.ts':\n case '.mts':\n case '.cts':\n return 'ts';\n case '.tsx':\n return 'tsx';\n default:\n return 'js';\n }\n}\n\nfunction splitQueryAndHash(request: string): {\n specifier: string;\n suffix: string;\n} {\n const queryIdx = request.indexOf('?');\n const hashIdx = request.indexOf('#');\n\n if (queryIdx === -1 && hashIdx === -1) {\n return { specifier: request, suffix: '' };\n }\n\n let startIdx: number;\n if (queryIdx === -1) {\n startIdx = hashIdx;\n } else if (hashIdx === -1) {\n startIdx = queryIdx;\n } else {\n startIdx = Math.min(queryIdx, hashIdx);\n }\n\n return {\n specifier: request.slice(0, startIdx),\n suffix: request.slice(startIdx),\n };\n}\n\nexport default function wywInJS({\n debug,\n include,\n exclude,\n filter = /\\.[cm]?[jt]sx?$/,\n nodeModules = false,\n transformLibraries,\n sourceMap,\n keepComments,\n prefixer,\n preprocessor,\n ...rest\n}: BunPluginOptions = {}): BunPlugin {\n const cache = new TransformCacheCollection();\n const filterFn = createFilter(include, exclude);\n const allowNodeModules = transformLibraries ?? nodeModules;\n\n return {\n name: 'wyw-in-js',\n setup(build: PluginBuilder) {\n const cssLookup = new Map<string, string>();\n const emittedWarnings = new Set<string>();\n\n const { emitter, onDone } = createFileReporter(debug ?? false);\n\n const transpilers = new Map<\n JavaScriptLoader,\n InstanceType<typeof Transpiler>\n >();\n const getTranspiler = (\n loader: JavaScriptLoader\n ): InstanceType<typeof Transpiler> => {\n const cached = transpilers.get(loader);\n if (cached) {\n return cached;\n }\n\n const created = new Transpiler({ loader, autoImportJSX: true });\n transpilers.set(loader, created);\n return created;\n };\n\n const asyncResolve = async (\n what: string,\n importer: string,\n stack: string[]\n ): Promise<string | null> => {\n const { specifier, suffix } = splitQueryAndHash(what);\n try {\n return (await asyncResolveFallback(specifier, importer, stack))\n .replace(/\\\\/g, path.posix.sep)\n .concat(suffix);\n } catch {\n return resolveSync(specifier, importer)\n .replace(/\\\\/g, path.posix.sep)\n .concat(suffix);\n }\n };\n\n const emitWarning = (message: string) => {\n const match = message.match(/\\nconfig key: (.+)\\n/);\n const key = match?.[1] ?? message;\n if (emittedWarnings.has(key)) {\n return;\n }\n\n emittedWarnings.add(key);\n // eslint-disable-next-line no-console\n console.warn(message);\n };\n\n build.onEnd(() => {\n onDone(process.cwd());\n });\n\n build.onResolve({ filter: /\\.wyw\\.css$/ }, (args) => ({\n namespace: 'wyw-in-js',\n path: args.path,\n }));\n\n build.onLoad({ filter: /.*/, namespace: 'wyw-in-js' }, async (args) => {\n const contents = cssLookup.get(args.path);\n if (typeof contents === 'undefined') {\n return undefined;\n }\n\n return {\n contents,\n loader: 'css',\n };\n });\n\n const filterRegexp =\n typeof filter === 'string' ? new RegExp(filter) : filter;\n\n build.onLoad({ filter: filterRegexp }, async (args) => {\n if (!filterFn(args.path)) {\n return undefined;\n }\n\n if (!allowNodeModules && nodeModulesRegex.test(args.path)) {\n return undefined;\n }\n\n const rawCode = readFileSync(args.path, 'utf8');\n const loader = getLoader(args.path);\n const transpiler = getTranspiler(loader);\n\n const code = transpiler.transformSync(rawCode);\n\n const transformServices = {\n options: {\n filename: args.path,\n pluginOptions: rest,\n prefixer,\n keepComments,\n preprocessor,\n root: process.cwd(),\n },\n cache,\n emitWarning,\n eventEmitter: emitter,\n };\n\n const result = await transform(transformServices, code, asyncResolve);\n\n const { cssText } = result;\n\n if (typeof cssText === 'undefined') {\n return undefined;\n }\n\n if (cssText === '') {\n let contents = result.code;\n\n if (sourceMap && result.sourceMap) {\n const map = Buffer.from(JSON.stringify(result.sourceMap)).toString(\n 'base64'\n );\n contents += `\\n//# sourceMappingURL=data:application/json;base64,${map}`;\n }\n\n return {\n contents,\n loader,\n };\n }\n\n const slug = slugify(cssText);\n const cssFilename = `${args.path.replace(\n /\\.[cm]?[jt]sx?$/,\n ''\n )}_${slug}.wyw.css`.replace(/\\\\/g, path.posix.sep);\n\n let nextCssText = cssText;\n let contents = `${result.code}\\nimport ${JSON.stringify(\n cssFilename\n )};\\n`;\n\n if (sourceMap && result.cssSourceMapText) {\n const map = Buffer.from(result.cssSourceMapText).toString('base64');\n nextCssText += `/*# sourceMappingURL=data:application/json;base64,${map}*/`;\n }\n\n if (sourceMap && result.sourceMap) {\n const map = Buffer.from(JSON.stringify(result.sourceMap)).toString(\n 'base64'\n );\n contents += `//# sourceMappingURL=data:application/json;base64,${map}\\n`;\n }\n\n cssLookup.set(cssFilename, nextCssText);\n\n return {\n contents,\n loader,\n };\n });\n },\n };\n}\n"],"mappings":"AAAA,SAASA,YAAY,QAAQ,IAAI;AACjC,OAAOC,IAAI,MAAM,MAAM;AAEvB,SAASC,YAAY,QAAQ,qBAAqB;AAGlD,SAASC,WAAW,EAAEC,UAAU,QAAQ,KAAK;AAE7C,SAASC,oBAAoB,QAAQ,mBAAmB;AAMxD,SACEC,kBAAkB,EAClBC,OAAO,EACPC,SAAS,EACTC,wBAAwB,QACnB,sBAAsB;AAe7B,MAAMC,gBAAgB,GAAG,wCAAwC;AAEjE,SAASC,SAASA,CAACC,QAAgB,EAAoB;EACrD,MAAMC,GAAG,GAAGZ,IAAI,CAACa,OAAO,CAACF,QAAQ,CAAC;EAClC,QAAQC,GAAG;IACT,KAAK,MAAM;MACT,OAAO,KAAK;IACd,KAAK,KAAK;IACV,KAAK,MAAM;IACX,KAAK,MAAM;MACT,OAAO,IAAI;IACb,KAAK,MAAM;MACT,OAAO,KAAK;IACd;MACE,OAAO,IAAI;EACf;AACF;AAEA,SAASE,iBAAiBA,CAACC,OAAe,EAGxC;EACA,MAAMC,QAAQ,GAAGD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,IAAI,CAAC6D,OAAO,CACtC,iBAAiB,EACjB,EACF,CAAC,IAAIyC,IAAI,UAAU,CAACzC,OAAO,CAAC,KAAK,EAAE7D,IAAI,CAAC8D,KAAK,CAACC,GAAG,CAAC;QAElD,IAAIyC,WAAW,GAAGT,OAAO;QACzB,IAAIf,QAAQ,GAAG,GAAGc,MAAM,CAACP,IAAI,YAAYY,IAAI,CAACC,SAAS,CACrDG,WACF,CAAC,KAAK;QAEN,IAAIvE,SAAS,IAAI8D,MAAM,CAACW,gBAAgB,EAAE;UACxC,MAAMT,GAAG,GAAGC,MAAM,CAACC,IAAI,CAACJ,MAAM,CAACW,gBAAgB,CAAC,CAACJ,QAAQ,CAAC,QAAQ,CAAC;UACnEG,WAAW,IAAI,qDAAqDR,GAAG,IAAI;QAC7E;QAEA,IAAIhE,SAAS,IAAI8D,MAAM,CAAC9D,SAAS,EAAE;UACjC,MAAMgE,GAAG,GAAGC,MAAM,CAACC,IAAI,CAACC,IAAI,CAACC,SAAS,CAACN,MAAM,CAAC9D,SAAS,CAAC,CAAC,CAACqE,QAAQ,CAChE,QACF,CAAC;UACDrB,QAAQ,IAAI,qDAAqDgB,GAAG,IAAI;QAC1E;QAEArD,SAAS,CAACa,GAAG,CAAC+C,WAAW,EAAEC,WAAW,CAAC;QAEvC,OAAO;UACLxB,QAAQ;UACR7B;QACF,CAAC;MACH,CAAC,CAAC;IACJ;EACF,CAAC;AACH","ignoreList":[]}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = wywInJS;
|
|
7
|
+
var _fs = require("fs");
|
|
8
|
+
var _path = _interopRequireDefault(require("path"));
|
|
9
|
+
var _pluginutils = require("@rollup/pluginutils");
|
|
10
|
+
var _bun = require("bun");
|
|
11
|
+
var _shared = require("@wyw-in-js/shared");
|
|
12
|
+
var _transform = require("@wyw-in-js/transform");
|
|
13
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
14
|
+
const nodeModulesRegex = /^(?:.*[\\/])?node_modules(?:[\\/].*)?$/;
|
|
15
|
+
function getLoader(filename) {
|
|
16
|
+
const ext = _path.default.extname(filename);
|
|
17
|
+
switch (ext) {
|
|
18
|
+
case '.jsx':
|
|
19
|
+
return 'jsx';
|
|
20
|
+
case '.ts':
|
|
21
|
+
case '.mts':
|
|
22
|
+
case '.cts':
|
|
23
|
+
return 'ts';
|
|
24
|
+
case '.tsx':
|
|
25
|
+
return 'tsx';
|
|
26
|
+
default:
|
|
27
|
+
return 'js';
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function splitQueryAndHash(request) {
|
|
31
|
+
const queryIdx = request.indexOf('?');
|
|
32
|
+
const hashIdx = request.indexOf('#');
|
|
33
|
+
if (queryIdx === -1 && hashIdx === -1) {
|
|
34
|
+
return {
|
|
35
|
+
specifier: request,
|
|
36
|
+
suffix: ''
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
let startIdx;
|
|
40
|
+
if (queryIdx === -1) {
|
|
41
|
+
startIdx = hashIdx;
|
|
42
|
+
} else if (hashIdx === -1) {
|
|
43
|
+
startIdx = queryIdx;
|
|
44
|
+
} else {
|
|
45
|
+
startIdx = Math.min(queryIdx, hashIdx);
|
|
46
|
+
}
|
|
47
|
+
return {
|
|
48
|
+
specifier: request.slice(0, startIdx),
|
|
49
|
+
suffix: request.slice(startIdx)
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
function wywInJS({
|
|
53
|
+
debug,
|
|
54
|
+
include,
|
|
55
|
+
exclude,
|
|
56
|
+
filter = /\.[cm]?[jt]sx?$/,
|
|
57
|
+
nodeModules = false,
|
|
58
|
+
transformLibraries,
|
|
59
|
+
sourceMap,
|
|
60
|
+
keepComments,
|
|
61
|
+
prefixer,
|
|
62
|
+
preprocessor,
|
|
63
|
+
...rest
|
|
64
|
+
} = {}) {
|
|
65
|
+
const cache = new _transform.TransformCacheCollection();
|
|
66
|
+
const filterFn = (0, _pluginutils.createFilter)(include, exclude);
|
|
67
|
+
const allowNodeModules = transformLibraries !== null && transformLibraries !== void 0 ? transformLibraries : nodeModules;
|
|
68
|
+
return {
|
|
69
|
+
name: 'wyw-in-js',
|
|
70
|
+
setup(build) {
|
|
71
|
+
const cssLookup = new Map();
|
|
72
|
+
const emittedWarnings = new Set();
|
|
73
|
+
const {
|
|
74
|
+
emitter,
|
|
75
|
+
onDone
|
|
76
|
+
} = (0, _transform.createFileReporter)(debug !== null && debug !== void 0 ? debug : false);
|
|
77
|
+
const transpilers = new Map();
|
|
78
|
+
const getTranspiler = loader => {
|
|
79
|
+
const cached = transpilers.get(loader);
|
|
80
|
+
if (cached) {
|
|
81
|
+
return cached;
|
|
82
|
+
}
|
|
83
|
+
const created = new _bun.Transpiler({
|
|
84
|
+
loader,
|
|
85
|
+
autoImportJSX: true
|
|
86
|
+
});
|
|
87
|
+
transpilers.set(loader, created);
|
|
88
|
+
return created;
|
|
89
|
+
};
|
|
90
|
+
const asyncResolve = async (what, importer, stack) => {
|
|
91
|
+
const {
|
|
92
|
+
specifier,
|
|
93
|
+
suffix
|
|
94
|
+
} = splitQueryAndHash(what);
|
|
95
|
+
try {
|
|
96
|
+
return (await (0, _shared.asyncResolveFallback)(specifier, importer, stack)).replace(/\\/g, _path.default.posix.sep).concat(suffix);
|
|
97
|
+
} catch {
|
|
98
|
+
return (0, _bun.resolveSync)(specifier, importer).replace(/\\/g, _path.default.posix.sep).concat(suffix);
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
const emitWarning = message => {
|
|
102
|
+
var _match$;
|
|
103
|
+
const match = message.match(/\nconfig key: (.+)\n/);
|
|
104
|
+
const key = (_match$ = match === null || match === void 0 ? void 0 : match[1]) !== null && _match$ !== void 0 ? _match$ : message;
|
|
105
|
+
if (emittedWarnings.has(key)) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
emittedWarnings.add(key);
|
|
109
|
+
// eslint-disable-next-line no-console
|
|
110
|
+
console.warn(message);
|
|
111
|
+
};
|
|
112
|
+
build.onEnd(() => {
|
|
113
|
+
onDone(process.cwd());
|
|
114
|
+
});
|
|
115
|
+
build.onResolve({
|
|
116
|
+
filter: /\.wyw\.css$/
|
|
117
|
+
}, args => ({
|
|
118
|
+
namespace: 'wyw-in-js',
|
|
119
|
+
path: args.path
|
|
120
|
+
}));
|
|
121
|
+
build.onLoad({
|
|
122
|
+
filter: /.*/,
|
|
123
|
+
namespace: 'wyw-in-js'
|
|
124
|
+
}, async args => {
|
|
125
|
+
const contents = cssLookup.get(args.path);
|
|
126
|
+
if (typeof contents === 'undefined') {
|
|
127
|
+
return undefined;
|
|
128
|
+
}
|
|
129
|
+
return {
|
|
130
|
+
contents,
|
|
131
|
+
loader: 'css'
|
|
132
|
+
};
|
|
133
|
+
});
|
|
134
|
+
const filterRegexp = typeof filter === 'string' ? new RegExp(filter) : filter;
|
|
135
|
+
build.onLoad({
|
|
136
|
+
filter: filterRegexp
|
|
137
|
+
}, async args => {
|
|
138
|
+
if (!filterFn(args.path)) {
|
|
139
|
+
return undefined;
|
|
140
|
+
}
|
|
141
|
+
if (!allowNodeModules && nodeModulesRegex.test(args.path)) {
|
|
142
|
+
return undefined;
|
|
143
|
+
}
|
|
144
|
+
const rawCode = (0, _fs.readFileSync)(args.path, 'utf8');
|
|
145
|
+
const loader = getLoader(args.path);
|
|
146
|
+
const transpiler = getTranspiler(loader);
|
|
147
|
+
const code = transpiler.transformSync(rawCode);
|
|
148
|
+
const transformServices = {
|
|
149
|
+
options: {
|
|
150
|
+
filename: args.path,
|
|
151
|
+
pluginOptions: rest,
|
|
152
|
+
prefixer,
|
|
153
|
+
keepComments,
|
|
154
|
+
preprocessor,
|
|
155
|
+
root: process.cwd()
|
|
156
|
+
},
|
|
157
|
+
cache,
|
|
158
|
+
emitWarning,
|
|
159
|
+
eventEmitter: emitter
|
|
160
|
+
};
|
|
161
|
+
const result = await (0, _transform.transform)(transformServices, code, asyncResolve);
|
|
162
|
+
const {
|
|
163
|
+
cssText
|
|
164
|
+
} = result;
|
|
165
|
+
if (typeof cssText === 'undefined') {
|
|
166
|
+
return undefined;
|
|
167
|
+
}
|
|
168
|
+
if (cssText === '') {
|
|
169
|
+
let contents = result.code;
|
|
170
|
+
if (sourceMap && result.sourceMap) {
|
|
171
|
+
const map = Buffer.from(JSON.stringify(result.sourceMap)).toString('base64');
|
|
172
|
+
contents += `\n//# sourceMappingURL=data:application/json;base64,${map}`;
|
|
173
|
+
}
|
|
174
|
+
return {
|
|
175
|
+
contents,
|
|
176
|
+
loader
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
const slug = (0, _transform.slugify)(cssText);
|
|
180
|
+
const cssFilename = `${args.path.replace(/\.[cm]?[jt]sx?$/, '')}_${slug}.wyw.css`.replace(/\\/g, _path.default.posix.sep);
|
|
181
|
+
let nextCssText = cssText;
|
|
182
|
+
let contents = `${result.code}\nimport ${JSON.stringify(cssFilename)};\n`;
|
|
183
|
+
if (sourceMap && result.cssSourceMapText) {
|
|
184
|
+
const map = Buffer.from(result.cssSourceMapText).toString('base64');
|
|
185
|
+
nextCssText += `/*# sourceMappingURL=data:application/json;base64,${map}*/`;
|
|
186
|
+
}
|
|
187
|
+
if (sourceMap && result.sourceMap) {
|
|
188
|
+
const map = Buffer.from(JSON.stringify(result.sourceMap)).toString('base64');
|
|
189
|
+
contents += `//# sourceMappingURL=data:application/json;base64,${map}\n`;
|
|
190
|
+
}
|
|
191
|
+
cssLookup.set(cssFilename, nextCssText);
|
|
192
|
+
return {
|
|
193
|
+
contents,
|
|
194
|
+
loader
|
|
195
|
+
};
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["_fs","require","_path","_interopRequireDefault","_pluginutils","_bun","_shared","_transform","e","__esModule","default","nodeModulesRegex","getLoader","filename","ext","path","extname","splitQueryAndHash","request","queryIdx","indexOf","hashIdx","specifier","suffix","startIdx","Math","min","slice","wywInJS","debug","include","exclude","filter","nodeModules","transformLibraries","sourceMap","keepComments","prefixer","preprocessor","rest","cache","TransformCacheCollection","filterFn","createFilter","allowNodeModules","name","setup","build","cssLookup","Map","emittedWarnings","Set","emitter","onDone","createFileReporter","transpilers","getTranspiler","loader","cached","get","created","Transpiler","autoImportJSX","set","asyncResolve","what","importer","stack","asyncResolveFallback","replace","posix","sep","concat","resolveSync","emitWarning","message","_match$","match","key","has","add","console","warn","onEnd","process","cwd","onResolve","args","namespace","onLoad","contents","undefined","filterRegexp","RegExp","test","rawCode","readFileSync","transpiler","code","transformSync","transformServices","options","pluginOptions","root","eventEmitter","result","transform","cssText","map","Buffer","from","JSON","stringify","toString","slug","slugify","cssFilename","nextCssText","cssSourceMapText"],"sources":["../src/index.ts"],"sourcesContent":["import { readFileSync } from 'fs';\nimport path from 'path';\n\nimport { createFilter } from '@rollup/pluginutils';\nimport type { FilterPattern } from '@rollup/pluginutils';\nimport type { BunPlugin, JavaScriptLoader, PluginBuilder } from 'bun';\nimport { resolveSync, Transpiler } from 'bun';\n\nimport { asyncResolveFallback } from '@wyw-in-js/shared';\nimport type {\n IFileReporterOptions,\n PluginOptions,\n Preprocessor,\n} from '@wyw-in-js/transform';\nimport {\n createFileReporter,\n slugify,\n transform,\n TransformCacheCollection,\n} from '@wyw-in-js/transform';\n\nexport type BunPluginOptions = {\n debug?: IFileReporterOptions | false | null | undefined;\n exclude?: FilterPattern;\n filter?: RegExp | string;\n include?: FilterPattern;\n keepComments?: boolean | RegExp;\n nodeModules?: boolean;\n prefixer?: boolean;\n preprocessor?: Preprocessor;\n sourceMap?: boolean;\n transformLibraries?: boolean;\n} & Partial<PluginOptions>;\n\nconst nodeModulesRegex = /^(?:.*[\\\\/])?node_modules(?:[\\\\/].*)?$/;\n\nfunction getLoader(filename: string): JavaScriptLoader {\n const ext = path.extname(filename);\n switch (ext) {\n case '.jsx':\n return 'jsx';\n case '.ts':\n case '.mts':\n case '.cts':\n return 'ts';\n case '.tsx':\n return 'tsx';\n default:\n return 'js';\n }\n}\n\nfunction splitQueryAndHash(request: string): {\n specifier: string;\n suffix: string;\n} {\n const queryIdx = request.indexOf('?');\n const hashIdx = request.indexOf('#');\n\n if (queryIdx === -1 && hashIdx === -1) {\n return { specifier: request, suffix: '' };\n }\n\n let startIdx: number;\n if (queryIdx === -1) {\n startIdx = hashIdx;\n } else if (hashIdx === -1) {\n startIdx = queryIdx;\n } else {\n startIdx = Math.min(queryIdx, hashIdx);\n }\n\n return {\n specifier: request.slice(0, startIdx),\n suffix: request.slice(startIdx),\n };\n}\n\nexport default function wywInJS({\n debug,\n include,\n exclude,\n filter = /\\.[cm]?[jt]sx?$/,\n nodeModules = false,\n transformLibraries,\n sourceMap,\n keepComments,\n prefixer,\n preprocessor,\n ...rest\n}: BunPluginOptions = {}): BunPlugin {\n const cache = new TransformCacheCollection();\n const filterFn = createFilter(include, exclude);\n const allowNodeModules = transformLibraries ?? nodeModules;\n\n return {\n name: 'wyw-in-js',\n setup(build: PluginBuilder) {\n const cssLookup = new Map<string, string>();\n const emittedWarnings = new Set<string>();\n\n const { emitter, onDone } = createFileReporter(debug ?? false);\n\n const transpilers = new Map<\n JavaScriptLoader,\n InstanceType<typeof Transpiler>\n >();\n const getTranspiler = (\n loader: JavaScriptLoader\n ): InstanceType<typeof Transpiler> => {\n const cached = transpilers.get(loader);\n if (cached) {\n return cached;\n }\n\n const created = new Transpiler({ loader, autoImportJSX: true });\n transpilers.set(loader, created);\n return created;\n };\n\n const asyncResolve = async (\n what: string,\n importer: string,\n stack: string[]\n ): Promise<string | null> => {\n const { specifier, suffix } = splitQueryAndHash(what);\n try {\n return (await asyncResolveFallback(specifier, importer, stack))\n .replace(/\\\\/g, path.posix.sep)\n .concat(suffix);\n } catch {\n return resolveSync(specifier, importer)\n .replace(/\\\\/g, path.posix.sep)\n .concat(suffix);\n }\n };\n\n const emitWarning = (message: string) => {\n const match = message.match(/\\nconfig key: (.+)\\n/);\n const key = match?.[1] ?? message;\n if (emittedWarnings.has(key)) {\n return;\n }\n\n emittedWarnings.add(key);\n // eslint-disable-next-line no-console\n console.warn(message);\n };\n\n build.onEnd(() => {\n onDone(process.cwd());\n });\n\n build.onResolve({ filter: /\\.wyw\\.css$/ }, (args) => ({\n namespace: 'wyw-in-js',\n path: args.path,\n }));\n\n build.onLoad({ filter: /.*/, namespace: 'wyw-in-js' }, async (args) => {\n const contents = cssLookup.get(args.path);\n if (typeof contents === 'undefined') {\n return undefined;\n }\n\n return {\n contents,\n loader: 'css',\n };\n });\n\n const filterRegexp =\n typeof filter === 'string' ? new RegExp(filter) : filter;\n\n build.onLoad({ filter: filterRegexp }, async (args) => {\n if (!filterFn(args.path)) {\n return undefined;\n }\n\n if (!allowNodeModules && nodeModulesRegex.test(args.path)) {\n return undefined;\n }\n\n const rawCode = readFileSync(args.path, 'utf8');\n const loader = getLoader(args.path);\n const transpiler = getTranspiler(loader);\n\n const code = transpiler.transformSync(rawCode);\n\n const transformServices = {\n options: {\n filename: args.path,\n pluginOptions: rest,\n prefixer,\n keepComments,\n preprocessor,\n root: process.cwd(),\n },\n cache,\n emitWarning,\n eventEmitter: emitter,\n };\n\n const result = await transform(transformServices, code, asyncResolve);\n\n const { cssText } = result;\n\n if (typeof cssText === 'undefined') {\n return undefined;\n }\n\n if (cssText === '') {\n let contents = result.code;\n\n if (sourceMap && result.sourceMap) {\n const map = Buffer.from(JSON.stringify(result.sourceMap)).toString(\n 'base64'\n );\n contents += `\\n//# sourceMappingURL=data:application/json;base64,${map}`;\n }\n\n return {\n contents,\n loader,\n };\n }\n\n const slug = slugify(cssText);\n const cssFilename = `${args.path.replace(\n /\\.[cm]?[jt]sx?$/,\n ''\n )}_${slug}.wyw.css`.replace(/\\\\/g, path.posix.sep);\n\n let nextCssText = cssText;\n let contents = `${result.code}\\nimport ${JSON.stringify(\n cssFilename\n )};\\n`;\n\n if (sourceMap && result.cssSourceMapText) {\n const map = Buffer.from(result.cssSourceMapText).toString('base64');\n nextCssText += `/*# sourceMappingURL=data:application/json;base64,${map}*/`;\n }\n\n if (sourceMap && result.sourceMap) {\n const map = Buffer.from(JSON.stringify(result.sourceMap)).toString(\n 'base64'\n );\n contents += `//# sourceMappingURL=data:application/json;base64,${map}\\n`;\n }\n\n cssLookup.set(cssFilename, nextCssText);\n\n return {\n contents,\n loader,\n };\n });\n },\n };\n}\n"],"mappings":";;;;;;AAAA,IAAAA,GAAA,GAAAC,OAAA;AACA,IAAAC,KAAA,GAAAC,sBAAA,CAAAF,OAAA;AAEA,IAAAG,YAAA,GAAAH,OAAA;AAGA,IAAAI,IAAA,GAAAJ,OAAA;AAEA,IAAAK,OAAA,GAAAL,OAAA;AAMA,IAAAM,UAAA,GAAAN,OAAA;AAK8B,SAAAE,uBAAAK,CAAA,WAAAA,CAAA,IAAAA,CAAA,CAAAC,UAAA,GAAAD,CAAA,KAAAE,OAAA,EAAAF,CAAA;AAe9B,MAAMG,gBAAgB,GAAG,wCAAwC;AAEjE,SAASC,SAASA,CAACC,QAAgB,EAAoB;EACrD,MAAMC,GAAG,GAAGC,aAAI,CAACC,OAAO,CAACH,QAAQ,CAAC;EAClC,QAAQC,GAAG;IACT,KAAK,MAAM;MACT,OAAO,KAAK;IACd,KAAK,KAAK;IACV,KAAK,MAAM;IACX,KAAK,MAAM;MACT,OAAO,IAAI;IACb,KAAK,MAAM;MACT,OAAO,KAAK;IACd;MACE,OAAO,IAAI;EACf;AACF;AAEA,SAASG,iBAAiBA,CAACC,OAAe,EAGxC;EACA,MAAMC,QAAQ,GAAGD,OAAO,CAACE,OAAO,CAAC,GAAG,CAAC;EACrC,MAAMC,OAAO,GAAGH,OAAO,CAACE,OAAO,CAAC,GAAG,CAAC;EAEpC,IAAID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,CAAC;QAC7B,MAAMS,WAAW,GAAG,GAAG7B,IAAI,CAACxE,IAAI,CAACsD,OAAO,CACtC,iBAAiB,EACjB,EACF,CAAC,IAAI6C,IAAI,UAAU,CAAC7C,OAAO,CAAC,KAAK,EAAEtD,aAAI,CAACuD,KAAK,CAACC,GAAG,CAAC;QAElD,IAAI8C,WAAW,GAAGV,OAAO;QACzB,IAAIjB,QAAQ,GAAG,GAAGe,MAAM,CAACP,IAAI,YAAYa,IAAI,CAACC,SAAS,CACrDI,WACF,CAAC,KAAK;QAEN,IAAIjF,SAAS,IAAIsE,MAAM,CAACa,gBAAgB,EAAE;UACxC,MAAMV,GAAG,GAAGC,MAAM,CAACC,IAAI,CAACL,MAAM,CAACa,gBAAgB,CAAC,CAACL,QAAQ,CAAC,QAAQ,CAAC;UACnEI,WAAW,IAAI,qDAAqDT,GAAG,IAAI;QAC7E;QAEA,IAAIzE,SAAS,IAAIsE,MAAM,CAACtE,SAAS,EAAE;UACjC,MAAMyE,GAAG,GAAGC,MAAM,CAACC,IAAI,CAACC,IAAI,CAACC,SAAS,CAACP,MAAM,CAACtE,SAAS,CAAC,CAAC,CAAC8E,QAAQ,CAChE,QACF,CAAC;UACDvB,QAAQ,IAAI,qDAAqDkB,GAAG,IAAI;QAC1E;QAEA5D,SAAS,CAACe,GAAG,CAACqD,WAAW,EAAEC,WAAW,CAAC;QAEvC,OAAO;UACL3B,QAAQ;UACRjC;QACF,CAAC;MACH,CAAC,CAAC;IACJ;EACF,CAAC;AACH","ignoreList":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@wyw-in-js/bun",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"dependencies": {
|
|
5
|
+
"@rollup/pluginutils": "^5.1.4",
|
|
6
|
+
"bun-types": "^1.3.5",
|
|
7
|
+
"@wyw-in-js/shared": "workspace:*",
|
|
8
|
+
"@wyw-in-js/transform": "workspace:*"
|
|
9
|
+
},
|
|
10
|
+
"devDependencies": {
|
|
11
|
+
"@types/node": "^16.18.55",
|
|
12
|
+
"@wyw-in-js/babel-config": "workspace:*",
|
|
13
|
+
"@wyw-in-js/eslint-config": "workspace:*",
|
|
14
|
+
"@wyw-in-js/ts-config": "workspace:*"
|
|
15
|
+
},
|
|
16
|
+
"engines": {
|
|
17
|
+
"node": ">=16.0.0"
|
|
18
|
+
},
|
|
19
|
+
"exports": {
|
|
20
|
+
"import": "./esm/index.mjs",
|
|
21
|
+
"require": "./lib/index.js",
|
|
22
|
+
"types": "./types/index.d.ts"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"esm/",
|
|
26
|
+
"lib/",
|
|
27
|
+
"types/"
|
|
28
|
+
],
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"main": "lib/index.js",
|
|
31
|
+
"module": "esm/index.mjs",
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build:esm": "babel src --out-dir esm --out-file-extension .mjs --extensions '.js,.jsx,.ts,.tsx' --source-maps --delete-dir-on-start",
|
|
37
|
+
"build:lib": "cross-env NODE_ENV=legacy babel src --out-dir lib --extensions '.js,.jsx,.ts,.tsx' --source-maps --delete-dir-on-start",
|
|
38
|
+
"build:types": "tsc --project ./tsconfig.lib.json --baseUrl . --rootDir ./src",
|
|
39
|
+
"lint": "eslint --ext .js,.ts .",
|
|
40
|
+
"test": "bun test src"
|
|
41
|
+
},
|
|
42
|
+
"types": "types/index.d.ts"
|
|
43
|
+
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { FilterPattern } from '@rollup/pluginutils';
|
|
2
|
+
import type { BunPlugin } from 'bun';
|
|
3
|
+
import type { IFileReporterOptions, PluginOptions, Preprocessor } from '@wyw-in-js/transform';
|
|
4
|
+
export type BunPluginOptions = {
|
|
5
|
+
debug?: IFileReporterOptions | false | null | undefined;
|
|
6
|
+
exclude?: FilterPattern;
|
|
7
|
+
filter?: RegExp | string;
|
|
8
|
+
include?: FilterPattern;
|
|
9
|
+
keepComments?: boolean | RegExp;
|
|
10
|
+
nodeModules?: boolean;
|
|
11
|
+
prefixer?: boolean;
|
|
12
|
+
preprocessor?: Preprocessor;
|
|
13
|
+
sourceMap?: boolean;
|
|
14
|
+
transformLibraries?: boolean;
|
|
15
|
+
} & Partial<PluginOptions>;
|
|
16
|
+
export default function wywInJS({ debug, include, exclude, filter, nodeModules, transformLibraries, sourceMap, keepComments, prefixer, preprocessor, ...rest }?: BunPluginOptions): BunPlugin;
|
package/types/index.js
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.default = wywInJS;
|
|
7
|
+
const fs_1 = require("fs");
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const pluginutils_1 = require("@rollup/pluginutils");
|
|
10
|
+
const bun_1 = require("bun");
|
|
11
|
+
const shared_1 = require("@wyw-in-js/shared");
|
|
12
|
+
const transform_1 = require("@wyw-in-js/transform");
|
|
13
|
+
const nodeModulesRegex = /^(?:.*[\\/])?node_modules(?:[\\/].*)?$/;
|
|
14
|
+
function getLoader(filename) {
|
|
15
|
+
const ext = path_1.default.extname(filename);
|
|
16
|
+
switch (ext) {
|
|
17
|
+
case '.jsx':
|
|
18
|
+
return 'jsx';
|
|
19
|
+
case '.ts':
|
|
20
|
+
case '.mts':
|
|
21
|
+
case '.cts':
|
|
22
|
+
return 'ts';
|
|
23
|
+
case '.tsx':
|
|
24
|
+
return 'tsx';
|
|
25
|
+
default:
|
|
26
|
+
return 'js';
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function splitQueryAndHash(request) {
|
|
30
|
+
const queryIdx = request.indexOf('?');
|
|
31
|
+
const hashIdx = request.indexOf('#');
|
|
32
|
+
if (queryIdx === -1 && hashIdx === -1) {
|
|
33
|
+
return { specifier: request, suffix: '' };
|
|
34
|
+
}
|
|
35
|
+
let startIdx;
|
|
36
|
+
if (queryIdx === -1) {
|
|
37
|
+
startIdx = hashIdx;
|
|
38
|
+
}
|
|
39
|
+
else if (hashIdx === -1) {
|
|
40
|
+
startIdx = queryIdx;
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
startIdx = Math.min(queryIdx, hashIdx);
|
|
44
|
+
}
|
|
45
|
+
return {
|
|
46
|
+
specifier: request.slice(0, startIdx),
|
|
47
|
+
suffix: request.slice(startIdx),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function wywInJS({ debug, include, exclude, filter = /\.[cm]?[jt]sx?$/, nodeModules = false, transformLibraries, sourceMap, keepComments, prefixer, preprocessor, ...rest } = {}) {
|
|
51
|
+
const cache = new transform_1.TransformCacheCollection();
|
|
52
|
+
const filterFn = (0, pluginutils_1.createFilter)(include, exclude);
|
|
53
|
+
const allowNodeModules = transformLibraries ?? nodeModules;
|
|
54
|
+
return {
|
|
55
|
+
name: 'wyw-in-js',
|
|
56
|
+
setup(build) {
|
|
57
|
+
const cssLookup = new Map();
|
|
58
|
+
const emittedWarnings = new Set();
|
|
59
|
+
const { emitter, onDone } = (0, transform_1.createFileReporter)(debug ?? false);
|
|
60
|
+
const transpilers = new Map();
|
|
61
|
+
const getTranspiler = (loader) => {
|
|
62
|
+
const cached = transpilers.get(loader);
|
|
63
|
+
if (cached) {
|
|
64
|
+
return cached;
|
|
65
|
+
}
|
|
66
|
+
const created = new bun_1.Transpiler({ loader, autoImportJSX: true });
|
|
67
|
+
transpilers.set(loader, created);
|
|
68
|
+
return created;
|
|
69
|
+
};
|
|
70
|
+
const asyncResolve = async (what, importer, stack) => {
|
|
71
|
+
const { specifier, suffix } = splitQueryAndHash(what);
|
|
72
|
+
try {
|
|
73
|
+
return (await (0, shared_1.asyncResolveFallback)(specifier, importer, stack))
|
|
74
|
+
.replace(/\\/g, path_1.default.posix.sep)
|
|
75
|
+
.concat(suffix);
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
return (0, bun_1.resolveSync)(specifier, importer)
|
|
79
|
+
.replace(/\\/g, path_1.default.posix.sep)
|
|
80
|
+
.concat(suffix);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
const emitWarning = (message) => {
|
|
84
|
+
const match = message.match(/\nconfig key: (.+)\n/);
|
|
85
|
+
const key = match?.[1] ?? message;
|
|
86
|
+
if (emittedWarnings.has(key)) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
emittedWarnings.add(key);
|
|
90
|
+
// eslint-disable-next-line no-console
|
|
91
|
+
console.warn(message);
|
|
92
|
+
};
|
|
93
|
+
build.onEnd(() => {
|
|
94
|
+
onDone(process.cwd());
|
|
95
|
+
});
|
|
96
|
+
build.onResolve({ filter: /\.wyw\.css$/ }, (args) => ({
|
|
97
|
+
namespace: 'wyw-in-js',
|
|
98
|
+
path: args.path,
|
|
99
|
+
}));
|
|
100
|
+
build.onLoad({ filter: /.*/, namespace: 'wyw-in-js' }, async (args) => {
|
|
101
|
+
const contents = cssLookup.get(args.path);
|
|
102
|
+
if (typeof contents === 'undefined') {
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
return {
|
|
106
|
+
contents,
|
|
107
|
+
loader: 'css',
|
|
108
|
+
};
|
|
109
|
+
});
|
|
110
|
+
const filterRegexp = typeof filter === 'string' ? new RegExp(filter) : filter;
|
|
111
|
+
build.onLoad({ filter: filterRegexp }, async (args) => {
|
|
112
|
+
if (!filterFn(args.path)) {
|
|
113
|
+
return undefined;
|
|
114
|
+
}
|
|
115
|
+
if (!allowNodeModules && nodeModulesRegex.test(args.path)) {
|
|
116
|
+
return undefined;
|
|
117
|
+
}
|
|
118
|
+
const rawCode = (0, fs_1.readFileSync)(args.path, 'utf8');
|
|
119
|
+
const loader = getLoader(args.path);
|
|
120
|
+
const transpiler = getTranspiler(loader);
|
|
121
|
+
const code = transpiler.transformSync(rawCode);
|
|
122
|
+
const transformServices = {
|
|
123
|
+
options: {
|
|
124
|
+
filename: args.path,
|
|
125
|
+
pluginOptions: rest,
|
|
126
|
+
prefixer,
|
|
127
|
+
keepComments,
|
|
128
|
+
preprocessor,
|
|
129
|
+
root: process.cwd(),
|
|
130
|
+
},
|
|
131
|
+
cache,
|
|
132
|
+
emitWarning,
|
|
133
|
+
eventEmitter: emitter,
|
|
134
|
+
};
|
|
135
|
+
const result = await (0, transform_1.transform)(transformServices, code, asyncResolve);
|
|
136
|
+
const { cssText } = result;
|
|
137
|
+
if (typeof cssText === 'undefined') {
|
|
138
|
+
return undefined;
|
|
139
|
+
}
|
|
140
|
+
if (cssText === '') {
|
|
141
|
+
let contents = result.code;
|
|
142
|
+
if (sourceMap && result.sourceMap) {
|
|
143
|
+
const map = Buffer.from(JSON.stringify(result.sourceMap)).toString('base64');
|
|
144
|
+
contents += `\n//# sourceMappingURL=data:application/json;base64,${map}`;
|
|
145
|
+
}
|
|
146
|
+
return {
|
|
147
|
+
contents,
|
|
148
|
+
loader,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
const slug = (0, transform_1.slugify)(cssText);
|
|
152
|
+
const cssFilename = `${args.path.replace(/\.[cm]?[jt]sx?$/, '')}_${slug}.wyw.css`.replace(/\\/g, path_1.default.posix.sep);
|
|
153
|
+
let nextCssText = cssText;
|
|
154
|
+
let contents = `${result.code}\nimport ${JSON.stringify(cssFilename)};\n`;
|
|
155
|
+
if (sourceMap && result.cssSourceMapText) {
|
|
156
|
+
const map = Buffer.from(result.cssSourceMapText).toString('base64');
|
|
157
|
+
nextCssText += `/*# sourceMappingURL=data:application/json;base64,${map}*/`;
|
|
158
|
+
}
|
|
159
|
+
if (sourceMap && result.sourceMap) {
|
|
160
|
+
const map = Buffer.from(JSON.stringify(result.sourceMap)).toString('base64');
|
|
161
|
+
contents += `//# sourceMappingURL=data:application/json;base64,${map}\n`;
|
|
162
|
+
}
|
|
163
|
+
cssLookup.set(cssFilename, nextCssText);
|
|
164
|
+
return {
|
|
165
|
+
contents,
|
|
166
|
+
loader,
|
|
167
|
+
};
|
|
168
|
+
});
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
}
|