@modern-js/plugin-tailwindcss 1.4.1 → 1.5.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.
Files changed (32) hide show
  1. package/CHANGELOG.md +40 -0
  2. package/dist/js/modern/cli.js +126 -0
  3. package/dist/js/modern/design-token/cli/index.js +129 -0
  4. package/dist/js/modern/design-token/index.js +2 -0
  5. package/dist/js/modern/design-token/postcss-plugin/index.js +18 -0
  6. package/dist/js/modern/design-token/runtime/index.js +3 -0
  7. package/dist/js/modern/design-token/runtime/plugin.js +62 -0
  8. package/dist/js/modern/index.js +1 -0
  9. package/dist/js/modern/tailwind.js +43 -0
  10. package/dist/js/modern/utils.js +34 -0
  11. package/dist/js/node/cli.js +9 -3
  12. package/dist/js/node/design-token/cli/index.js +139 -0
  13. package/dist/js/node/design-token/index.js +30 -0
  14. package/dist/js/node/design-token/postcss-plugin/index.js +27 -0
  15. package/dist/js/node/design-token/runtime/index.js +28 -0
  16. package/dist/js/node/design-token/runtime/plugin.js +86 -0
  17. package/dist/js/treeshaking/cli.js +142 -0
  18. package/dist/js/treeshaking/design-token/cli/index.js +107 -0
  19. package/dist/js/treeshaking/design-token/index.js +2 -0
  20. package/dist/js/treeshaking/design-token/postcss-plugin/index.js +30 -0
  21. package/dist/js/treeshaking/design-token/runtime/index.js +3 -0
  22. package/dist/js/treeshaking/design-token/runtime/plugin.js +62 -0
  23. package/dist/js/treeshaking/index.js +1 -0
  24. package/dist/js/treeshaking/tailwind.js +51 -0
  25. package/dist/js/treeshaking/utils.js +56 -0
  26. package/dist/types/cli.d.ts +5 -1
  27. package/dist/types/design-token/cli/index.d.ts +9 -0
  28. package/dist/types/design-token/index.d.ts +2 -0
  29. package/dist/types/design-token/postcss-plugin/index.d.ts +12 -0
  30. package/dist/types/design-token/runtime/index.d.ts +3 -0
  31. package/dist/types/design-token/runtime/plugin.d.ts +12 -0
  32. package/package.json +28 -6
package/CHANGELOG.md CHANGED
@@ -1,5 +1,45 @@
1
1
  # @modern-js/plugin-tailwindcss
2
2
 
3
+ ## 1.5.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 3c874ca16: fix: `@modern-js/plugin-tailwindcss` support `pluginName` params
8
+
9
+ fix: `@modern-js/plugin-tailwindcss` 支持 `pluginName` 参数
10
+
11
+ ## 1.5.0
12
+
13
+ ### Minor Changes
14
+
15
+ - 79e83ef: chore: merge `@modern-js/plugin-design-token` to `@modern-js/plugin-tailwindcss`
16
+
17
+ chore: 合并 `@modern-js/plugin-design-token` 到 `@modern-js/plugin-tailwindcss`
18
+
19
+ - 59c941a: chore(runtime): merge `@modern-js/runtime-core` to `@modern-js/runtime`
20
+
21
+ chore(runtime): 合并 `@modern-js/runtime-core` 到 `@modern-js/runtime`
22
+
23
+ ### Patch Changes
24
+
25
+ - Updated dependencies [79e83ef]
26
+ - Updated dependencies [e0cd14a]
27
+ - Updated dependencies [287ac8b]
28
+ - Updated dependencies [22f4dca]
29
+ - Updated dependencies [59c941a]
30
+ - Updated dependencies [7b9067f]
31
+ - @modern-js/utils@1.9.0
32
+ - @modern-js/runtime@1.5.0
33
+
34
+ ## 1.4.2
35
+
36
+ ### Patch Changes
37
+
38
+ - nothing happen, only bump
39
+ - Updated dependencies
40
+ - @modern-js/utils@1.7.11
41
+ - @modern-js/plugin-design-token@1.0.11
42
+
3
43
  ## 1.4.1
