@modern-js/module-tools 1.5.8 → 1.6.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 (116) hide show
  1. package/CHANGELOG.md +41 -0
  2. package/dist/js/modern/cli/build.js +5 -4
  3. package/dist/js/modern/commands/build.js +55 -28
  4. package/dist/js/modern/features/build/bundle/index.js +9 -0
  5. package/dist/js/modern/features/build/bundle/runRollup.js +149 -0
  6. package/dist/js/modern/features/build/bundle/runSpeedy.js +185 -0
  7. package/dist/js/modern/{tasks → features/build/bundleless}/copy-assets.js +31 -56
  8. package/dist/js/modern/{tasks → features/build/bundleless}/generator-dts/index.js +46 -71
  9. package/dist/js/modern/{tasks → features/build/bundleless}/generator-dts/utils.js +10 -10
  10. package/dist/js/modern/features/build/bundleless/index.js +23 -0
  11. package/dist/js/modern/features/build/bundleless/runBabel.js +269 -0
  12. package/dist/js/modern/features/build/bundleless/style.js +266 -0
  13. package/dist/js/modern/features/build/constants.js +94 -59
  14. package/dist/js/modern/features/build/error.js +150 -0
  15. package/dist/js/modern/features/build/index.js +87 -18
  16. package/dist/js/modern/features/build/legacy-constants.js +99 -0
  17. package/dist/js/modern/features/build/normalize.js +301 -0
  18. package/dist/js/modern/features/build/types.js +0 -0
  19. package/dist/js/modern/features/build/utils.js +39 -136
  20. package/dist/js/modern/index.js +3 -1
  21. package/dist/js/modern/locale/en.js +5 -3
  22. package/dist/js/modern/locale/zh.js +5 -3
  23. package/dist/js/modern/schema/build-config.js +111 -0
  24. package/dist/js/modern/schema/index.js +2 -1
  25. package/dist/js/modern/schema/output.js +18 -9
  26. package/dist/js/modern/schema/source.js +2 -10
  27. package/dist/js/modern/schema/tools.js +6 -0
  28. package/dist/js/modern/schema/types.js +0 -0
  29. package/dist/js/modern/utils/babel.js +4 -1
  30. package/dist/js/modern/utils/constants.js +3 -0
  31. package/dist/js/modern/utils/copy.js +2 -2
  32. package/dist/js/node/cli/build.js +5 -4
  33. package/dist/js/node/commands/build.js +62 -29
  34. package/dist/js/node/features/build/bundle/index.js +23 -0
  35. package/dist/js/node/features/build/bundle/runRollup.js +174 -0
  36. package/dist/js/node/features/build/bundle/runSpeedy.js +209 -0
  37. package/dist/js/node/{tasks → features/build/bundleless}/copy-assets.js +37 -56
  38. package/dist/js/node/{tasks → features/build/bundleless}/generator-dts/index.js +58 -73
  39. package/dist/js/node/{tasks → features/build/bundleless}/generator-dts/utils.js +9 -10
  40. package/dist/js/node/features/build/bundleless/index.js +40 -0
  41. package/dist/js/node/features/build/bundleless/runBabel.js +309 -0
  42. package/dist/js/node/features/build/bundleless/style.js +299 -0
  43. package/dist/js/node/features/build/constants.js +115 -67
  44. package/dist/js/node/features/build/error.js +175 -0
  45. package/dist/js/node/features/build/index.js +104 -18
  46. package/dist/js/node/features/build/legacy-constants.js +107 -0
  47. package/dist/js/node/features/build/normalize.js +330 -0
  48. package/dist/js/node/features/build/types.js +0 -0
  49. package/dist/js/node/features/build/utils.js +48 -145
  50. package/dist/js/node/index.js +20 -1
  51. package/dist/js/node/locale/en.js +5 -3
  52. package/dist/js/node/locale/zh.js +5 -3
  53. package/dist/js/node/schema/build-config.js +120 -0
  54. package/dist/js/node/schema/index.js +3 -1
  55. package/dist/js/node/schema/output.js +20 -9
  56. package/dist/js/node/schema/source.js +2 -10
  57. package/dist/js/node/schema/tools.js +13 -0
  58. package/dist/js/node/schema/types.js +0 -0
  59. package/dist/js/node/utils/babel.js +4 -1
  60. package/dist/js/node/utils/constants.js +11 -0
  61. package/dist/js/node/utils/copy.js +1 -1
  62. package/dist/types/cli/build.d.ts +1 -1
  63. package/dist/types/commands/build.d.ts +12 -12
  64. package/dist/types/features/build/bundle/index.d.ts +3 -0
  65. package/dist/types/features/build/bundle/runRollup.d.ts +3 -0
  66. package/dist/types/features/build/bundle/runSpeedy.d.ts +10 -0
  67. package/dist/types/features/build/bundleless/copy-assets.d.ts +3 -0
  68. package/dist/types/features/build/bundleless/generator-dts/index.d.ts +3 -0
  69. package/dist/types/{tasks → features/build/bundleless}/generator-dts/utils.d.ts +3 -6
  70. package/dist/types/features/build/bundleless/index.d.ts +4 -0
  71. package/dist/types/features/build/bundleless/runBabel.d.ts +32 -0
  72. package/dist/types/features/build/bundleless/style.d.ts +25 -0
  73. package/dist/types/features/build/constants.d.ts +17 -13
  74. package/dist/types/features/build/error.d.ts +33 -0
  75. package/dist/types/features/build/index.d.ts +12 -3
  76. package/dist/types/features/build/legacy-constants.d.ts +8 -0
  77. package/dist/types/features/build/normalize.d.ts +19 -0
  78. package/dist/types/features/build/types.d.ts +15 -0
  79. package/dist/types/features/build/utils.d.ts +10 -31
  80. package/dist/types/index.d.ts +1 -0
  81. package/dist/types/locale/en.d.ts +2 -0
  82. package/dist/types/locale/index.d.ts +4 -0
  83. package/dist/types/locale/zh.d.ts +2 -0
  84. package/dist/types/schema/build-config.d.ts +197 -0
  85. package/dist/types/schema/index.d.ts +184 -18
  86. package/dist/types/schema/output.d.ts +188 -10
  87. package/dist/types/schema/source.d.ts +2 -11
  88. package/dist/types/schema/tools.d.ts +6 -0
  89. package/dist/types/schema/types.d.ts +46 -0
  90. package/dist/types/types.d.ts +29 -28
  91. package/dist/types/utils/babel.d.ts +3 -2
  92. package/dist/types/utils/constants.d.ts +2 -0
  93. package/package.json +23 -9
  94. package/dist/js/modern/features/build/build-watch.js +0 -101
  95. package/dist/js/modern/features/build/build.js +0 -123
  96. package/dist/js/modern/tasks/build-source-code.js +0 -208
  97. package/dist/js/modern/tasks/build-style.js +0 -169
  98. package/dist/js/modern/tasks/build-watch-source-code.js +0 -162
  99. package/dist/js/modern/tasks/build-watch-style.js +0 -216
  100. package/dist/js/modern/tasks/constants.js +0 -1
  101. package/dist/js/node/features/build/build-watch.js +0 -121
  102. package/dist/js/node/features/build/build.js +0 -143
  103. package/dist/js/node/tasks/build-source-code.js +0 -234
  104. package/dist/js/node/tasks/build-style.js +0 -184
  105. package/dist/js/node/tasks/build-watch-source-code.js +0 -174
  106. package/dist/js/node/tasks/build-watch-style.js +0 -233
  107. package/dist/js/node/tasks/constants.js +0 -8
  108. package/dist/types/features/build/build-watch.d.ts +0 -3
  109. package/dist/types/features/build/build.d.ts +0 -3
  110. package/dist/types/tasks/build-source-code.d.ts +0 -25
  111. package/dist/types/tasks/build-style.d.ts +0 -1
  112. package/dist/types/tasks/build-watch-source-code.d.ts +0 -1
  113. package/dist/types/tasks/build-watch-style.d.ts +0 -1
  114. package/dist/types/tasks/constants.d.ts +0 -1
  115. package/dist/types/tasks/copy-assets.d.ts +0 -1
  116. package/dist/types/tasks/generator-dts/index.d.ts +0 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,46 @@
1
1
  # @modern-js/module-tools
2
2
 
3
+ ## 1.6.1
4
+
5
+ ### Patch Changes
6
+
7
+ - nothing happen, only bump
8
+ - Updated dependencies
9
+ - @modern-js/babel-preset-module@1.3.10
10
+ - @modern-js/core@1.12.4
11
+ - @modern-js/style-compiler@1.2.12
12
+ - @modern-js/utils@1.7.11
13
+ - @modern-js/css-config@1.2.7
14
+ - @modern-js/plugin-analyze@1.4.7
15
+ - @modern-js/plugin-changeset@1.3.1
16
+ - @modern-js/plugin-jarvis@1.2.14
17
+
18
+ ## 1.6.0
19
+
20
+ ### Minor Changes
21
+
22
+ - b82869d: - use speedy as a bundler to support bundle, and support dts and css bundle.
23
+ - design `buildConfig` to support build for different scene, and design `buildPreset` to cover most of scenes.
24
+ - add `tools.speedy` to receive custom speedy config.
25
+ - remove child process to solving problems with serialisation parameters.
26
+ - add more test cases, improve quality construction.
27
+ - design a new log about build process
28
+
29
+ ### Patch Changes
30
+
31
+ - Updated dependencies [b82869d]
32
+ - Updated dependencies [b82869d]
33
+ - Updated dependencies [b82869d]
34
+ - Updated dependencies [b82869d]
35
+ - @modern-js/utils@1.7.10
36
+ - @modern-js/babel-preset-module@1.3.9
37
+ - @modern-js/core@1.12.3
38
+ - @modern-js/style-compiler@1.2.11
39
+ - @modern-js/css-config@1.2.7
40
+ - @modern-js/plugin-analyze@1.4.7
41
+ - @modern-js/plugin-changeset@1.3.1
42
+ - @modern-js/plugin-jarvis@1.2.14
43
+
3
44
  ## 1.5.8
4
45
 
5
46
  ### Patch Changes
@@ -1,9 +1,10 @@
1
1
  import { Import } from '@modern-js/utils';
2
2
  const local = Import.lazy('../locale/index', require);
3
- const commands = Import.lazy('../commands', require);
3
+ const command = Import.lazy('../commands/build', require);
4
4
  export const buildCli = (program, api) => {
5
- // TODO: 初始化环境变量
6
- program.command('build').usage('[options]').description(local.i18n.t(local.localeKeys.command.build.describe)).option('-w, --watch', local.i18n.t(local.localeKeys.command.build.watch)).option('--tsconfig [tsconfig]', local.i18n.t(local.localeKeys.command.build.tsconfig), './tsconfig.json').option('--style-only', local.i18n.t(local.localeKeys.command.build.style_only)).option('-p, --platform [platform]', local.i18n.t(local.localeKeys.command.build.platform)).option('--no-tsc', local.i18n.t(local.localeKeys.command.build.no_tsc)).option('--no-clear', local.i18n.t(local.localeKeys.command.build.no_clear)).action(async subCommand => {
7
- await commands.build(api, subCommand);
5
+ return program.command('build').usage('[options]').description(local.i18n.t(local.localeKeys.command.build.describe)).option('-w, --watch', local.i18n.t(local.localeKeys.command.build.watch)).option('--tsconfig [tsconfig]', local.i18n.t(local.localeKeys.command.build.tsconfig), './tsconfig.json').option('--style-only', local.i18n.t(local.localeKeys.command.build.style_only)).option('-p, --platform [platform]', local.i18n.t(local.localeKeys.command.build.platform)) // @deprecated
6
+ // The `--no-tsc` option has been superceded by the `--no-dts` option.
7
+ .option('--no-tsc', local.i18n.t(local.localeKeys.command.build.no_tsc)).option('--dts', local.i18n.t(local.localeKeys.command.build.dts)).option('--no-clear', local.i18n.t(local.localeKeys.command.build.no_clear)).option('-c --config <config>', local.i18n.t(local.localeKeys.command.build.config)).action(async subCommand => {
8
+ await command.build(api, subCommand);
8
9
  });
9
10
  };
@@ -1,46 +1,73 @@
1
1
  import * as path from 'path';
2
- import { fs, Import, dotenv } from '@modern-js/utils';
2
+ import { fs, Import, dotenv, globby, slash } from '@modern-js/utils';
3
+ import onExit from 'signal-exit';
3
4
  const tsConfigutils = Import.lazy('../utils/tsconfig', require);
4
5
  const valid = Import.lazy('../utils/valide', require);
5
6
  const buildFeature = Import.lazy('../features/build', require);
6
- export const build = async (api, {
7
- watch: _watch = false,
8
- tsconfig: tsconfigName,
9
- tsc,
10
- clear: _clear = true,
11
- platform,
12
- styleOnly
13
- }) => {
7
+ /**
8
+ * init work before build task.
9
+ * @param api
10
+ */
11
+
12
+ export const init = api => {
13
+ const {
14
+ appDirectory
15
+ } = api.useAppContext();
16
+ dotenv.config();
17
+ onExit(async code => {
18
+ if (code === 0) {
19
+ return;
20
+ }
21
+
22
+ const tempTsconfigPathPattern = path.join(appDirectory, './node_modules', `./tsconfig.**.**.json`);
23
+ const files = globby(slash(tempTsconfigPathPattern), {
24
+ stats: true,
25
+ absolute: true
26
+ });
27
+ const currentTime = Date.now();
28
+
29
+ for (const file of files) {
30
+ // over 30s, will delete it
31
+ if (currentTime - file.stats.birthtimeMs >= 30 * 1000) {
32
+ fs.unlinkSync(file.path);
33
+ }
34
+ }
35
+ });
36
+ };
37
+ export const build = async (api, buildCommandOption) => {
38
+ var _modernConfig$output$;
39
+
40
+ const {
41
+ watch = false,
42
+ tsconfig: tsconfigName,
43
+ tsc,
44
+ dts = false,
45
+ clear,
46
+ platform = false,
47
+ styleOnly = false
48
+ } = buildCommandOption;
49
+ init(api);
14
50
  const {
15
51
  appDirectory
16
52
  } = api.useAppContext();
17
53
  const modernConfig = api.useResolvedConfigContext();
18
54
  const tsconfigPath = path.join(appDirectory, tsconfigName);
19
- dotenv.config();
55
+ const outputPath = (_modernConfig$output$ = modernConfig.output.path) !== null && _modernConfig$output$ !== void 0 ? _modernConfig$output$ : 'dist';
20
56
  const isTsProject = tsConfigutils.existTsConfigFile(tsconfigPath);
21
- const enableTscCompiler = isTsProject && tsc && !modernConfig.output.disableTsChecker;
57
+ const enableDtsGen = isTsProject && dts;
22
58
  valid.valideBeforeTask({
23
59
  modernConfig,
24
60
  tsconfigPath
25
- }); // TODO: 一些配置只需要从modernConfig中获取
26
-
61
+ });
27
62
  await buildFeature.build(api, {
28
- appDirectory,
29
- enableWatchMode: _watch,
63
+ enableWatchMode: watch,
64
+ enableDtsGen,
30
65
  isTsProject,
31
- platform,
32
- sourceDir: 'src',
33
66
  tsconfigName,
34
- enableTscCompiler,
35
- clear: _clear,
36
- styleOnly
37
- }, modernConfig);
38
- process.on('SIGBREAK', () => {
39
- console.info('exit');
40
- const tempTsconfigFilePath = path.join(appDirectory, './tsconfig.temp.json');
41
-
42
- if (fs.existsSync(tempTsconfigFilePath)) {
43
- fs.removeSync(tempTsconfigFilePath);
44
- }
67
+ outputPath,
68
+ styleOnly,
69
+ platform,
70
+ clear,
71
+ legacyTsc: tsc
45
72
  });
46
73
  };
@@ -0,0 +1,9 @@
1
+ import pMap from 'p-map';
2
+ import { runSpeedy } from "./runSpeedy";
3
+ import { startRollup } from "./runRollup";
4
+ export const build = async (api, config) => {
5
+ const tasks = config.dtsOnly ? [startRollup] : [runSpeedy, startRollup];
6
+ await pMap(tasks, async task => {
7
+ await task(api, config);
8
+ });
9
+ };
@@ -0,0 +1,149 @@
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 ts from 'typescript';
9
+ import hashbangPlugin from 'rollup-plugin-hashbang';
10
+ import jsonPlugin from '@rollup/plugin-json';
11
+ import { InternalDTSError } from "../error";
12
+ import { SectionTitleStatus, watchSectionTitle } from "../utils"; // Copied from https://github.com/egoist/tsup/blob/dev/src/rollup.ts
13
+
14
+ const loadCompilerOptions = tsconfig => {
15
+ if (!tsconfig) {
16
+ return {};
17
+ }
18
+
19
+ const configFile = ts.readConfigFile(tsconfig, ts.sys.readFile);
20
+ const {
21
+ options
22
+ } = ts.parseJsonConfigFileContent(configFile.config, ts.sys, './');
23
+ return options;
24
+ };
25
+
26
+ const dtsPlugin = require('rollup-plugin-dts');
27
+
28
+ const getRollupConfig = async (api, options) => {
29
+ const {
30
+ appDirectory
31
+ } = api.useAppContext();
32
+ const {
33
+ output: {
34
+ path: distPath = 'dist'
35
+ }
36
+ } = api.useResolvedConfigContext();
37
+ const {
38
+ outputPath,
39
+ bundleOptions,
40
+ tsconfig
41
+ } = options;
42
+ const distDir = path.join(appDirectory, distPath, outputPath);
43
+ const compilerOptions = loadCompilerOptions(tsconfig);
44
+ const dtsOptions = {
45
+ entry: bundleOptions.entry
46
+ };
47
+ const ignoreFiles = {
48
+ name: 'ignore-files',
49
+
50
+ load(id) {
51
+ if (!/\.(js|cjs|mjs|jsx|ts|tsx|mts|json)$/.test(id)) {
52
+ return '';
53
+ }
54
+
55
+ return null;
56
+ }
57
+
58
+ };
59
+ return {
60
+ inputConfig: {
61
+ input: dtsOptions.entry,
62
+ external: bundleOptions.externals,
63
+
64
+ onwarn(warning, handler) {
65
+ if (warning.code === 'UNRESOLVED_IMPORT' || warning.code === 'CIRCULAR_DEPENDENCY' || warning.code === 'EMPTY_BUNDLE') {
66
+ return;
67
+ }
68
+
69
+ handler(warning);
70
+ },
71
+
72
+ plugins: [hashbangPlugin(), jsonPlugin(), ignoreFiles, dtsPlugin.default({
73
+ // use external to prevent them which come from node_modules from be bundled.
74
+ respectExternal: true,
75
+ compilerOptions: _objectSpread(_objectSpread({}, compilerOptions), {}, {
76
+ baseUrl: path.resolve(compilerOptions.baseUrl || '.'),
77
+ // Ensure ".d.ts" modules are generated
78
+ declaration: true,
79
+ // Skip ".js" generation
80
+ noEmit: false,
81
+ emitDeclarationOnly: true,
82
+ // Skip code generation when error occurs
83
+ noEmitOnError: true,
84
+ // Avoid extra work
85
+ checkJs: false,
86
+ declarationMap: false,
87
+ skipLibCheck: true,
88
+ preserveSymlinks: false,
89
+ // Ensure we can parse the latest code
90
+ target: ts.ScriptTarget.ESNext
91
+ })
92
+ })].filter(Boolean)
93
+ },
94
+ outputConfig: {
95
+ dir: distDir,
96
+ format: 'esm',
97
+ exports: 'named'
98
+ }
99
+ };
100
+ };
101
+
102
+ async function runRollup(options, context) {
103
+ try {
104
+ const {
105
+ rollup
106
+ } = await import('rollup');
107
+ const bundle = await rollup(options.inputConfig);
108
+ await bundle.write(options.outputConfig);
109
+ } catch (e) {
110
+ if (e instanceof Error) {
111
+ throw new InternalDTSError(e, _objectSpread({
112
+ buildType: 'bundle'
113
+ }, context));
114
+ }
115
+ }
116
+ }
117
+
118
+ async function watchRollup(options) {
119
+ const {
120
+ watch
121
+ } = await import('rollup');
122
+ const titleText = '[Bundle:DTS]';
123
+ watch(_objectSpread(_objectSpread({}, options.inputConfig), {}, {
124
+ plugins: options.inputConfig.plugins,
125
+ output: options.outputConfig
126
+ })).on('event', event => {
127
+ if (event.code === 'START') {
128
+ console.info(watchSectionTitle(titleText, SectionTitleStatus.Log));
129
+ } else if (event.code === 'BUNDLE_END') {
130
+ console.info(watchSectionTitle(titleText, SectionTitleStatus.Success));
131
+ } else if (event.code === 'ERROR') {// this is dts rollup plugin bug, error not complete message
132
+ }
133
+ });
134
+ }
135
+
136
+ export const startRollup = async (api, options) => {
137
+ if (options.enableDts) {
138
+ const config = await getRollupConfig(api, options);
139
+
140
+ if (options.watch) {
141
+ watchRollup(config);
142
+ } else {
143
+ await runRollup(config, {
144
+ target: options.target,
145
+ format: options.format
146
+ });
147
+ }
148
+ }
149
+ };
@@ -0,0 +1,185 @@
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 { SpeedyBundler } from '@speedy-js/speedy-core';
9
+ import { es5InputPlugin } from '@speedy-js/speedy-plugin-es5';
10
+ import { applyOptionsChain, ensureAbsolutePath } from '@modern-js/utils';
11
+ import { InternalBuildError } from "../error";
12
+ import { getPostcssOption, SectionTitleStatus, watchSectionTitle } from "../utils";
13
+ export const getAlias = api => {
14
+ const {
15
+ appDirectory,
16
+ srcDirectory
17
+ } = api.useAppContext();
18
+ const {
19
+ source
20
+ } = api.useResolvedConfigContext(); // TODO: maybe check tsconfig `paths`
21
+
22
+ const defaultAlias = {
23
+ '@': srcDirectory
24
+ };
25
+ const alias = applyOptionsChain(defaultAlias, source === null || source === void 0 ? void 0 : source.alias);
26
+ return Object.keys(alias).reduce((o, name) => {
27
+ return _objectSpread(_objectSpread({}, o), {}, {
28
+ [name]: ensureAbsolutePath(appDirectory, alias[name])
29
+ });
30
+ }, {});
31
+ };
32
+ export const getDefine = api => {
33
+ const {
34
+ source: {
35
+ envVars,
36
+ globalVars
37
+ }
38
+ } = api.useResolvedConfigContext();
39
+ const envVarsDefine = [...(envVars || [])].reduce((memo, name) => {
40
+ memo[`process.env.${name}`] = JSON.stringify(process.env[name]);
41
+ return memo;
42
+ }, {});
43
+ const globalVarsDefine = Object.keys(globalVars || {}).reduce((memo, name) => {
44
+ memo[name] = globalVars ? JSON.stringify(globalVars[name]) : '';
45
+ return memo;
46
+ }, {});
47
+ return _objectSpread(_objectSpread({}, envVarsDefine), globalVarsDefine);
48
+ };
49
+
50
+ const getStyleOptionFromModern = async api => {
51
+ const runner = api.useHookRunners();
52
+ const {
53
+ appDirectory
54
+ } = api.useAppContext();
55
+ const modernConfig = api.useResolvedConfigContext();
56
+ const lessOption = await runner.moduleLessConfig({
57
+ modernConfig
58
+ }, {
59
+ onLast: async _ => undefined
60
+ });
61
+ const sassOption = await runner.moduleSassConfig({
62
+ modernConfig
63
+ }, {
64
+ onLast: async _ => undefined
65
+ });
66
+ const tailwindPlugin = await runner.moduleTailwindConfig({
67
+ modernConfig
68
+ }, {
69
+ onLast: async _ => undefined
70
+ });
71
+ const postcssOption = getPostcssOption(appDirectory, modernConfig);
72
+
73
+ if (tailwindPlugin) {
74
+ var _postcssOption$plugin;
75
+
76
+ (_postcssOption$plugin = postcssOption.plugins) === null || _postcssOption$plugin === void 0 ? void 0 : _postcssOption$plugin.push(tailwindPlugin);
77
+ }
78
+
79
+ return {
80
+ less: lessOption === null || lessOption === void 0 ? void 0 : lessOption.lessOption,
81
+ sass: sassOption,
82
+ postcss: _objectSpread(_objectSpread({}, postcssOption.options), {}, {
83
+ plugins: postcssOption.plugins
84
+ })
85
+ };
86
+ };
87
+
88
+ export const runSpeedy = async (api, config) => {
89
+ const {
90
+ appDirectory
91
+ } = api.useAppContext();
92
+ const {
93
+ output: {
94
+ path: distPath = 'dist'
95
+ },
96
+ tools: {
97
+ speedy: userSpeedyConfig
98
+ }
99
+ } = api.useResolvedConfigContext();
100
+ const {
101
+ target,
102
+ watch,
103
+ bundleOptions,
104
+ outputPath,
105
+ format,
106
+ sourceMap
107
+ } = config;
108
+ const {
109
+ entry,
110
+ platform,
111
+ splitting,
112
+ minify,
113
+ externals
114
+ } = bundleOptions;
115
+ const distDir = path.join(appDirectory, distPath, outputPath);
116
+ const titleText = `[Bundle:Speedy:${format}_${target}]`;
117
+ const style = await getStyleOptionFromModern(api);
118
+ const alias = getAlias(api);
119
+ const define = getDefine(api);
120
+
121
+ const watchPlugin = () => {
122
+ return {
123
+ name: 'watch-plugin',
124
+
125
+ apply(compiler) {
126
+ compiler.hooks.watchChange.tap('watch-plugin', async () => {
127
+ console.info(watchSectionTitle(titleText, SectionTitleStatus.Log));
128
+ });
129
+ }
130
+
131
+ };
132
+ };
133
+
134
+ const plugins = target === 'es5' ? [es5InputPlugin()] : [];
135
+ plugins.push(watchPlugin());
136
+ const internalSpeedyConfig = {
137
+ command: 'build',
138
+ mode: 'production',
139
+ html: false,
140
+ preset: 'webapp',
141
+ // support css and json
142
+ platform,
143
+ watch,
144
+ input: entry,
145
+ target,
146
+ clearScreen: false,
147
+ output: {
148
+ path: distDir,
149
+ format,
150
+ splitting,
151
+ filename: '[name]'
152
+ },
153
+ style,
154
+ resolve: {
155
+ alias
156
+ },
157
+ define,
158
+ sourceMap,
159
+ minify,
160
+ external: externals,
161
+ plugins
162
+ };
163
+ const speedyConfig = applyOptionsChain(internalSpeedyConfig, userSpeedyConfig);
164
+ const compiler = await SpeedyBundler.create(speedyConfig);
165
+
166
+ try {
167
+ if (watch) {
168
+ console.info(watchSectionTitle(titleText, SectionTitleStatus.Log));
169
+ }
170
+
171
+ await compiler.build();
172
+ } catch (e) {
173
+ /**
174
+ * in watch mode
175
+ * use speedy watch log
176
+ */
177
+ if (e instanceof Error) {
178
+ throw new InternalBuildError(e, {
179
+ target,
180
+ format,
181
+ buildType: 'bundle'
182
+ });
183
+ }
184
+ }
185
+ };
@@ -1,9 +1,6 @@
1
1
  import * as path from 'path';
2
2
  import { fs, watch, glob, WatchChangeType, Import } from '@modern-js/utils';
3
- const argv = Import.lazy('process.argv', require);
4
- const core = Import.lazy('@modern-js/core', require);
5
- const copyUtils = Import.lazy('../utils/copy', require);
6
- const STYLE_DIRS = 'styles';
3
+ const copyUtils = Import.lazy('../../../utils/copy', require);
7
4
  const SRC_DIRS = 'src';
8
5
 
9
6
  const copyAssets = ({
@@ -25,11 +22,13 @@ const copyAssets = ({
25
22
  }
26
23
  };
27
24
 
28
- const watchAssets = ({
25
+ const watchAssets = (api, {
29
26
  targetDir,
30
27
  outputDir
31
28
  }) => {
32
- watch(`${targetDir}/**/*.*`, ({
29
+ const appContext = api.useAppContext();
30
+ const modernConfig = api.useResolvedConfigContext();
31
+ watch(`${targetDir}/**/*.*`, async ({
33
32
  changeType,
34
33
  changedFilePath
35
34
  }) => {
@@ -41,74 +40,50 @@ const watchAssets = ({
41
40
 
42
41
  const file = path.relative(targetDir, changedFilePath);
43
42
  fs.copyFileSync(changedFilePath, path.resolve(outputDir, file));
43
+ await copyUtils.copyTask({
44
+ modernConfig,
45
+ appContext
46
+ });
44
47
  }, ['**/*.{js,jsx,ts,tsx,d.ts,scss,less,css,sass}']);
45
48
  };
46
49
 
47
- const taskMain = ({
48
- modernConfig,
49
- appContext
50
- }) => {
51
- const processArgv = argv(process.argv.slice(2));
52
- const config = processArgv({});
50
+ export const copyStaticAssets = async (api, config) => {
51
+ var _bundlelessOptions$so;
52
+
53
+ const appContext = api.useAppContext();
54
+ const modernConfig = api.useResolvedConfigContext();
53
55
  const {
54
56
  appDirectory
55
57
  } = appContext;
56
58
  const {
57
- jsPath = 'js',
58
- assetsPath = 'styles',
59
- path: outputPath = 'dist'
59
+ path: distPath = 'dist'
60
60
  } = modernConfig.output;
61
- const srcDir = path.join(appDirectory, SRC_DIRS);
62
- const outputDirToSrc = path.join(appDirectory, outputPath, jsPath, assetsPath);
63
- const styleDir = path.join(appDirectory, STYLE_DIRS);
64
- const outputDirToStyle = path.join(appDirectory, outputPath, assetsPath);
61
+ const {
62
+ outputPath,
63
+ bundlelessOptions
64
+ } = config;
65
+ const {
66
+ static: {
67
+ path: staticPath = './'
68
+ } = {
69
+ path: './'
70
+ }
71
+ } = bundlelessOptions;
72
+ const srcDir = path.join(appDirectory, (_bundlelessOptions$so = bundlelessOptions.sourceDir) !== null && _bundlelessOptions$so !== void 0 ? _bundlelessOptions$so : SRC_DIRS);
73
+ const outputDirToSrc = path.join(appDirectory, distPath, outputPath, staticPath);
65
74
  copyAssets({
66
75
  targetDir: srcDir,
67
76
  outputDir: outputDirToSrc
68
77
  });
69
- copyAssets({
70
- targetDir: styleDir,
71
- outputDir: outputDirToStyle
72
- });
73
- copyUtils.copyTask({
78
+ await copyUtils.copyTask({
74
79
  modernConfig,
75
80
  appContext
76
81
  });
77
82
 
78
83
  if (config.watch) {
79
- watchAssets({
84
+ watchAssets(api, {
80
85
  targetDir: srcDir,
81
86
  outputDir: outputDirToSrc
82
87
  });
83
- watchAssets({
84
- targetDir: styleDir,
85
- outputDir: outputDirToStyle
86
- });
87
88
  }
88
- };
89
-
90
- (async () => {
91
- let options;
92
-
93
- if (process.env.CORE_INIT_OPTION_FILE) {
94
- ({
95
- options
96
- } = require(process.env.CORE_INIT_OPTION_FILE));
97
- }
98
-
99
- const {
100
- resolved: modernConfig,
101
- appContext
102
- } = await core.cli.init([], options);
103
-
104
- (() => {
105
- try {
106
- taskMain({
107
- modernConfig,
108
- appContext
109
- });
110
- } catch (e) {
111
- console.error(e.message);
112
- }
113
- })();
114
- })();
89
+ };