@common-stack/rollup-vite-utils 4.0.1-alpha.52 → 4.0.1-alpha.54

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.
@@ -1,9 +1,13 @@
1
- import { Plugin } from 'rollup';
2
1
  interface AddJsExtensionOptions {
3
- include?: string | string[];
4
- exclude?: string | string[];
5
2
  packages: string[] | '*';
6
3
  needToAddIndexJs: string[];
4
+ excludeImports?: string[];
7
5
  }
8
- declare const addJsExtensionToImportsPlugin: (options: AddJsExtensionOptions) => Plugin;
6
+ declare const addJsExtensionToImportsPlugin: (options: AddJsExtensionOptions) => {
7
+ name: string;
8
+ transform(code: string, id: string): {
9
+ code: any;
10
+ map: any;
11
+ };
12
+ };
9
13
  export default addJsExtensionToImportsPlugin;
@@ -1,48 +1,69 @@
1
- import {createFilter}from'@rollup/pluginutils';const addJsExtensionToImportsPlugin = (options) => {
2
- const filter = createFilter(options.include, options.exclude, { resolve: false });
1
+ import {parse}from'@babel/parser';import generateSource from'@babel/generator';import traverseSource from'@babel/traverse';// to support esm default
2
+ // https://github.com/babel/babel/issues/15269#issuecomment-1505804243
3
+ const traverse = traverseSource.default ?? traverseSource;
4
+ const generate = generateSource.default ?? generateSource;
5
+ const nonJsExtensions = ['.css', '.scss', '.less', '.svg', '.png', '.jpg', '.jpeg', '.gif', '.json'];
6
+ const addJsExtensionToImportsPlugin = (options) => {
3
7
  // Add known packages that need `/index.js` if they are not already included
4
8
  const knownNeedToAddIndexJs = ['@apollo/client'];
5
9
  const needToAddIndexJs = Array.from(new Set([...options.needToAddIndexJs, ...knownNeedToAddIndexJs]));
6
10
  return {
7
- name: 'rollup-plugin-add-js-extension',
11
+ name: 'add-js-extension-to-imports',
8
12
  transform(code, id) {
9
- if (!filter(id))
13
+ // Only apply the transformation to .ts, .tsx, .js, and .jsx files
14
+ if (!/\.(tsx?|jsx?)$/.test(id))
10
15
  return null;
11
- const packageNameRegex = (importPath) => {
12
- if (importPath.startsWith('@')) {
13
- // Scoped package
14
- const match = importPath.match(/^(@[^/]+\/[^/]+)/);
15
- return match ? match[1] : null;
16
- }
17
- else {
18
- // Normal package
19
- const match = importPath.match(/^([^/]+)/);
20
- return match ? match[1] : null;
21
- }
22
- };
23
- const transformedCode = code.replace(/import\s+(['"])([^'"]+)\1/g, (match, quote, importPath) => {
24
- const packageName = packageNameRegex(importPath);
25
- !importPath.startsWith('@');
26
- const isInExceptionList = packageName && needToAddIndexJs.includes(packageName);
27
- const isModularPackage = importPath.startsWith('@');
28
- const hasAdditionalSlashes = isModularPackage && importPath.split('/').length > 2;
29
- const shouldTransform = (options.packages === '*' && importPath.includes('/') && !isModularPackage) ||
30
- (options.packages === '*' && isModularPackage && hasAdditionalSlashes) ||
31
- (isModularPackage && isInExceptionList) ||
32
- (packageName && options.packages.includes(packageName));
33
- if (shouldTransform && !importPath.endsWith('.js')) {
34
- if (isInExceptionList) {
35
- return `import ${quote}${importPath}/index.js${quote}`;
36
- }
37
- else {
38
- return `import ${quote}${importPath}.js${quote}`;
16
+ const ast = parse(code, {
17
+ sourceType: 'module',
18
+ plugins: ['typescript', 'jsx'], // Adjusted to 'typescript' for .tsx files
19
+ });
20
+ traverse(ast, {
21
+ ImportDeclaration(path) {
22
+ const source = path.node.source.value;
23
+ // Ignore paths that start with './', '../', end with non-JS extensions, or are in excluded imports
24
+ if (source.startsWith('./') ||
25
+ source.startsWith('../') ||
26
+ nonJsExtensions.some(ext => source.endsWith(ext)) ||
27
+ (options.excludeImports && options.excludeImports.includes(source)))
28
+ return;
29
+ // Function to extract package name from the import source
30
+ const getPackageName = (importPath) => {
31
+ if (importPath.startsWith('@')) {
32
+ // Scoped package
33
+ const match = importPath.match(/^(@[^/]+\/[^/]+)/);
34
+ return match ? match[1] : null;
35
+ }
36
+ else {
37
+ // Normal package
38
+ const match = importPath.match(/^([^/]+)/);
39
+ return match ? match[1] : null;
40
+ }
41
+ };
42
+ const packageName = getPackageName(source);
43
+ // Determine if we should transform the import based on the package patterns
44
+ !source.startsWith('@');
45
+ const isInExceptionList = packageName && needToAddIndexJs.includes(packageName);
46
+ const isModularPackage = source.startsWith('@');
47
+ const hasAdditionalSlashes = isModularPackage && source.split('/').length > 2;
48
+ const shouldTransform = (options.packages === '*' && source.includes('/') && !isModularPackage) ||
49
+ (options.packages === '*' && isModularPackage && hasAdditionalSlashes) ||
50
+ (isModularPackage && isInExceptionList) ||
51
+ (packageName && options.packages.includes(packageName));
52
+ // Append ".js" or "/index.js" to imports if they match the criteria and don't already end with ".js"
53
+ if (shouldTransform && !source.endsWith('.js')) {
54
+ if (isInExceptionList) {
55
+ path.node.source.value = `${source}/index.js`;
56
+ }
57
+ else {
58
+ path.node.source.value = `${source}.js`;
59
+ }
39
60
  }
40
- }
41
- return match;
61
+ },
42
62
  });
63
+ const output = generate(ast, {}, code);
43
64
  return {
44
- code: transformedCode,
45
- map: null,
65
+ code: output.code,
66
+ map: output.map,
46
67
  };
47
68
  },
48
69
  };
@@ -1 +1 @@
1
- {"version":3,"file":"rollupPluginAddJsExtension.js","sources":["../../src/rollup/rollupPluginAddJsExtension.ts"],"sourcesContent":[null],"names":[],"mappings":"+CAUA,MAAM,6BAA6B,GAAG,CAAC,OAA8B,KAAY;AAC7E,IAAA,MAAM,MAAM,GAAG,YAAY,CAAC,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,OAAO,EAAE,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC;;AAGlF,IAAA,MAAM,qBAAqB,GAAG,CAAC,gBAAgB,CAAC,CAAC;IACjD,MAAM,gBAAgB,GAAG,KAAK,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC,GAAG,OAAO,CAAC,gBAAgB,EAAE,GAAG,qBAAqB,CAAC,CAAC,CAAC,CAAC;IAEtG,OAAO;AACH,QAAA,IAAI,EAAE,gCAAgC;QACtC,SAAS,CAAC,IAAY,EAAE,EAAU,EAAA;AAC9B,YAAA,IAAI,CAAC,MAAM,CAAC,E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@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@common-stack/rollup-vite-utils",
3
- "version": "4.0.1-alpha.52",
3
+ "version": "4.0.1-alpha.54",
4
4
  "description": "Client Module for react app",
5
5
  "homepage": "https://github.com/cdmbase/fullstack-pro#readme",
6
6
  "bugs": {
@@ -54,5 +54,5 @@
54
54
  "typescript": {
55
55
  "definition": "lib/index.d.ts"
56
56
  },
57
- "gitHead": "14099d8395f365c088145a6834371efc6b28ce15"
57
+ "gitHead": "d50ab31d309df1e0eec3ea8ff07d2eeb566f0744"
58
58
  }