4
44
 
5
45
  ### Patch Changes
@@ -0,0 +1,126 @@
1
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
2
+
3
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
4
+
5
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6
+
7
+ import path from 'path';
8
+ import { PLUGIN_SCHEMAS, fs, CONFIG_CACHE_DIR, globby, nanoid, slash } from '@modern-js/utils';
9
+ import DesignTokenPlugin from "./design-token/cli";
10
+ import { getTailwindConfig } from "./tailwind";
11
+ import { template, checkTwinMacroNotExist } from "./utils";
12
+ const supportCssInJsLibrary = 'styled-components';
13
+ export const getRandomTwConfigFileName = internalDirectory => {
14
+ return slash(path.join(internalDirectory, `tailwind.config.${Date.now()}.${nanoid()}.js`));
15
+ };
16
+ export default (({
17
+ pluginName
18
+ } = {
19
+ pluginName: '@modern-js/plugin-tailwindcss'
20
+ }) => ({
21
+ name: '@modern-js/plugin-tailwindcss',
22
+ // support designSystem.supportStyledComponents
23
+ usePlugins: [DesignTokenPlugin({
24
+ pluginName
25
+ })],
26
+ setup: async api => {
27
+ const {
28
+ appDirectory,
29
+ internalDirectory
30
+ } = api.useAppContext();
31
+ let internalTwConfigPath = ''; // When reinstalling dependencies, most of the time the project will be restarted
32
+
33
+ const notHaveTwinMacro = await checkTwinMacroNotExist(appDirectory);
34
+ return {
35
+ prepare() {
36
+ if (notHaveTwinMacro) {
37
+ return;
38
+ }
39
+
40
+ internalTwConfigPath = getRandomTwConfigFileName(internalDirectory);
41
+ const globPattern = slash(path.join(appDirectory, CONFIG_CACHE_DIR, '*.cjs'));
42
+ const files = globby.sync(globPattern, {
43
+ absolute: true
44
+ });
45
+
46
+ if (files.length > 0) {
47
+ fs.writeFileSync(internalTwConfigPath, template(files[files.length - 1]), 'utf-8');
48
+ }
49
+ },
50
+
51
+ validateSchema() {
52
+ return PLUGIN_SCHEMAS['@modern-js/plugin-tailwindcss'];
53
+ },
54
+
55
+ config() {
56
+ return {
57
+ tools: {
58
+ // TODO: Add interface about postcss config
59
+ // TODO: In module project, also is called, but should not be called.
60
+ postcss: config => {
61
+ const modernConfig = api.useResolvedConfigContext();
62
+ const tailwindConfig = getTailwindConfig(modernConfig, {
63
+ pureConfig: {
64
+ content: ['./config/html/**/*.html', './config/html/**/*.ejs', './config/html/**/*.hbs', './src/**/*.js', './src/**/*.jsx', './src/**/*.ts', './src/**/*.tsx', // about storybook
65
+ './storybook/**/*', './styles/**/*.less', './styles/**/*.css', './styles/**/*.sass', './styles/**/*.scss']
66
+ }
67
+ });
68
+
69
+ if (Array.isArray(config.postcssOptions.plugins)) {
70
+ config.postcssOptions.plugins.push(require('tailwindcss')(tailwindConfig));
71
+ } else {
72
+ config.postcssOptions.plugins = [require('tailwindcss')(tailwindConfig)];
73
+ }
74
+ },
75
+
76
+ babel(config) {
77
+ var _config$plugins;
78
+
79
+ if (notHaveTwinMacro) {
80
+ return;
81
+ }
82
+
83
+ const twinConfig = {
84
+ twin: {
85
+ preset: supportCssInJsLibrary,
86
+ config: internalTwConfigPath
87
+ }
88
+ };
89
+ (_config$plugins = config.plugins) === null || _config$plugins === void 0 ? void 0 : _config$plugins.some(plugin => {
90
+ if (Array.isArray(plugin) && plugin[0]) {
91
+ const pluginTarget = plugin[0];
92
+ let pluginOptions = plugin[1];
93
+
94
+ if (typeof pluginTarget === 'string' && // TODO: use babel chain
95
+ slash(pluginTarget).includes('compiled/babel-plugin-macros')) {
96
+ if (pluginOptions) {
97
+ pluginOptions = _objectSpread(_objectSpread({}, pluginOptions), twinConfig);
98
+ } else {
99
+ plugin.push(twinConfig);
100
+ }
101
+
102
+ return true;
103
+ }
104
+ }
105
+
106
+ return false;
107
+ });
108
+ }
109
+
110
+ }
111
+ };
112
+ },
113
+
114
+ moduleTailwindConfig() {
115
+ const modernConfig = api.useResolvedConfigContext();
116
+ const tailwindConfig = getTailwindConfig(modernConfig, {
117
+ pureConfig: {
118
+ content: ['./src/**/*.js', './src/**/*.jsx', './src/**/*.ts', './src/**/*.tsx', './src/**/*.less', './src/**/*.css', './src/**/*.sass', './src/**/*.scss', './styles/**/*.less', './styles/**/*.css', './styles/**/*.sass', './styles/**/*.scss']
119
+ }
120
+ });
121
+ return require('tailwindcss')(tailwindConfig);
122
+ }
123
+
124
+ };
125
+ }
126
+ }));
@@ -0,0 +1,129 @@
1
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
2
+
3
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
4
+
5
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6
+
7
+ import { lazyImport, PLUGIN_SCHEMAS, createRuntimeExportsUtils } from '@modern-js/utils';
8
+ export default (({
9
+ pluginName
10
+ }) => ({
11
+ name: '@modern-js/plugin-design-token',
12
+
13
+ setup(api) {
14
+ let pluginsExportsUtils;
15
+ const resolveConfig = lazyImport('tailwindcss/resolveConfig', require);
16
+ const PLUGIN_IDENTIFIER = 'designToken';
17
+
18
+ const getDesignTokens = userConfig => {
19
+ const {
20
+ source: {
21
+ designSystem
22
+ }
23
+ } = userConfig; // TODO: Type to be filled
24
+
25
+ const tailwindcssConfig = {};
26
+ tailwindcssConfig.theme = designSystem ? _objectSpread({}, designSystem) : {}; // not use default design token when designToken.defaultTheme is false or theme is false
27
+
28
+ if (!designSystem) {
29
+ tailwindcssConfig.presets = [];
30
+ } // when only designSystem exist, need remove supportStyledComponents
31
+
32
+
33
+ if (designSystem) {
34
+ delete tailwindcssConfig.theme.supportStyledComponents;
35
+ }
36
+
37
+ return resolveConfig(tailwindcssConfig).theme || {};
38
+ };
39
+
40
+ return {
41
+ config() {
42
+ const appContext = api.useAppContext();
43
+ pluginsExportsUtils = createRuntimeExportsUtils(appContext.internalDirectory, 'plugins');
44
+ return {
45
+ source: {
46
+ alias: {
47
+ '@modern-js/runtime/plugins': pluginsExportsUtils.getPath()
48
+ }
49
+ },
50
+ tools: {// TODO: support less、scss、css vars
51
+ // less: https://github.com/modern-js-dev/modern.js/pull/398/files#diff-f77c749e403fbf1fb676d5687bef3d7138a230331c849298d8afaff9b6afbc3dR166
52
+ // sass: https://github.com/modern-js-dev/modern.js/pull/398/files#diff-f77c749e403fbf1fb676d5687bef3d7138a230331c849298d8afaff9b6afbc3dR229
53
+ // postcss: https://github.com/modern-js-dev/modern.js/pull/398/files#diff-f77c749e403fbf1fb676d5687bef3d7138a230331c849298d8afaff9b6afbc3dR281
54
+ }
55
+ };
56
+ },
57
+
58
+ modifyEntryImports({
59
+ entrypoint,
60
+ imports
61
+ }) {
62
+ const userConfig = api.useResolvedConfigContext();
63
+ const {
64
+ source: {
65
+ designSystem
66
+ }
67
+ } = userConfig;
68
+
69
+ if (typeof designSystem === 'object' && designSystem.supportStyledComponents) {
70
+ const designTokens = getDesignTokens(userConfig);
71
+ imports.push({
72
+ value: '@modern-js/runtime/plugins',
73
+ specifiers: [{
74
+ imported: PLUGIN_IDENTIFIER
75
+ }],
76
+ initialize: `
77
+ const designTokens = ${JSON.stringify(designTokens)};
78
+ `
79
+ });
80
+ }
81
+
82
+ return {
83
+ entrypoint,
84
+ imports
85
+ };
86
+ },
87
+
88
+ modifyEntryRuntimePlugins({
89
+ entrypoint,
90
+ plugins
91
+ }) {
92
+ const {
93
+ source: {
94
+ designSystem
95
+ }
96
+ } = api.useResolvedConfigContext();
97
+ let useSCThemeProvider = true;
98
+
99
+ if (designSystem) {
100
+ // when designSystem exist, designToken.styledComponents`s default value is false.
101
+ useSCThemeProvider = (designSystem === null || designSystem === void 0 ? void 0 : designSystem.supportStyledComponents) || false;
102
+ }
103
+
104
+ if (typeof designSystem === 'object' && designSystem.supportStyledComponents) {
105
+ plugins.push({
106
+ name: PLUGIN_IDENTIFIER,
107
+ options: `{token: designTokens, useStyledComponentsThemeProvider: ${useSCThemeProvider ? 'true' : 'false'}, useDesignTokenContext: false}`
108
+ });
109
+ }
110
+
111
+ return {
112
+ entrypoint,
113
+ plugins
114
+ };
115
+ },
116
+
117
+ validateSchema() {
118
+ // add source.designSystem.supportStyledComponents config
119
+ return PLUGIN_SCHEMAS['@modern-js/plugin-design-token'];
120
+ },
121
+
122
+ addRuntimeExports() {
123
+ pluginsExportsUtils.addExport(`export { default as designToken } from '${pluginName}/runtime-design-token'`);
124
+ }
125
+
126
+ };
127
+ }
128
+
129
+ }));
@@ -0,0 +1,2 @@
1
+ export { default } from "./runtime";
2
+ export * from "./runtime";
@@ -0,0 +1,18 @@
1
+ export default (({
2
+ cssVarsHash: _cssVarsHash = {}
3
+ } = {}) => ({
4
+ postcssPlugin: 'postcss-replace-css-vars',
5
+
6
+ Declaration(decl) {
7
+ if (decl.value && typeof decl.value === 'string') {
8
+ decl.value = decl.value.replace(/--\S*/g, match => {
9
+ if (_cssVarsHash[match]) {
10
+ return _cssVarsHash[match];
11
+ }
12
+
13
+ return match;
14
+ });
15
+ }
16
+ }
17
+
18
+ }));
@@ -0,0 +1,3 @@
1
+ import designTokenPlugin from "./plugin";
2
+ export * from "./plugin";
3
+ export default designTokenPlugin;
@@ -0,0 +1,62 @@
1
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
2
+
3
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
4
+
5
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6
+
7
+ import hoistNonReactStatics from 'hoist-non-react-statics';
8
+ import React, { useContext } from 'react';
9
+ import { jsx as _jsx } from "react/jsx-runtime";
10
+ export const DesignTokenContext = /*#__PURE__*/React.createContext({});
11
+ export const useDesignTokens = () => useContext(DesignTokenContext);
12
+ export default ((options = {}) => ({
13
+ name: '@modern-js/plugin-design-token',
14
+ setup: () => ({
15
+ hoc({
16
+ App
17
+ }, next) {
18
+ const DesignTokenAppWrapper = props => {
19
+ const {
20
+ token = {},
21
+ useStyledComponentsThemeProvider = false,
22
+ useDesignTokenContext = false
23
+ } = options;
24
+
25
+ if (useStyledComponentsThemeProvider && useDesignTokenContext) {
26
+ const {
27
+ ThemeProvider
28
+ } = require('@modern-js/runtime/styled');
29
+
30
+ return /*#__PURE__*/_jsx(ThemeProvider, {
31
+ theme: token,
32
+ children: /*#__PURE__*/_jsx(DesignTokenContext.Provider, {
33
+ value: token,
34
+ children: /*#__PURE__*/_jsx(App, _objectSpread({}, props))
35
+ })
36
+ });
37
+ } else if (useStyledComponentsThemeProvider) {
38
+ const {
39
+ ThemeProvider
40
+ } = require('@modern-js/runtime/styled');
41
+
42
+ return /*#__PURE__*/_jsx(ThemeProvider, {
43
+ theme: token,
44
+ children: /*#__PURE__*/_jsx(App, _objectSpread({}, props))
45
+ });
46
+ } else if (useDesignTokenContext) {
47
+ return /*#__PURE__*/_jsx(DesignTokenContext.Provider, {
48
+ value: token,
49
+ children: /*#__PURE__*/_jsx(App, _objectSpread({}, props))
50
+ });
51
+ } else {
52
+ return /*#__PURE__*/_jsx(App, _objectSpread({}, props));
53
+ }
54
+ };
55
+
56
+ return next({
57
+ App: hoistNonReactStatics(DesignTokenAppWrapper, App)
58
+ });
59
+ }
60
+
61
+ })
62
+ }));
@@ -0,0 +1 @@
1
+ export { default } from "./cli";
@@ -0,0 +1,43 @@
1
+ import { applyOptionsChain, logger } from '@modern-js/utils';
2
+ import { merge, cloneDeep } from '@modern-js/utils/lodash';
3
+
4
+ const checkIfExistNotAllowKeys = tailwindConfig => {
5
+ const notAllowExistKeys = ['theme'];
6
+ let notAllowKey = '';
7
+ const ret = Object.keys(tailwindConfig).some(key => notAllowExistKeys.includes(key) && (notAllowKey = key));
8
+ return [ret, notAllowKey];
9
+ };
10
+
11
+ const getPureDesignSystemConfig = designSystemConfig => {
12
+ const pureDesignSystemConfig = cloneDeep(designSystemConfig);
13
+ delete pureDesignSystemConfig.supportStyledComponents;
14
+ return pureDesignSystemConfig;
15
+ };
16
+
17
+ const getTailwindConfig = (config, option = {}) => {
18
+ const purgeConfig = merge({
19
+ // TODO: how the operating environment is determined
20
+ enabled: process.env.NODE_ENV === 'production',
21
+ // TODO: Remove or not
22
+ layers: ['utilities'],
23
+ content: []
24
+ }, option.pureConfig || {});
25
+ const defaultTailwindConfig = {
26
+ purge: purgeConfig
27
+ };
28
+ const tailwindConfig = applyOptionsChain(defaultTailwindConfig, config.tools.tailwindcss || {});
29
+ const designSystem = getPureDesignSystemConfig(config.source.designSystem || {});
30
+ const [exist, key] = checkIfExistNotAllowKeys(tailwindConfig);
31
+
32
+ if (exist) {
33
+ logger.error(`should not exist '${key}' on tools.tailwindcss, please remove it`); // eslint-disable-next-line no-process-exit
34
+
35
+ process.exit(0);
36
+ } // Because there is no default theme configuration
37
+
38
+
39
+ tailwindConfig.theme = designSystem || {};
40
+ return tailwindConfig;
41
+ };
42
+
43
+ export { getTailwindConfig };
@@ -0,0 +1,34 @@
1
+ import path from 'path';
2
+ import { fs } from '@modern-js/utils';
3
+ export const template = configPath => `
4
+ function _interopRequireDefault(obj) {
5
+ return obj && obj.__esModule ? obj : { default: obj };
6
+ }
7
+ const modernConfig = _interopRequireDefault(require('${configPath}')).default;
8
+
9
+ const theme =
10
+ modernConfig && modernConfig.source && modernConfig.source.designSystem
11
+ ? modernConfig.source.designSystem
12
+ : {};
13
+ const tailwindcss =
14
+ modernConfig && modernConfig.tools && modernConfig.tools.tailwindcss
15
+ ? modernConfig.tools.tailwindcss
16
+ : {};
17
+
18
+ module.exports = {
19
+ theme,
20
+ ...tailwindcss,
21
+ };
22
+ `;
23
+ export const checkTwinMacroNotExist = async appDirectory => {
24
+ const depName = 'twin.macro';
25
+ const packageJson = (await fs.readJSON(path.join(appDirectory, 'package.json'), {
26
+ throws: false
27
+ })) || {};
28
+
29
+ if (typeof packageJson.dependencies === 'object' && packageJson.dependencies[depName] || typeof packageJson.devDependencies === 'object' && packageJson.devDependencies[depName]) {
30
+ return false;
31
+ }
32
+
33
+ return true;
34
+ };
@@ -9,7 +9,7 @@ var _path = _interopRequireDefault(require("path"));
9
9
 
