@shuvi/toolpack 1.0.1 → 1.0.3

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.
@@ -8,12 +8,11 @@ export interface BaseOptions {
8
8
  outputDir: string;
9
9
  cacheDir: string;
10
10
  include: (string | RegExp)[];
11
- typescript?: {
11
+ jsConfig?: {
12
12
  useTypeScript: boolean;
13
13
  typeScriptPath?: string;
14
- tsConfigPath?: string;
15
- tsCompilerOptions?: TsCompilerOptions;
16
- resolvedBaseUrl?: string;
14
+ compilerOptions: TsCompilerOptions;
15
+ resolvedBaseUrl: string;
17
16
  };
18
17
  target?: string;
19
18
  publicPath?: string;
@@ -29,4 +28,4 @@ export declare function getDefineEnv(env: {
29
28
  }): {
30
29
  [x: string]: string;
31
30
  };
32
- export declare function baseWebpackChain({ dev, outputDir, lightningCss, compiler, projectRoot, include, typescript, name, publicPath, env, cacheDir }: BaseOptions): WebpackChain;
31
+ export declare function baseWebpackChain({ dev, outputDir, lightningCss, compiler, projectRoot, include, jsConfig, name, publicPath, env, cacheDir }: BaseOptions): WebpackChain;
@@ -37,7 +37,6 @@ const constants_1 = require("@shuvi/shared/lib/constants");
37
37
  const fix_watching_plugin_1 = __importDefault(require("../plugins/fix-watching-plugin"));
38
38
  const crypto = __importStar(require("crypto"));
39
39
  const jsconfig_paths_plugin_1 = __importDefault(require("../plugins/jsconfig-paths-plugin"));
40
- const support_ts_extension_resolver_plugin_1 = __importDefault(require("../plugins/support-ts-extension-resolver-plugin"));
41
40
  const helpers_1 = require("./parts/helpers");
42
41
  const resolveLocalLoader = (name) => path.join(__dirname, `../loaders/${name}`);
43
42
  const terserOptions = {
@@ -73,8 +72,7 @@ function getDefineEnv(env) {
73
72
  }, {}));
74
73
  }
75
74
  exports.getDefineEnv = getDefineEnv;
