magic-comments
Advanced tools
Comparing version 1.0.0-alpha.0 to 1.0.0-rc.0
@@ -1,2 +0,2 @@ | ||
import { getOverrideSchema, pathIsMatch, importPrefix } from './util.js'; | ||
import { getOverrideSchema, pathIsMatch, relativePathPrefix } from './util.js'; | ||
const defaultSchema = { | ||
@@ -40,3 +40,3 @@ type: 'object', | ||
}) => { | ||
const path = match === 'import' ? importPath.replace(importPrefix, '') : filepath; | ||
const path = match === 'import' ? importPath.replace(relativePathPrefix, '') : filepath; | ||
if (value === true) { | ||
@@ -43,0 +43,0 @@ return defaultConfig; |
@@ -11,3 +11,3 @@ export { webpackChunkName, schema as webpackChunkNameSchema } from './webpackChunkName.js'; | ||
export { dynamicImportsWithoutComments } from './util.js'; | ||
export { commentFor } from './strategy.js'; | ||
export { commentFor, getMagicComment } from './strategy.js'; | ||
export { schema } from './schema.js'; |
@@ -13,2 +13,8 @@ import { schema as webpackChunkNameSchema } from './webpackChunkName.js'; | ||
properties: { | ||
verbose: { | ||
type: 'boolean' | ||
}, | ||
match: { | ||
enum: ['module', 'import'] | ||
}, | ||
webpackChunkName: webpackChunkNameSchema, | ||
@@ -15,0 +21,0 @@ webpackFetchPriority: webpackFetchPrioritySchema, |
@@ -21,2 +21,19 @@ import { webpackChunkName } from './webpackChunkName.js'; | ||
}; | ||
export { commentFor }; | ||
const getMagicComment = ({ | ||
modulePath, | ||
importPath, | ||
options = { | ||
webpackChunkName: true | ||
}, | ||
match = 'module', | ||
open = false | ||
}) => { | ||
const bareImportPath = importPath.trim().replace(/^['"`]|['"`]$/g, ''); | ||
const magic = Object.entries(options).map(([key, value]) => commentFor[key](modulePath, bareImportPath, value, match)).filter(Boolean); | ||
if (!magic.length) { | ||
// The provided comment values produced only falsy results | ||
return ''; | ||
} | ||
return open ? ` ${magic.join(', ')} ` : `/* ${magic.join(', ')} */`; | ||
}; | ||
export { commentFor, getMagicComment }; |
@@ -49,7 +49,4 @@ import micromatch from 'micromatch'; | ||
}; | ||
const getBareImportSpecifier = specifier => { | ||
return specifier.replace(/['"`]/g, ''); | ||
}; | ||
const importPrefix = /^(?:(\.{1,2}\/)+)|^\/|^.+:\/\/\/?[.-\w]+\//; | ||
const relativePathPrefix = /^(?:(\.{1,2}\/)+)/; | ||
const dynamicImportsWithoutComments = /(?<![\w.]|#!|(?:\/{2}.+\n?)+|\/\*[\s\w]*?|\*.+?|['"`][^)$,\n]*)import\s*\((?!\s*\/\*)(?<path>\s*?['"`][^)]+['"`]\s*)\)(?!\s*?\*\/)/gm; | ||
export { getBareImportSpecifier, getOverrideConfig, getOverrideSchema, pathIsMatch, importPrefix, dynamicImportsWithoutComments }; | ||
export { getOverrideConfig, getOverrideSchema, pathIsMatch, relativePathPrefix, dynamicImportsWithoutComments }; |
{ | ||
"name": "magic-comments", | ||
"version": "1.0.0-alpha.0", | ||
"version": "1.0.0-rc.0", | ||
"description": "Utility for adding webpack magic comments at build time.", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -6,10 +6,10 @@ # ✨ [`magic-comments`](https://www.npmjs.com/package/magic-comments) | ||
Configurable utility for keeping your source code clean, and adding webpack [magic comments](https://webpack.js.org/api/module-methods/#magic-comments) to your dynamic `import()` expressions at build time. | ||
Tooling utility to add configurable webpack [magic comments](https://webpack.js.org/api/module-methods/#magic-comments) to dynamic `import()` expressions at build time. | ||
Useful when working with: | ||
* babel plugins | ||
* webpack loaders | ||
* vite plugins | ||
* anywhere you want to add magic comments to your source code before running through webpack | ||
* [Babel plugins](https://babeljs.io/docs/plugins) | ||
* [Webpack loaders](https://webpack.js.org/loaders/) | ||
* [Vite plugins](https://vitejs.dev/guide/api-plugin.html) | ||
* Anywhere you want to add magic comments to your source code before running through webpack. | ||
@@ -24,2 +24,37 @@ ## Getting Started | ||
Next ... (TODO) | ||
Next generate an AST or RegExp that provides the following information for each file processed: | ||
* The absolute filename of the file being processed (`modulePath`). | ||
* The import specifier used in the dynamic imports found (`importPath`). | ||
Then pass that information along to `magic-comments` to generate a magic comment that can be inserted into an `import()` expression: | ||
**src/file.js** | ||
```js | ||
const mod = import('./folder/module.js') | ||
``` | ||
**tooling** | ||
```js | ||
import { getMagicComment } from 'magic-comments' | ||
const modulePath = resolve(directory, './src/file.js') | ||
const code = fs.readFileSync(modulePath) | ||
const ast = parse(code) | ||
const dynamicImportsMeta = getDynamicImportMetaFrom(ast) | ||
dynamicImportsMeta.forEach(meta) => { | ||
const magicComment = getMagicComment({ | ||
modulePath, | ||
importPath: meta.importPath, | ||
options: { | ||
webpackChunkName: true, | ||
webpackFetchPriority: "high" | ||
} | ||
}) | ||
// prints /* webpackChunkName: "folder-module", webpackFetchPriority: "high" */ | ||
console.log(magicComment) | ||
}) | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
42421
1367
59