@wyw-in-js/rollup 0.1.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.
- package/LICENSE +21 -0
- package/esm/index.js +103 -0
- package/esm/index.js.map +1 -0
- package/lib/index.js +109 -0
- package/lib/index.js.map +1 -0
- package/package.json +47 -0
- package/types/index.d.ts +15 -0
- package/types/index.js +93 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Anton Evzhakov
|
|
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
|
+
SOFTWARE.
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file contains a Rollup loader for wyw-in-js.
|
|
3
|
+
* It uses the transform.ts function to generate class names from source code,
|
|
4
|
+
* returns transformed code without template literals and attaches generated source maps
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { createFilter } from '@rollup/pluginutils';
|
|
8
|
+
import { logger, slugify, syncResolve } from '@wyw-in-js/shared';
|
|
9
|
+
import { getFileIdx, transform, TransformCacheCollection } from '@wyw-in-js/transform';
|
|
10
|
+
export default function wywInJS({
|
|
11
|
+
exclude,
|
|
12
|
+
include,
|
|
13
|
+
preprocessor,
|
|
14
|
+
sourceMap,
|
|
15
|
+
...rest
|
|
16
|
+
} = {}) {
|
|
17
|
+
const filter = createFilter(include, exclude);
|
|
18
|
+
const cssLookup = {};
|
|
19
|
+
const cache = new TransformCacheCollection();
|
|
20
|
+
const emptyConfig = {};
|
|
21
|
+
const plugin = {
|
|
22
|
+
name: 'wyw-in-js',
|
|
23
|
+
load(id) {
|
|
24
|
+
return cssLookup[id];
|
|
25
|
+
},
|
|
26
|
+
/* eslint-disable-next-line consistent-return */
|
|
27
|
+
resolveId(importee) {
|
|
28
|
+
if (importee in cssLookup) return importee;
|
|
29
|
+
},
|
|
30
|
+
async transform(code, id) {
|
|
31
|
+
// Do not transform ignored and generated files
|
|
32
|
+
if (!filter(id) || id in cssLookup) return;
|
|
33
|
+
const log = logger.extend('rollup').extend(getFileIdx(id));
|
|
34
|
+
log('init %s', id);
|
|
35
|
+
const asyncResolve = async (what, importer, stack) => {
|
|
36
|
+
const resolved = await this.resolve(what, importer);
|
|
37
|
+
if (resolved) {
|
|
38
|
+
if (resolved.external) {
|
|
39
|
+
// If module is marked as external, Rollup will not resolve it,
|
|
40
|
+
// so we need to resolve it ourselves with default resolver
|
|
41
|
+
const resolvedId = syncResolve(what, importer, stack);
|
|
42
|
+
log("resolve: ✅ '%s'@'%s -> %O\n%s", what, importer, resolved);
|
|
43
|
+
return resolvedId;
|
|
44
|
+
}
|
|
45
|
+
log("resolve: ✅ '%s'@'%s -> %O\n%s", what, importer, resolved);
|
|
46
|
+
|
|
47
|
+
// Vite adds param like `?v=667939b3` to cached modules
|
|
48
|
+
const resolvedId = resolved.id.split('?')[0];
|
|
49
|
+
if (resolvedId.startsWith('\0')) {
|
|
50
|
+
// \0 is a special character in Rollup that tells Rollup to not include this in the bundle
|
|
51
|
+
// https://rollupjs.org/guide/en/#outputexports
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
return resolvedId;
|
|
55
|
+
}
|
|
56
|
+
log("resolve: ❌ '%s'@'%s", what, importer);
|
|
57
|
+
throw new Error(`Could not resolve ${what}`);
|
|
58
|
+
};
|
|
59
|
+
const transformServices = {
|
|
60
|
+
options: {
|
|
61
|
+
filename: id,
|
|
62
|
+
root: process.cwd(),
|
|
63
|
+
preprocessor,
|
|
64
|
+
pluginOptions: rest
|
|
65
|
+
},
|
|
66
|
+
cache
|
|
67
|
+
};
|
|
68
|
+
const result = await transform(transformServices, code, asyncResolve, emptyConfig);
|
|
69
|
+
if (!result.cssText) return;
|
|
70
|
+
let {
|
|
71
|
+
cssText
|
|
72
|
+
} = result;
|
|
73
|
+
const slug = slugify(cssText);
|
|
74
|
+
const filename = `${id.replace(/\.[jt]sx?$/, '')}_${slug}.css`;
|
|
75
|
+
if (sourceMap && result.cssSourceMapText) {
|
|
76
|
+
const map = Buffer.from(result.cssSourceMapText).toString('base64');
|
|
77
|
+
cssText += `/*# sourceMappingURL=data:application/json;base64,${map}*/`;
|
|
78
|
+
}
|
|
79
|
+
cssLookup[filename] = cssText;
|
|
80
|
+
result.code += `\nimport ${JSON.stringify(filename)};\n`;
|
|
81
|
+
|
|
82
|
+
/* eslint-disable-next-line consistent-return */
|
|
83
|
+
return {
|
|
84
|
+
code: result.code,
|
|
85
|
+
map: result.sourceMap
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
return new Proxy(plugin, {
|
|
90
|
+
get(target, prop) {
|
|
91
|
+
return target[prop];
|
|
92
|
+
},
|
|
93
|
+
getOwnPropertyDescriptor(target, prop) {
|
|
94
|
+
return Object.getOwnPropertyDescriptor(target, prop);
|
|
95
|
+
},
|
|
96
|
+
ownKeys() {
|
|
97
|
+
// Rollup doesn't ask config about its own keys, so it is Vite.
|
|
98
|
+
|
|
99
|
+
throw new Error('You are trying to use @wyw-in-js/rollup with Vite. Please use @wyw-in-js/vite instead.');
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
//# sourceMappingURL=index.js.map
|
package/esm/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["createFilter","logger","slugify","syncResolve","getFileIdx","transform","TransformCacheCollection","wywInJS","exclude","include","preprocessor","sourceMap","rest","filter","cssLookup","cache","emptyConfig","plugin","name","load","id","resolveId","importee","code","log","extend","asyncResolve","what","importer","stack","resolved","resolve","external","resolvedId","split","startsWith","Error","transformServices","options","filename","root","process","cwd","pluginOptions","result","cssText","slug","replace","cssSourceMapText","map","Buffer","from","toString","JSON","stringify","Proxy","get","target","prop","getOwnPropertyDescriptor","Object","ownKeys"],"sources":["../src/index.ts"],"sourcesContent":["/**\n * This file contains a Rollup loader for wyw-in-js.\n * It uses the transform.ts function to generate class names from source code,\n * returns transformed code without template literals and attaches generated source maps\n */\n\nimport { createFilter } from '@rollup/pluginutils';\nimport type { Plugin } from 'rollup';\n\nimport { logger, slugify, syncResolve } from '@wyw-in-js/shared';\nimport type { PluginOptions, Preprocessor, Result } from '@wyw-in-js/transform';\nimport {\n getFileIdx,\n transform,\n TransformCacheCollection,\n} from '@wyw-in-js/transform';\n\ntype RollupPluginOptions = {\n exclude?: string | string[];\n include?: string | string[];\n preprocessor?: Preprocessor;\n sourceMap?: boolean;\n} & Partial<PluginOptions>;\n\nexport default function wywInJS({\n exclude,\n include,\n preprocessor,\n sourceMap,\n ...rest\n}: RollupPluginOptions = {}): Plugin {\n const filter = createFilter(include, exclude);\n const cssLookup: { [key: string]: string } = {};\n const cache = new TransformCacheCollection();\n const emptyConfig = {};\n\n const plugin: Plugin = {\n name: 'wyw-in-js',\n load(id: string) {\n return cssLookup[id];\n },\n /* eslint-disable-next-line consistent-return */\n resolveId(importee: string) {\n if (importee in cssLookup) return importee;\n },\n async transform(\n code: string,\n id: string\n ): Promise<{ code: string; map: Result['sourceMap'] } | undefined> {\n // Do not transform ignored and generated files\n if (!filter(id) || id in cssLookup) return;\n\n const log = logger.extend('rollup').extend(getFileIdx(id));\n\n log('init %s', id);\n\n const asyncResolve = async (\n what: string,\n importer: string,\n stack: string[]\n ) => {\n const resolved = await this.resolve(what, importer);\n if (resolved) {\n if (resolved.external) {\n // If module is marked as external, Rollup will not resolve it,\n // so we need to resolve it ourselves with default resolver\n const resolvedId = syncResolve(what, importer, stack);\n log(\"resolve: ✅ '%s'@'%s -> %O\\n%s\", what, importer, resolved);\n return resolvedId;\n }\n\n log(\"resolve: ✅ '%s'@'%s -> %O\\n%s\", what, importer, resolved);\n\n // Vite adds param like `?v=667939b3` to cached modules\n const resolvedId = resolved.id.split('?')[0];\n\n if (resolvedId.startsWith('\\0')) {\n // \\0 is a special character in Rollup that tells Rollup to not include this in the bundle\n // https://rollupjs.org/guide/en/#outputexports\n return null;\n }\n\n return resolvedId;\n }\n\n log(\"resolve: ❌ '%s'@'%s\", what, importer);\n throw new Error(`Could not resolve ${what}`);\n };\n\n const transformServices = {\n options: {\n filename: id,\n root: process.cwd(),\n preprocessor,\n pluginOptions: rest,\n },\n cache,\n };\n\n const result = await transform(\n transformServices,\n code,\n asyncResolve,\n emptyConfig\n );\n\n if (!result.cssText) return;\n\n let { cssText } = result;\n\n const slug = slugify(cssText);\n const filename = `${id.replace(/\\.[jt]sx?$/, '')}_${slug}.css`;\n\n if (sourceMap && result.cssSourceMapText) {\n const map = Buffer.from(result.cssSourceMapText).toString('base64');\n cssText += `/*# sourceMappingURL=data:application/json;base64,${map}*/`;\n }\n\n cssLookup[filename] = cssText;\n\n result.code += `\\nimport ${JSON.stringify(filename)};\\n`;\n\n /* eslint-disable-next-line consistent-return */\n return { code: result.code, map: result.sourceMap };\n },\n };\n\n return new Proxy<Plugin>(plugin, {\n get(target, prop) {\n return target[prop as keyof Plugin];\n },\n\n getOwnPropertyDescriptor(target, prop) {\n return Object.getOwnPropertyDescriptor(target, prop as keyof Plugin);\n },\n\n ownKeys() {\n // Rollup doesn't ask config about its own keys, so it is Vite.\n\n throw new Error(\n 'You are trying to use @wyw-in-js/rollup with Vite. Please use @wyw-in-js/vite instead.'\n );\n },\n });\n}\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;;AAEA,SAASA,YAAY,QAAQ,qBAAqB;AAGlD,SAASC,MAAM,EAAEC,OAAO,EAAEC,WAAW,QAAQ,mBAAmB;AAEhE,SACEC,UAAU,EACVC,SAAS,EACTC,wBAAwB,QACnB,sBAAsB;AAS7B,eAAe,SAASC,OAAOA,CAAC;EAC9BC,OAAO;EACPC,OAAO;EACPC,YAAY;EACZC,SAAS;EACT,GAAGC;AACgB,CAAC,GAAG,CAAC,CAAC,EAAU;EACnC,MAAMC,MAAM,GAAGb,YAAY,CAACS,OAAO,EAAED,OAAO,CAAC;EAC7C,MAAMM,SAAoC,GAAG,CAAC,CAAC;EAC/C,MAAMC,KAAK,GAAG,IAAIT,wBAAwB,CAAC,CAAC;EAC5C,MAAMU,WAAW,GAAG,CAAC,CAAC;EAEtB,MAAMC,MAAc,GAAG;IACrBC,IAAI,EAAE,WAAW;IACjBC,IAAIA,CAACC,EAAU,EAAE;MACf,OAAON,SAAS,CAACM,EAAE,CAAC;IACtB,CAAC;IACD;IACAC,SAASA,CAACC,QAAgB,EAAE;MAC1B,IAAIA,QAAQ,IAAIR,SAAS,EAAE,OAAOQ,QAAQ;IAC5C,CAAC;IACD,MAAMjB,SAASA,CACbkB,IAAY,EACZH,EAAU,EACuD;MACjE;MACA,IAAI,CAACP,MAAM,CAACO,EAAE,CAAC,IAAIA,EAAE,IAAIN,SAAS,EAAE;MAEp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
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = wywInJS;
|
|
7
|
+
var _pluginutils = require("@rollup/pluginutils");
|
|
8
|
+
var _shared = require("@wyw-in-js/shared");
|
|
9
|
+
var _transform = require("@wyw-in-js/transform");
|
|
10
|
+
/**
|
|
11
|
+
* This file contains a Rollup loader for wyw-in-js.
|
|
12
|
+
* It uses the transform.ts function to generate class names from source code,
|
|
13
|
+
* returns transformed code without template literals and attaches generated source maps
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
function wywInJS({
|
|
17
|
+
exclude,
|
|
18
|
+
include,
|
|
19
|
+
preprocessor,
|
|
20
|
+
sourceMap,
|
|
21
|
+
...rest
|
|
22
|
+
} = {}) {
|
|
23
|
+
const filter = (0, _pluginutils.createFilter)(include, exclude);
|
|
24
|
+
const cssLookup = {};
|
|
25
|
+
const cache = new _transform.TransformCacheCollection();
|
|
26
|
+
const emptyConfig = {};
|
|
27
|
+
const plugin = {
|
|
28
|
+
name: 'wyw-in-js',
|
|
29
|
+
load(id) {
|
|
30
|
+
return cssLookup[id];
|
|
31
|
+
},
|
|
32
|
+
/* eslint-disable-next-line consistent-return */
|
|
33
|
+
resolveId(importee) {
|
|
34
|
+
if (importee in cssLookup) return importee;
|
|
35
|
+
},
|
|
36
|
+
async transform(code, id) {
|
|
37
|
+
// Do not transform ignored and generated files
|
|
38
|
+
if (!filter(id) || id in cssLookup) return;
|
|
39
|
+
const log = _shared.logger.extend('rollup').extend((0, _transform.getFileIdx)(id));
|
|
40
|
+
log('init %s', id);
|
|
41
|
+
const asyncResolve = async (what, importer, stack) => {
|
|
42
|
+
const resolved = await this.resolve(what, importer);
|
|
43
|
+
if (resolved) {
|
|
44
|
+
if (resolved.external) {
|
|
45
|
+
// If module is marked as external, Rollup will not resolve it,
|
|
46
|
+
// so we need to resolve it ourselves with default resolver
|
|
47
|
+
const resolvedId = (0, _shared.syncResolve)(what, importer, stack);
|
|
48
|
+
log("resolve: ✅ '%s'@'%s -> %O\n%s", what, importer, resolved);
|
|
49
|
+
return resolvedId;
|
|
50
|
+
}
|
|
51
|
+
log("resolve: ✅ '%s'@'%s -> %O\n%s", what, importer, resolved);
|
|
52
|
+
|
|
53
|
+
// Vite adds param like `?v=667939b3` to cached modules
|
|
54
|
+
const resolvedId = resolved.id.split('?')[0];
|
|
55
|
+
if (resolvedId.startsWith('\0')) {
|
|
56
|
+
// \0 is a special character in Rollup that tells Rollup to not include this in the bundle
|
|
57
|
+
// https://rollupjs.org/guide/en/#outputexports
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
return resolvedId;
|
|
61
|
+
}
|
|
62
|
+
log("resolve: ❌ '%s'@'%s", what, importer);
|
|
63
|
+
throw new Error(`Could not resolve ${what}`);
|
|
64
|
+
};
|
|
65
|
+
const transformServices = {
|
|
66
|
+
options: {
|
|
67
|
+
filename: id,
|
|
68
|
+
root: process.cwd(),
|
|
69
|
+
preprocessor,
|
|
70
|
+
pluginOptions: rest
|
|
71
|
+
},
|
|
72
|
+
cache
|
|
73
|
+
};
|
|
74
|
+
const result = await (0, _transform.transform)(transformServices, code, asyncResolve, emptyConfig);
|
|
75
|
+
if (!result.cssText) return;
|
|
76
|
+
let {
|
|
77
|
+
cssText
|
|
78
|
+
} = result;
|
|
79
|
+
const slug = (0, _shared.slugify)(cssText);
|
|
80
|
+
const filename = `${id.replace(/\.[jt]sx?$/, '')}_${slug}.css`;
|
|
81
|
+
if (sourceMap && result.cssSourceMapText) {
|
|
82
|
+
const map = Buffer.from(result.cssSourceMapText).toString('base64');
|
|
83
|
+
cssText += `/*# sourceMappingURL=data:application/json;base64,${map}*/`;
|
|
84
|
+
}
|
|
85
|
+
cssLookup[filename] = cssText;
|
|
86
|
+
result.code += `\nimport ${JSON.stringify(filename)};\n`;
|
|
87
|
+
|
|
88
|
+
/* eslint-disable-next-line consistent-return */
|
|
89
|
+
return {
|
|
90
|
+
code: result.code,
|
|
91
|
+
map: result.sourceMap
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
return new Proxy(plugin, {
|
|
96
|
+
get(target, prop) {
|
|
97
|
+
return target[prop];
|
|
98
|
+
},
|
|
99
|
+
getOwnPropertyDescriptor(target, prop) {
|
|
100
|
+
return Object.getOwnPropertyDescriptor(target, prop);
|
|
101
|
+
},
|
|
102
|
+
ownKeys() {
|
|
103
|
+
// Rollup doesn't ask config about its own keys, so it is Vite.
|
|
104
|
+
|
|
105
|
+
throw new Error('You are trying to use @wyw-in-js/rollup with Vite. Please use @wyw-in-js/vite instead.');
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["_pluginutils","require","_shared","_transform","wywInJS","exclude","include","preprocessor","sourceMap","rest","filter","createFilter","cssLookup","cache","TransformCacheCollection","emptyConfig","plugin","name","load","id","resolveId","importee","transform","code","log","logger","extend","getFileIdx","asyncResolve","what","importer","stack","resolved","resolve","external","resolvedId","syncResolve","split","startsWith","Error","transformServices","options","filename","root","process","cwd","pluginOptions","result","cssText","slug","slugify","replace","cssSourceMapText","map","Buffer","from","toString","JSON","stringify","Proxy","get","target","prop","getOwnPropertyDescriptor","Object","ownKeys"],"sources":["../src/index.ts"],"sourcesContent":["/**\n * This file contains a Rollup loader for wyw-in-js.\n * It uses the transform.ts function to generate class names from source code,\n * returns transformed code without template literals and attaches generated source maps\n */\n\nimport { createFilter } from '@rollup/pluginutils';\nimport type { Plugin } from 'rollup';\n\nimport { logger, slugify, syncResolve } from '@wyw-in-js/shared';\nimport type { PluginOptions, Preprocessor, Result } from '@wyw-in-js/transform';\nimport {\n getFileIdx,\n transform,\n TransformCacheCollection,\n} from '@wyw-in-js/transform';\n\ntype RollupPluginOptions = {\n exclude?: string | string[];\n include?: string | string[];\n preprocessor?: Preprocessor;\n sourceMap?: boolean;\n} & Partial<PluginOptions>;\n\nexport default function wywInJS({\n exclude,\n include,\n preprocessor,\n sourceMap,\n ...rest\n}: RollupPluginOptions = {}): Plugin {\n const filter = createFilter(include, exclude);\n const cssLookup: { [key: string]: string } = {};\n const cache = new TransformCacheCollection();\n const emptyConfig = {};\n\n const plugin: Plugin = {\n name: 'wyw-in-js',\n load(id: string) {\n return cssLookup[id];\n },\n /* eslint-disable-next-line consistent-return */\n resolveId(importee: string) {\n if (importee in cssLookup) return importee;\n },\n async transform(\n code: string,\n id: string\n ): Promise<{ code: string; map: Result['sourceMap'] } | undefined> {\n // Do not transform ignored and generated files\n if (!filter(id) || id in cssLookup) return;\n\n const log = logger.extend('rollup').extend(getFileIdx(id));\n\n log('init %s', id);\n\n const asyncResolve = async (\n what: string,\n importer: string,\n stack: string[]\n ) => {\n const resolved = await this.resolve(what, importer);\n if (resolved) {\n if (resolved.external) {\n // If module is marked as external, Rollup will not resolve it,\n // so we need to resolve it ourselves with default resolver\n const resolvedId = syncResolve(what, importer, stack);\n log(\"resolve: ✅ '%s'@'%s -> %O\\n%s\", what, importer, resolved);\n return resolvedId;\n }\n\n log(\"resolve: ✅ '%s'@'%s -> %O\\n%s\", what, importer, resolved);\n\n // Vite adds param like `?v=667939b3` to cached modules\n const resolvedId = resolved.id.split('?')[0];\n\n if (resolvedId.startsWith('\\0')) {\n // \\0 is a special character in Rollup that tells Rollup to not include this in the bundle\n // https://rollupjs.org/guide/en/#outputexports\n return null;\n }\n\n return resolvedId;\n }\n\n log(\"resolve: ❌ '%s'@'%s\", what, importer);\n throw new Error(`Could not resolve ${what}`);\n };\n\n const transformServices = {\n options: {\n filename: id,\n root: process.cwd(),\n preprocessor,\n pluginOptions: rest,\n },\n cache,\n };\n\n const result = await transform(\n transformServices,\n code,\n asyncResolve,\n emptyConfig\n );\n\n if (!result.cssText) return;\n\n let { cssText } = result;\n\n const slug = slugify(cssText);\n const filename = `${id.replace(/\\.[jt]sx?$/, '')}_${slug}.css`;\n\n if (sourceMap && result.cssSourceMapText) {\n const map = Buffer.from(result.cssSourceMapText).toString('base64');\n cssText += `/*# sourceMappingURL=data:application/json;base64,${map}*/`;\n }\n\n cssLookup[filename] = cssText;\n\n result.code += `\\nimport ${JSON.stringify(filename)};\\n`;\n\n /* eslint-disable-next-line consistent-return */\n return { code: result.code, map: result.sourceMap };\n },\n };\n\n return new Proxy<Plugin>(plugin, {\n get(target, prop) {\n return target[prop as keyof Plugin];\n },\n\n getOwnPropertyDescriptor(target, prop) {\n return Object.getOwnPropertyDescriptor(target, prop as keyof Plugin);\n },\n\n ownKeys() {\n // Rollup doesn't ask config about its own keys, so it is Vite.\n\n throw new Error(\n 'You are trying to use @wyw-in-js/rollup with Vite. Please use @wyw-in-js/vite instead.'\n );\n },\n });\n}\n"],"mappings":";;;;;;AAMA,IAAAA,YAAA,GAAAC,OAAA;AAGA,IAAAC,OAAA,GAAAD,OAAA;AAEA,IAAAE,UAAA,GAAAF,OAAA;AAXA;AACA;AACA;AACA;AACA;;AAoBe,SAASG,OAAOA,CAAC;EAC9BC,OAAO;EACPC,OAAO;EACPC,YAAY;EACZC,SAAS;EACT,GAAGC;AACgB,CAAC,GAAG,CAAC,CAAC,EAAU;EACnC,MAAMC,MAAM,GAAG,IAAAC,yBAAY,EAACL,OAAO,EAAED,OAAO,CAAC;EAC7C,MAAMO,SAAoC,GAAG,CAAC,CAAC;EAC/C,MAAMC,KAAK,GAAG,IAAIC,mCAAwB,CAAC,CAAC;EAC5C,MAAMC,WAAW,GAAG,CAAC,CAAC;EAEtB,MAAMC,MAAc,GAAG;IACrBC,IAAI,EAAE,WAAW;IACjBC,IAAIA,CAACC,EAAU,EAAE;MACf,OAAOP,SAAS,CAACO,EAAE,CAAC;IACtB,CAAC;IACD;IACAC,SAASA,CAACC,QAAgB,EAAE;MAC1B,IAAIA,QAAQ,IAAIT,SAAS,EAAE,OAAOS,QAAQ;IAC5C,CAAC;IACD,MAAMC,SA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IAAI1B,KAAK,CACb,wFACF,CAAC;IACH;EACF,CAAC,CAAC;AACJ"}
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@wyw-in-js/rollup",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"dependencies": {
|
|
5
|
+
"@rollup/pluginutils": "^5.0.4",
|
|
6
|
+
"@wyw-in-js/shared": "0.1.1",
|
|
7
|
+
"@wyw-in-js/transform": "0.1.1"
|
|
8
|
+
},
|
|
9
|
+
"devDependencies": {
|
|
10
|
+
"@types/node": "^16.18.55",
|
|
11
|
+
"rollup": "^3.11.0",
|
|
12
|
+
"source-map": "^0.7.4",
|
|
13
|
+
"@wyw-in-js/babel-config": "0.1.1",
|
|
14
|
+
"@wyw-in-js/eslint-config": "0.1.1",
|
|
15
|
+
"@wyw-in-js/jest-preset": "0.1.1",
|
|
16
|
+
"@wyw-in-js/ts-config": "0.1.1"
|
|
17
|
+
},
|
|
18
|
+
"engines": {
|
|
19
|
+
"node": ">=16.0.0"
|
|
20
|
+
},
|
|
21
|
+
"exports": {
|
|
22
|
+
"import": "./esm/index.js",
|
|
23
|
+
"require": "./lib/index.js",
|
|
24
|
+
"types": "./types/index.d.ts"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"esm/",
|
|
28
|
+
"lib/",
|
|
29
|
+
"types/"
|
|
30
|
+
],
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"main": "lib/index.js",
|
|
33
|
+
"module": "esm/index.js",
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"rollup": "1.20.0||^2.0.0||^3.0.0"
|
|
36
|
+
},
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public"
|
|
39
|
+
},
|
|
40
|
+
"types": "types/index.d.ts",
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build:esm": "babel src --out-dir esm --extensions '.js,.jsx,.ts,.tsx' --source-maps --delete-dir-on-start",
|
|
43
|
+
"build:lib": "cross-env NODE_ENV=legacy babel src --out-dir lib --extensions '.js,.jsx,.ts,.tsx' --source-maps --delete-dir-on-start",
|
|
44
|
+
"build:types": "tsc --project ./tsconfig.lib.json --baseUrl . --rootDir ./src",
|
|
45
|
+
"lint": "eslint --ext .js,.ts ."
|
|
46
|
+
}
|
|
47
|
+
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file contains a Rollup loader for wyw-in-js.
|
|
3
|
+
* It uses the transform.ts function to generate class names from source code,
|
|
4
|
+
* returns transformed code without template literals and attaches generated source maps
|
|
5
|
+
*/
|
|
6
|
+
import type { Plugin } from 'rollup';
|
|
7
|
+
import type { PluginOptions, Preprocessor } from '@wyw-in-js/transform';
|
|
8
|
+
type RollupPluginOptions = {
|
|
9
|
+
exclude?: string | string[];
|
|
10
|
+
include?: string | string[];
|
|
11
|
+
preprocessor?: Preprocessor;
|
|
12
|
+
sourceMap?: boolean;
|
|
13
|
+
} & Partial<PluginOptions>;
|
|
14
|
+
export default function wywInJS({ exclude, include, preprocessor, sourceMap, ...rest }?: RollupPluginOptions): Plugin;
|
|
15
|
+
export {};
|
package/types/index.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* This file contains a Rollup loader for wyw-in-js.
|
|
4
|
+
* It uses the transform.ts function to generate class names from source code,
|
|
5
|
+
* returns transformed code without template literals and attaches generated source maps
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
const pluginutils_1 = require("@rollup/pluginutils");
|
|
9
|
+
const shared_1 = require("@wyw-in-js/shared");
|
|
10
|
+
const transform_1 = require("@wyw-in-js/transform");
|
|
11
|
+
function wywInJS({ exclude, include, preprocessor, sourceMap, ...rest } = {}) {
|
|
12
|
+
const filter = (0, pluginutils_1.createFilter)(include, exclude);
|
|
13
|
+
const cssLookup = {};
|
|
14
|
+
const cache = new transform_1.TransformCacheCollection();
|
|
15
|
+
const emptyConfig = {};
|
|
16
|
+
const plugin = {
|
|
17
|
+
name: 'wyw-in-js',
|
|
18
|
+
load(id) {
|
|
19
|
+
return cssLookup[id];
|
|
20
|
+
},
|
|
21
|
+
/* eslint-disable-next-line consistent-return */
|
|
22
|
+
resolveId(importee) {
|
|
23
|
+
if (importee in cssLookup)
|
|
24
|
+
return importee;
|
|
25
|
+
},
|
|
26
|
+
async transform(code, id) {
|
|
27
|
+
// Do not transform ignored and generated files
|
|
28
|
+
if (!filter(id) || id in cssLookup)
|
|
29
|
+
return;
|
|
30
|
+
const log = shared_1.logger.extend('rollup').extend((0, transform_1.getFileIdx)(id));
|
|
31
|
+
log('init %s', id);
|
|
32
|
+
const asyncResolve = async (what, importer, stack) => {
|
|
33
|
+
const resolved = await this.resolve(what, importer);
|
|
34
|
+
if (resolved) {
|
|
35
|
+
if (resolved.external) {
|
|
36
|
+
// If module is marked as external, Rollup will not resolve it,
|
|
37
|
+
// so we need to resolve it ourselves with default resolver
|
|
38
|
+
const resolvedId = (0, shared_1.syncResolve)(what, importer, stack);
|
|
39
|
+
log("resolve: ✅ '%s'@'%s -> %O\n%s", what, importer, resolved);
|
|
40
|
+
return resolvedId;
|
|
41
|
+
}
|
|
42
|
+
log("resolve: ✅ '%s'@'%s -> %O\n%s", what, importer, resolved);
|
|
43
|
+
// Vite adds param like `?v=667939b3` to cached modules
|
|
44
|
+
const resolvedId = resolved.id.split('?')[0];
|
|
45
|
+
if (resolvedId.startsWith('\0')) {
|
|
46
|
+
// \0 is a special character in Rollup that tells Rollup to not include this in the bundle
|
|
47
|
+
// https://rollupjs.org/guide/en/#outputexports
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
return resolvedId;
|
|
51
|
+
}
|
|
52
|
+
log("resolve: ❌ '%s'@'%s", what, importer);
|
|
53
|
+
throw new Error(`Could not resolve ${what}`);
|
|
54
|
+
};
|
|
55
|
+
const transformServices = {
|
|
56
|
+
options: {
|
|
57
|
+
filename: id,
|
|
58
|
+
root: process.cwd(),
|
|
59
|
+
preprocessor,
|
|
60
|
+
pluginOptions: rest,
|
|
61
|
+
},
|
|
62
|
+
cache,
|
|
63
|
+
};
|
|
64
|
+
const result = await (0, transform_1.transform)(transformServices, code, asyncResolve, emptyConfig);
|
|
65
|
+
if (!result.cssText)
|
|
66
|
+
return;
|
|
67
|
+
let { cssText } = result;
|
|
68
|
+
const slug = (0, shared_1.slugify)(cssText);
|
|
69
|
+
const filename = `${id.replace(/\.[jt]sx?$/, '')}_${slug}.css`;
|
|
70
|
+
if (sourceMap && result.cssSourceMapText) {
|
|
71
|
+
const map = Buffer.from(result.cssSourceMapText).toString('base64');
|
|
72
|
+
cssText += `/*# sourceMappingURL=data:application/json;base64,${map}*/`;
|
|
73
|
+
}
|
|
74
|
+
cssLookup[filename] = cssText;
|
|
75
|
+
result.code += `\nimport ${JSON.stringify(filename)};\n`;
|
|
76
|
+
/* eslint-disable-next-line consistent-return */
|
|
77
|
+
return { code: result.code, map: result.sourceMap };
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
return new Proxy(plugin, {
|
|
81
|
+
get(target, prop) {
|
|
82
|
+
return target[prop];
|
|
83
|
+
},
|
|
84
|
+
getOwnPropertyDescriptor(target, prop) {
|
|
85
|
+
return Object.getOwnPropertyDescriptor(target, prop);
|
|
86
|
+
},
|
|
87
|
+
ownKeys() {
|
|
88
|
+
// Rollup doesn't ask config about its own keys, so it is Vite.
|
|
89
|
+
throw new Error('You are trying to use @wyw-in-js/rollup with Vite. Please use @wyw-in-js/vite instead.');
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
exports.default = wywInJS;
|