76
- function baseWebpackChain({ dev, outputDir, lightningCss, compiler, projectRoot, include, typescript, name, publicPath = '/', env = {}, cacheDir }) {
77
- var _a;
75
+ function baseWebpackChain({ dev, outputDir, lightningCss, compiler, projectRoot, include, jsConfig, name, publicPath = '/', env = {}, cacheDir }) {
78
76
  const config = new webpack_chain_1.default();
79
77
  config.mode(dev ? 'development' : 'production');
80
78
  config.bail(!dev);
@@ -225,19 +223,12 @@ function baseWebpackChain({ dev, outputDir, lightningCss, compiler, projectRoot,
225
223
  config.cache(typeof process.env.SHUVI_DEV_DISABLE_CACHE !== 'undefined'
226
224
  ? false
227
225
  : getCacheConfig());
228
- if (((_a = typescript === null || typescript === void 0 ? void 0 : typescript.tsCompilerOptions) === null || _a === void 0 ? void 0 : _a.paths) && (typescript === null || typescript === void 0 ? void 0 : typescript.resolvedBaseUrl)) {
229
- config.resolve
230
- .plugin('jsconfig-paths-plugin')
231
- .use(jsconfig_paths_plugin_1.default, [
232
- typescript.tsCompilerOptions.paths,
233
- typescript.resolvedBaseUrl
234
- ]);
235
- }
236
- if (!(typescript === null || typescript === void 0 ? void 0 : typescript.useTypeScript)) {
237
- config.resolve
238
- .plugin('support-ts-extension-resolver-plugin')
239
- .use(support_ts_extension_resolver_plugin_1.default);
240
- }
226
+ config.resolve
227
+ .plugin('jsconfig-paths-plugin')
228
+ .use(jsconfig_paths_plugin_1.default, [
229
+ (jsConfig === null || jsConfig === void 0 ? void 0 : jsConfig.compilerOptions.paths) || {},
230
+ (jsConfig === null || jsConfig === void 0 ? void 0 : jsConfig.resolvedBaseUrl) || projectRoot
231
+ ]);
241
232
  if (dev) {
242
233
  // For webpack-dev-middleware usage
243
234
  config.watchOptions({
@@ -1,7 +1,5 @@
1
- import { IWebpackHelpers } from '../types';
2
1
  import { WebpackChain, BaseOptions } from './base';
3
2
  export interface BrowserOptions extends BaseOptions {
4
- webpackHelpers: IWebpackHelpers;
5
3
  analyze?: boolean;
6
4
  }
7
5
  export declare function createBrowserWebpackChain(options: BrowserOptions): WebpackChain;
@@ -50,13 +50,14 @@ const FRAMEWORK_REACT_MODULES = [
50
50
  }
51
51
  ];
52
52
  function createBrowserWebpackChain(options) {
53
- const { cacheDir, typescript, dev, publicPath, analyze } = options;
53
+ const { projectRoot, cacheDir, jsConfig, dev, publicPath, analyze } = options;
54
54
  const chain = (0, base_1.baseWebpackChain)(options);
55
- const useTypeScript = !!(typescript === null || typescript === void 0 ? void 0 : typescript.useTypeScript);
55
+ const useTypeScript = !!(jsConfig === null || jsConfig === void 0 ? void 0 : jsConfig.useTypeScript);
56
56
  chain.target('web');
57
57
  chain.devtool(dev ? 'cheap-module-source-map' : false);
58
58
  chain.resolve.extensions.merge([
59
- ...(useTypeScript ? ['.ts', '.tsx'] : []),
59
+ '.ts',
60
+ '.tsx',
60
61
  '.mjs',
61
62
  '.js',
62
63
  '.jsx',
@@ -69,9 +70,9 @@ function createBrowserWebpackChain(options) {
69
70
  .use(require.resolve('fork-ts-checker-webpack-plugin'), [
70
71
  {
71
72
  typescript: {
72
- configFile: typescript.tsConfigPath,
73
+ configFile: path.join(projectRoot, 'tsconfig.json'),
73
74
  mode: 'write-references',
74
- typeScriptPath: typescript.typeScriptPath,
75
+ typeScriptPath: jsConfig.typeScriptPath,
75
76
  diagnosticOptions: {
76
77
  syntactic: true
77
78
  },
@@ -1,6 +1,2 @@
1
1
  import { WebpackChain, BaseOptions } from './base';
2
- import { IWebpackHelpers } from '../types';
3
- export interface NodeOptions extends BaseOptions {
4
- webpackHelpers: IWebpackHelpers;
5
- }
6
- export declare function createNodeWebpackChain(options: NodeOptions): WebpackChain;
2
+ export declare function createNodeWebpackChain(options: BaseOptions): WebpackChain;
@@ -4,14 +4,15 @@ exports.createNodeWebpackChain = void 0;
4
4
  const base_1 = require("./base");
5
5
  const external_1 = require("./parts/external");
6
6
  const style_1 = require("./parts/style");
7
+ const helpers_1 = require("./parts/helpers");
7
8
  function createNodeWebpackChain(options) {
8
- const { webpackHelpers, typescript, dev } = options;
9
+ const { dev } = options;
9
10
  const chain = (0, base_1.baseWebpackChain)(options);
10
- const useTypeScript = !!(typescript === null || typescript === void 0 ? void 0 : typescript.useTypeScript);
11
11
  chain.target('node');
12
12
  chain.devtool(dev ? 'cheap-module-source-map' : false);
13
13
  chain.resolve.extensions.merge([
14
- ...(useTypeScript ? ['.ts', '.tsx'] : []),
14
+ '.ts',
15
+ '.tsx',
15
16
  '.js',
16
17
  '.mjs',
17
18
  '.jsx',
@@ -23,7 +24,7 @@ function createNodeWebpackChain(options) {
23
24
  chain.resolve.mainFields.clear().add('main').add('module');
24
25
  chain.output.libraryTarget('commonjs2');
25
26
  chain.optimization.minimize(false);
26
- webpackHelpers.addExternals(chain, (0, external_1.nodeExternals)({
27
+ (0, helpers_1.addExternals)(chain, (0, external_1.nodeExternals)({
27
28
  projectRoot: options.projectRoot,
28
29
  include: options.include
29
30
  }));
@@ -1,5 +1,7 @@
1
- import { IWebpackHelpers } from '../../types';
2
- export declare const webpackHelpers: () => IWebpackHelpers;
1
+ import { WebpackChain } from '../base';
2
+ import { ExternalsFunction } from '../../types';
3
+ export declare const checkWebpackExternals: (webpackChain: WebpackChain) => void;
4
+ export declare const addExternals: (webpackChain: WebpackChain, externalFn: ExternalsFunction) => void;
3
5
  export declare function shouldUseRelativeAssetPaths(publicPath: string): boolean;
4
6
  export declare function splitChunksFilter(chunk: any): boolean;
5
7
  export declare const commonChunkFilename: ({ dev }: {
@@ -1,7 +1,19 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.commonChunkFilename = exports.splitChunksFilter = exports.shouldUseRelativeAssetPaths = exports.webpackHelpers = void 0;
4
- const webpackHelpers = () => {
6
+ exports.commonChunkFilename = exports.splitChunksFilter = exports.shouldUseRelativeAssetPaths = exports.addExternals = exports.checkWebpackExternals = void 0;
7
+ const invariant_1 = __importDefault(require("@shuvi/utils/lib/invariant"));
8
+ const externalsFunctionMap = new WeakMap();
9
+ const checkWebpackExternals = (webpackChain) => {
10
+ let externals = webpackChain.get('externals');
11
+ (0, invariant_1.default)(!externals ||
12
+ (typeof externals === 'function' &&
13
+ externals.name === 'defaultExternalsFn'), `Externals was modified directly, addExternals will have no effect.`);
14
+ };
15
+ exports.checkWebpackExternals = checkWebpackExternals;
16
+ const initExternalsHelpers = (webpackChain) => {
5
17
  const externalFns = [];
6
18
  const defaultExternalsFn = ({ context, request }, callback) => {
7
19
  let callbackCalled = false;
@@ -27,23 +39,26 @@ const webpackHelpers = () => {
27
39
  callback(null, undefined);
28
40
  }
29
41
  };
30
- return {
31
- addExternals: (webpackChain, externalFn) => {
32
- let externals = webpackChain.get('externals');
33
- if (!externals) {
34
- externals = defaultExternalsFn;
35
- webpackChain.externals(externals);
36
- }
37
- if (typeof externals === 'function' &&
38
- externals.name === 'defaultExternalsFn') {
39
- externalFns.push(externalFn);
40
- return;
41
- }
42
- throw new Error('Externals was modified directly, addExternals will have no effect.');
43
- }
44
- };
42
+ let externals = webpackChain.get('externals');
43
+ (0, invariant_1.default)(!externals, `webpackChain externals has been set, initWebpackHelpers can't work as expected.`);
44
+ if (!externals) {
45
+ externals = defaultExternalsFn;
46
+ webpackChain.externals(externals);
47
+ externalsFunctionMap.set(webpackChain, externalFns);
48
+ }
49
+ };
50
+ const addExternals = (webpackChain, externalFn) => {
51
+ let externals = webpackChain.get('externals');
52
+ if (!externals) {
53
+ initExternalsHelpers(webpackChain);
54
+ }
55
+ else {
56
+ (0, exports.checkWebpackExternals)(webpackChain);
57
+ }
58
+ const externalFns = externalsFunctionMap.get(webpackChain);
59
+ externalFns.push(externalFn);
45
60
  };
46
- exports.webpackHelpers = webpackHelpers;
61
+ exports.addExternals = addExternals;
47
62
  function shouldUseRelativeAssetPaths(publicPath) {
48
63
  return publicPath === './';
49
64
  }
@@ -38,6 +38,7 @@ declare type Paths = {
38
38
  export default class JsConfigPathsPlugin implements ResolvePluginInstance {
39
39
  paths: Paths;
40
40
  resolvedBaseUrl: string;
41
+ jsConfigPlugin: boolean;
41
42
  constructor(paths: Paths, resolvedBaseUrl: string);
42
43
  apply(resolver: Resolver): void;
43
44
  }
@@ -163,19 +163,19 @@ class JsConfigPathsPlugin {
163
163
  constructor(paths, resolvedBaseUrl) {
164
164
  this.paths = paths;
165
165
  this.resolvedBaseUrl = resolvedBaseUrl;
166
+ this.jsConfigPlugin = true;
166
167
  }
167
168
  apply(resolver) {
168
- const paths = this.paths;
169
- const pathsKeys = Object.keys(paths);
170
- // If no aliases are added bail out
171
- if (pathsKeys.length === 0) {
172
- return;
173
- }
174
- const baseDirectory = this.resolvedBaseUrl;
175
169
  const target = resolver.ensureHook('resolve');
176
170
  resolver
177
171
  .getHook('described-resolve')
178
172
  .tapAsync('JsConfigPathsPlugin', (request, resolveContext, callback) => {
173
+ const paths = this.paths;
174
+ const pathsKeys = Object.keys(paths);
175
+ // If no aliases are added bail out
176
+ if (pathsKeys.length === 0) {
177
+ return callback();
178
+ }
179
179
  const moduleName = request.request;
180
180
  // Exclude node_modules from paths support (speeds up resolving)
181
181
  if (request.path.match(NODE_MODULES_REGEX)) {
@@ -209,7 +209,7 @@ class JsConfigPathsPlugin {
209
209
  // try next path candidate
210
210
  return pathCallback();
211
211
  }
212
- const candidate = path.join(baseDirectory, curPath);
212
+ const candidate = path.join(this.resolvedBaseUrl, curPath);
213
213
  const obj = Object.assign({}, request, {
214
214
  request: candidate
215
215
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shuvi/toolpack",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/shuvijs/shuvi.git",
@@ -20,7 +20,7 @@
20
20
  "node": ">= 12.0.0"
21
21
  },
22
22
  "dependencies": {
23
- "@shuvi/compiler": "1.0.1",
23
+ "@shuvi/compiler": "1.0.3",
24
24
  "@babel/core": "7.12.10",
25
25
  "@babel/plugin-proposal-class-properties": "7.12.1",
26
26
  "@babel/plugin-proposal-nullish-coalescing-operator": "7.10.1",
@@ -35,8 +35,8 @@
35
35
  "@babel/preset-typescript": "7.12.7",
36
36
  "@babel/runtime": "7.12.5",
37
37
  "lightningcss": "1.15.0",
38
- "@shuvi/shared": "1.0.1",
39
- "@shuvi/utils": "1.0.1",
38
+ "@shuvi/shared": "1.0.3",
39
+ "@shuvi/utils": "1.0.3",
40
40
  "babel-loader": "8.2.2",
41
41
  "babel-plugin-syntax-jsx": "6.18.0",
42
42
  "babel-plugin-transform-define": "2.0.0",