10
10
  var _utils = require("@modern-js/utils");
11
11
 
12
- var _cli = _interopRequireDefault(require("@modern-js/plugin-design-token/cli"));
12
+ var _cli = _interopRequireDefault(require("./design-token/cli"));
13
13
 
14
14
  var _tailwind = require("./tailwind");
15
15
 
@@ -31,10 +31,16 @@ const getRandomTwConfigFileName = internalDirectory => {
31
31
 
32
32
  exports.getRandomTwConfigFileName = getRandomTwConfigFileName;
33
33
 
34
- var _default = () => ({
34
+ var _default = ({
35
+ pluginName
36
+ } = {
37
+ pluginName: '@modern-js/plugin-tailwindcss'
38
+ }) => ({
35
39
  name: '@modern-js/plugin-tailwindcss',
36
40
  // support designSystem.supportStyledComponents
37
- usePlugins: [(0, _cli.default)()],
41
+ usePlugins: [(0, _cli.default)({
42
+ pluginName
43
+ })],
38
44
  setup: async api => {
39
45
  const {
40
46
  appDirectory,
@@ -0,0 +1,139 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _utils = require("@modern-js/utils");
9
+
10
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
11
+
12
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
13
+
14
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
15
+
16
+ var _default = ({
17
+ pluginName
18
+ }) => ({
19
+ name: '@modern-js/plugin-design-token',
20
+
21
+ setup(api) {
22
+ let pluginsExportsUtils;
23
+ const resolveConfig = (0, _utils.lazyImport)('tailwindcss/resolveConfig', require);
24
+ const PLUGIN_IDENTIFIER = 'designToken';
25
+
26
+ const getDesignTokens = userConfig => {
27
+ const {
28
+ source: {
29
+ designSystem
30
+ }
31
+ } = userConfig; // TODO: Type to be filled
32
+
33
+ const tailwindcssConfig = {};
34
+ tailwindcssConfig.theme = designSystem ? _objectSpread({}, designSystem) : {}; // not use default design token when designToken.defaultTheme is false or theme is false
35
+
36
+ if (!designSystem) {
37
+ tailwindcssConfig.presets = [];
38
+ } // when only designSystem exist, need remove supportStyledComponents
39
+
40
+
41
+ if (designSystem) {
42
+ delete tailwindcssConfig.theme.supportStyledComponents;
43
+ }
44
+
45
+ return resolveConfig(tailwindcssConfig).theme || {};
46
+ };
47
+
48
+ return {
49
+ config() {
50
+ const appContext = api.useAppContext();
51
+ pluginsExportsUtils = (0, _utils.createRuntimeExportsUtils)(appContext.internalDirectory, 'plugins');
52
+ return {
53
+ source: {
54
+ alias: {
55
+ '@modern-js/runtime/plugins': pluginsExportsUtils.getPath()
56
+ }
57
+ },
58
+ tools: {// TODO: support less、scss、css vars
59
+ // less: https://github.com/modern-js-dev/modern.js/pull/398/files#diff-f77c749e403fbf1fb676d5687bef3d7138a230331c849298d8afaff9b6afbc3dR166
60
+ // sass: https://github.com/modern-js-dev/modern.js/pull/398/files#diff-f77c749e403fbf1fb676d5687bef3d7138a230331c849298d8afaff9b6afbc3dR229
61
+ // postcss: https://github.com/modern-js-dev/modern.js/pull/398/files#diff-f77c749e403fbf1fb676d5687bef3d7138a230331c849298d8afaff9b6afbc3dR281
62
+ }
63
+ };
64
+ },
65
+
66
+ modifyEntryImports({
67
+ entrypoint,
68
+ imports
69
+ }) {
70
+ const userConfig = api.useResolvedConfigContext();
71
+ const {
72
+ source: {
73
+ designSystem
74
+ }
75
+ } = userConfig;
76
+
77
+ if (typeof designSystem === 'object' && designSystem.supportStyledComponents) {
78
+ const designTokens = getDesignTokens(userConfig);
79
+ imports.push({
80
+ value: '@modern-js/runtime/plugins',
81
+ specifiers: [{
82
+ imported: PLUGIN_IDENTIFIER
83
+ }],
84
+ initialize: `
85
+ const designTokens = ${JSON.stringify(designTokens)};
86
+ `
87
+ });
88
+ }
89
+
90
+ return {
91
+ entrypoint,
92
+ imports
93
+ };
94
+ },
95
+
96
+ modifyEntryRuntimePlugins({
97
+ entrypoint,
98
+ plugins
99
+ }) {
100
+ const {
101
+ source: {
102
+ designSystem
103
+ }
104
+ } = api.useResolvedConfigContext();
105
+ let useSCThemeProvider = true;
106
+
107
+ if (designSystem) {
108
+ // when designSystem exist, designToken.styledComponents`s default value is false.
109
+ useSCThemeProvider = (designSystem === null || designSystem === void 0 ? void 0 : designSystem.supportStyledComponents) || false;
110
+ }
111
+
112
+ if (typeof designSystem === 'object' && designSystem.supportStyledComponents) {
113
+ plugins.push({
114
+ name: PLUGIN_IDENTIFIER,
115
+ options: `{token: designTokens, useStyledComponentsThemeProvider: ${useSCThemeProvider ? 'true' : 'false'}, useDesignTokenContext: false}`
116
+ });
117
+ }
118
+
119
+ return {
120
+ entrypoint,
121
+ plugins
122
+ };
123
+ },
124
+
125
+ validateSchema() {
126
+ // add source.designSystem.supportStyledComponents config
127
+ return _utils.PLUGIN_SCHEMAS['@modern-js/plugin-design-token'];
128
+ },
129
+
130
+ addRuntimeExports() {
131
+ pluginsExportsUtils.addExport(`export { default as designToken } from '${pluginName}/runtime-design-token'`);
132
+ }
133
+
134
+ };
135
+ }
136
+
137
+ });
138
+
139
+ exports.default = _default;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ var _exportNames = {};
7
+ Object.defineProperty(exports, "default", {
8
+ enumerable: true,
9
+ get: function () {
10
+ return _runtime.default;
11
+ }
12
+ });
13
+
14
+ var _runtime = _interopRequireWildcard(require("./runtime"));
15
+
16
+ Object.keys(_runtime).forEach(function (key) {
17
+ if (key === "default" || key === "__esModule") return;
18
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
19
+ if (key in exports && exports[key] === _runtime[key]) return;
20
+ Object.defineProperty(exports, key, {
21
+ enumerable: true,
22
+ get: function () {
23
+ return _runtime[key];
24
+ }
25
+ });
26
+ });
27
+
28
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
29
+
30
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }